Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using table that use auto paging, when i get data from database it show all record from DB and show in table using list. If record are 10,000 it take time. I want to use such trick that not show all record it just show 10 record and when click on page two it get 11-20 record from database.

How can it possible, and this technique is best or not

Can i use store procedure for this purpose

What I have tried:

function loadData() {
       var Year = $("#year").val();
       var Month = $("#month").val();
       var DataObjectForAjaxCall = { strYear: Year, strMonth: Month };
       if (Year != "" && Month != "") {
           $("#modalCenter").click();
           $.ajax({
               url: "@Url.Action("loadData", "Home")",
               method: "GET",
               data: DataObjectForAjaxCall,
               async: false,
               success: function (Result) {
                   $('.modal-body').html(Result);
               }
           });
           $("#hideLoader").click();
       } else {
           Alerts("Select Year and Month First");
       }

   }
Posted
Updated 5-Jul-17 0:13am

1 solution

A stored procedure is perfect for this.

The technique you're looking for is called "Paging".

You will have to extend your DataObjectForAjaxCall object to include page number and maybe a page size (but you might want to assume 10).

JavaScript
function loadData(pageNum) {

       if(pageNum == undefined)
          pageNum = 1;

       var Year = $("#year").val();
       var Month = $("#month").val();
       var DataObjectForAjaxCall = { strYear: Year, strMonth: Month, iPageNum: pageNum, iPageSize: 10 };
// ...


change the loadData action to use these new parameters to call the procedure.


In the procedure, you will need an index column:
SQL
SELECT *,
          ROW_NUMBER() OVER (ORDER BY @sort DESC) AS [Index]
      FROM myResults

Index will number the rows from 1 to n. Use this in the WHERE clause:
SQL
WHERE [Index] BETWEEN (@pageNum-1)*@pageSize AND (@pageNum)*@pageSize


There are loads of examples of Paging online. here's a simple example:
sql - Pagination with the stored procedure - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900