简体   繁体   中英

Pagination in Jquery Datatable in asp.net webforms

I have an API, which accepts page number and page size as a parameter to return user details. Loading data to the jquery data table. Needs to pass page number and size to API for fetch the data each time. How can I get and pass the page number to webmethod and enable next button all time. Because when i loaded first time data it only shows page number as 1 and Next button is disabled.

 var tableUserDetails = $("#grdUser").DataTable({
    processing: true,
    filter: true,
    orderMulti: false,
    paging: true,
    searching: true,
    bFilter: true,
    bsort: true,
    bInfo: true,
    pagingType: "simple",
    columns: [{ "data": "Id" },
    { "data": "Name" },
    { "data": "userName" },
    { "data": "email" },
    { "data": "role" }
    ]
});

function getUsers() {
    var info =tableUserDetails.page.info();
    $.ajax({
        data: '{pageNumber:' + info.page+1 + ', pageSize:' + 10 + '}',
        type: "POST",
        url: "MyPage.aspx/GetUsers",
        contentType: Constants.ContentType,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            
        },
    });
}

I think that you are looking for this: DataTables server side processing . The configuration you have is not loading partially the data. It assumes you have all the rows when you set the dataset.

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "../server_side/scripts/server_processing.php"
    } );
} );

And the format of the returned data should be like this:

{
  "draw": 3,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    [
      "Airi",
      "Satou",
      "Accountant",
      "Tokyo",
      "28th Nov 08",
      "$162,700"
    ],
    /* etc */

An example of C# like response:

    /// <summary>
    ///     Resultset to be JSON stringified and set back to client.
    /// </summary>
    [Serializable]
    [SuppressMessage("ReSharper", "InconsistentNaming")]
    public class DataTableResultSet
    {
        /// <summary>Array of records. Each element of the array is itself an array of columns</summary>
        public List<List<string>> data = new List<List<string>>();

        /// <summary>value of draw parameter sent by client</summary>
        public int draw;

        /// <summary>filtered record count</summary>
        public int recordsFiltered;

        /// <summary>total record count in resultset</summary>
        public int recordsTotal;

        public string ToJSON()
        {
            return JsonConvert.SerializeObject(this);
        }
    }

Edit - How to post with ajax

  $(document).ready(function () {
    $('#mytable').DataTable({
      processing: true,
      serverSide: true,
      ajax: {
        data: '{pageNumber:' + info.page+1 + ', pageSize:' + 10 + '}',
        type: "POST",
        url: "MyPage.aspx/GetUsers",
        contentType: Constants.ContentType,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
        },
        success: function (result) {
            
        }
    });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM