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

时间:2018-10-30 07:47:40

标签: c# datatables

我正在使用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代码

  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);
}

如果我将服务器端处理设置为false,则此方法工作正常,但是当我将其设置为true时,会出现某些错误。有人可以帮我还是建议如何在代码中传递参数,以使其正常工作。

请告知操作方法。

2 个答案:

答案 0 :(得分:0)

根据DataTables documentation,“通过将serverSide选项设置为true 并通过ajax option提供Ajax数据源来启用服务器端处理。”

当我查看您的代码时,看不到您使用此ajax option

此外,如果要使用服务器端处理,请you can initialize a DataTable instance as soon as your document is ready instead of having inside of an ajax success callback。像这样:

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

答案 1 :(得分:0)

$(document).ready(function(){     $('#id')。DataTable({
        销毁:是的,         “ serverSide”:是的,         “处理”:true,         “ ajax”:{
            “ url”:“您的URL”,             “ type”:“ POST”,             “数据”:{}         },     }); });