简体   繁体   English

asp.net 网络表单中 Jquery 数据表中的分页

[英]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.我有一个 API,它接受页码和页面大小作为返回用户详细信息的参数。 Loading data to the jquery data table.将数据加载到 jquery 数据表。 Needs to pass page number and size to API for fetch the data each time.每次都需要将页码和大小传递给 API 以获取数据。 How can I get and pass the page number to webmethod and enable next button all time.如何获取页码并将其传递给 webmethod 并始终启用下一个按钮。 Because when i loaded first time data it only shows page number as 1 and Next button is disabled.因为当我加载第一次数据时,它只显示页码为 1 并且下一步按钮被禁用。

 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 .我认为您正在寻找这个: 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: C# 类似响应的示例:

    /// <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编辑 - 如何使用 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) {
            
        }
    });
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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