Тёмный

jQuery datatables stored procedure for paging sorting and searching 

kudvenkat
Подписаться 836 тыс.
Просмотров 76 тыс.
50% 1

Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RU-vid channel. Hope you can help.
/ @aarvikitchen5572
In this video we will discuss implementing a stored procedure that can perform paging sorting and searching. In our upcoming videos we will discuss writing a generic handler that calls this stored procedure. The generic handler will convert the data to JSON format. The JSON formatted data can then be consumed by the jQuery datatables plugin. All the processing, that is, paging, sorting and searching is done on the server side.
If the dataset is small you can retrieve all the data at once and all the processing (i.e paging, searching, sorting) can be done on the client-side. We discussed this in Part 106 for jQuery tutorial.
However, if the dataset is large (i.e if you have millions of records), loading all data at once is not the best thing to do from a performance standpoint. With large dataset, we only want to load the correct set of rows (sorted, paged and filtered data ) that want to display on the page. So all the processing ((i.e paging, searching and sorting) should be done by the server. So in this and our upcoming videos we will discuss performing server side processing.
SQL Script to create the table and populate it with test data
Create table tblEmployees
(
ID int primary key identity,
FirstName nvarchar(50),
LastName nvarchar(50),
Gender nvarchar(20),
JobTitle nvarchar(20)
)
Go
Insert into tblEmployees values('Mark', 'Hastings','Male','Developer')
Insert into tblEmployees values('Maria', 'Nicholas','Female','Developer')
Insert into tblEmployees values('Robert', 'Stephenson','Male','Sr. Developer')
Insert into tblEmployees values('Mary', 'Quant','Female','Sr. Developer')
Insert into tblEmployees values('John', 'Stenson','Male','Sr. Developer')
Insert into tblEmployees values('Gilbert', 'Sullivan','Male','Developer')
Insert into tblEmployees values('Rob', 'Gerald','Male','Sr. Developer')
Insert into tblEmployees values('Ron', 'Simpson','Male','Developer')
Insert into tblEmployees values('Sara', 'Solomon','Female','Sr. Developer')
Insert into tblEmployees values('Rad', 'Wicht','Male','Sr. Developer')
Insert into tblEmployees values('Julian', 'John','Male','Developer')
Insert into tblEmployees values('James', 'Bynes','Male','Sr. Developer')
Insert into tblEmployees values('Mary', 'Ward','Female','Developer')
Insert into tblEmployees values('Michael', 'Niron','Male','Sr. Developer')
SQL Server stored procedure paging sorting and searching
create proc spGetEmployees
@DisplayLength int,
@DisplayStart int,
@SortCol int,
@SortDir nvarchar(10),
@Search nvarchar(255) = NULL
as
begin
Declare @FirstRec int, @LastRec int
Set @FirstRec = @DisplayStart;
Set @LastRec = @DisplayStart + @DisplayLength;
With CTE_Employees as
(
Select ROW_NUMBER() over (order by
case when (@SortCol = 0 and @SortDir='asc')
then Id
end asc,
case when (@SortCol = 0 and @SortDir='desc')
then Id
end desc,
case when (@SortCol = 1 and @SortDir='asc')
then FirstName
end asc,
case when (@SortCol = 1 and @SortDir='desc')
then FirstName
end desc,
case when (@SortCol = 2 and @SortDir='asc')
then LastName
end asc,
case when (@SortCol = 2 and @SortDir='desc')
then LastName
end desc,
case when (@SortCol = 3 and @SortDir='asc')
then Gender
end asc,
case when (@SortCol = 3 and @SortDir='desc')
then Gender
end desc,
case when (@SortCol = 4 and @SortDir='asc')
then JobTitle
end asc,
case when (@SortCol = 4 and @SortDir='desc')
then JobTitle
end desc
)
as RowNum,
COUNT(*) over() as TotalCount,
Id,
FirstName,
LastName,
Gender,
JobTitle
from tblEmployees
where (@Search IS NULL
Or Id like '%' + @Search + '%'
Or FirstName like '%' + @Search + '%'
Or LastName like '%' + @Search + '%'
Or Gender like '%' + @Search + '%'
Or JobTitle like '%' + @Search + '%')
)
Select *
from CTE_Employees
where RowNum ] @FirstRec and RowNum [= @LastRec
end
Finally test the stored procedure

Опубликовано:

 

21 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 19   
@LetCode96666
@LetCode96666 5 лет назад
Venkat, this is probably the 100000th time im saying, U R THE BEST !!! always with the most important tutorials ! Thanks again
@sajjansarkar
@sajjansarkar 8 лет назад
very nice, only one piece missing though, if we do type a search term, datatables needs an additional field to show the total unfiltered rows. This will allow it to display "Showing 1 to 10 of 138 entries (filtered from *7,506* total entries)" the missing variable here is the 7506 one, I got around this by doing a separate SELECT COUNT(*) without the @Search condition. Also out of curiosity, in the final SELECT, wouldnt it be faster if you did SELECT TOP (@DisplayLength) from cte instead of processing the whole CTE set to match the RowNum? In the TOP case,SQL Server will stop processing the rest of the CTE once @DisplayLength records have been found, no?
@imadabab
@imadabab 4 года назад
Videos 109, 110, 111 and 112 are excellent. Thanks a lot. Appreciate if you add another video number 114 explaining how to implement customer column search with server side jquery datatable. This is will be perfect.
@ronchennai
@ronchennai 9 лет назад
Thanks Venkat. Hoping your next series will be AngularJS... :)
@saikiran538
@saikiran538 6 лет назад
Hello Sir, excellent explanation. Could you please make an application to show data on bootstrap table with server side pagination and search functionality.? Can you please do that with returning json data. Thanks in Advance
@gurpreetsingharora8018
@gurpreetsingharora8018 5 лет назад
Can we do the same thing using sql query without using Stored Procedure
@ymtan
@ymtan 9 лет назад
Excellent video sir.
@AsimMughal9
@AsimMughal9 8 лет назад
Hello Sir, did you have any demo for inline jQuery editable datatable on youtube? I would appreciate it :)
@RSdevLife
@RSdevLife 5 лет назад
Where is the part where you code it in the jquery? I'm interested in how to code it on the client side and how it handles the paging, sorting and searching. thanks!
@Csharp-video-tutorialsBlogspot
Hello Ross - You can all the jQuery tutorial series videos in logical sequence at the following link. Hope this helps. ru-vid.com/group/PL6n9fhu94yhVDV697uvHpavA3K_eWGQap
@hugomartinez2880
@hugomartinez2880 4 года назад
@@Csharp-video-tutorialsBlogspot hola, entre a esa liga pero no encontré dónde se explica como implementarlo esto a datatables js
@王智立-i8m
@王智立-i8m 2 месяца назад
很有價值,謝謝
@zxcvbnm2020
@zxcvbnm2020 6 лет назад
Works fine but slow with large tables I have a table with 2 millions records , it takes 20 seconds to execute spGetData 1000 ,0,0,'desc' would you please let me know how can i enhance it more
@ali-13392
@ali-13392 3 года назад
I believe it's due to LIKE operator. There's another approach known as "Full Text Search" in SQL server for searching up records, also there's 1 more approach "nGrams". Check them out, hope that helps!
@gurpreetsingharora8018
@gurpreetsingharora8018 5 лет назад
Can someone help me to do the same thing but without using stored procedure
@josephregallis3394
@josephregallis3394 6 лет назад
When I test the stored procedure with spGetEmployees 10, 0, 1, 'asc', 'Male' I don't get all 'Male' employees.
@rezkarjs2808
@rezkarjs2808 6 лет назад
Because it uses "like clause", where 'female' contains 'male'
@mahipalkamanchi
@mahipalkamanchi 9 лет назад
thank u sir...!
Далее
asp net generic handler return json
19:28
Просмотров 57 тыс.
jQuery datatables get data from database table
21:04
Просмотров 282 тыс.
jQuery datatables individual column search
15:04
Просмотров 95 тыс.
Autocomplete textbox using jquery in asp net
17:30
Просмотров 87 тыс.
jQuery datatable show hide columns
9:05
Просмотров 65 тыс.
jQuery datatables export to pdf
15:22
Просмотров 99 тыс.
jQuery autocomplete with images and text
20:12
Просмотров 28 тыс.