简体   繁体   English

Datatable.net-服务器端处理参数

[英]Datatable.net - server side processing parameters

I am using datatable.net and the html code I have written so far is: 我正在使用datatable.net,到目前为止我写的html代码是:

    $(document).ready(function () {
        $.ajax({
            "url": "Handlers/jQueryDatatableHandler.ashx",
            "data": { Operation: 'EmployeeList', searchText: '' },
            success: function (data) {
                json = JSON.parse(data);
                columns = [];
                // build column titles
                for (var i = 0; i < json.colnames.length; i++) {
                    columns.push({ title: json.colnames[i] });
                }

                var table = $('#example').DataTable({
                    "responsive": true,
                    "processing": true, 
                    "serverSide": true,
                    "order": [[4, 'desc']],
                    data: json.rows,
                    columns: columns,
                    columnDefs: [
                        {
                            targets: 0,                           
                            render: function (data, type, row) {
                                if (type === 'display') {
                                    return '<input type="checkbox" class="editor-active">';
                                }
                                return data;
                            },
                            className: "dt-body-center",
                            "orderable": false,
                            "searchable": false
                        },
                        {
                            targets: 1,
                            visible: false
                        },
                        {
                            targets: -1,
                            visible: false
                        }
                    ]
                });
            }
        });

jQueryDatatableHandler.ashx code jQueryDatatableHandler.ashx代码

  public class DatatableInboxResults
  {
    public int draw { get; set; }
    public int recordsTotal { get; set; }
    public int recordsFiltered { get; set; }
    public List<string> colnames;
    public List<string[]> rows { get; set; }
  }

private string BuildDatatableResults()
{
    EmployeeListParameters mlp = new EmployeeListParameters();       
    mlp.numberOfRows = "10"; //not sure how to pass this value
    mlp.pageIndex = "1"; //not sure how to pass this value
    mlp.sortColumnName = sortColumnName; //not sure how to pass this value
    mlp.sortOrderBy = sortOrderBy; //not sure how to pass this value       
    mlp.searchText = searchTxt;

    DatatableInboxResults result = new DatatableInboxResults();
    result.colnames = new List<string>();

    result.colnames.Add(" ");
    result.colnames.Add("EmployeeId");
    result.colnames.Add("Name");
    result.colnames.Add("Title");
    result.colnames.Add("Joining");
    result.colnames.Add("Viewed");

    int totalRecords;
    int colCount = result.colnames.Count;;
    List<string> rows = new List<string>();
    result.rows = new List<string[]>();
    EmployeeViewerDataProvider mvdp = new EmployeeViewerDataProvider ();
    List<NEmployee> empList;
    msgList = mvdp.GetEmployeeDetails(mlp, out totalRecords);//subscriptionId, username, numberOfRows, pageIndex, sortColumnName, sortOrderBy,strText, out totalRecords);

    foreach (NEmployee msg in empList)
    {
        string[] row = new string[colCount];

        row[0] = "0";
        row[1] = msg.EmployeeId.ToString();
        row[2] = msg.Name;
        row[3] = msg.Title;
        row[4] = TimeZoneInfo.ConvertTimeFromUtc(msg.TimeSent, tinfo).ToString();
        row[5] = msg.Viewed.ToString();

        result.rows.Add(row);
    }
    result.recordsTotal = (Convert.ToInt32(totalRecords) + Convert.ToInt32(mlp.numberOfRows) - 1) / Convert.ToInt32(mlp.numberOfRows);

    return new JavaScriptSerializer().Serialize(result);
}

This is working fine if I set serverside processing to false but when I make it true there are certain errors. 如果将服务器端处理设置为false,则此方法工作正常,但是当我将其设置为true时,会出现某些错误。 Can someone please help me or suggest how to pass parameters in the code so it works fine. 有人可以帮我还是建议如何在代码中传递参数,使其正常工作。

Please advise how to do this. 请告知如何执行此操作。

According to DataTables documentation , "Server-side processing is enabled by setting the serverSide option to true and providing an Ajax data source through the ajax option . ". 根据DataTables文档 ,“通过将serverSide选项设置为true 并通过ajax option提供Ajax数据源来启用服务器端处理

When I look at your code, I don't see you using this ajax option . 当我查看您的代码时,看不到您使用此ajax option

Besides, if you want to use Server-side processing, you can initialize a DataTable instance as soon as your document is ready instead of having inside of an ajax success callback . 此外,如果要使用服务器端处理,则可以在文档准备好后立即初始化DataTable实例,而不用将其包含在ajax成功回调中 Something like: 就像是:

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "Handlers/jQueryDatatableHandler.ashx"
    } );
} );

$(document).ready(function() { $('#id').DataTable({ $(document).ready(function(){$('#id')。DataTable({
destroy: true, "serverSide": true, "processing":true, "ajax": { destroy:true,“ serverSide”:true,“ processing”:true,“ ajax”:{
"url": "your url", "type": "POST", "data": {} }, }); “ url”:“您的url”,“ type”:“ POST”,“ data”:{}},}); }); });

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

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