在datatable上实现服务器端分页

时间:2015-11-10 15:23:14

标签: javascript jquery asp.net-mvc datatables

在mvc控制器内我接收MyTable作为参数

public JsonResult GetListOfData(JobTable result)
{
   var query = ... get data ..
   IQueryable<MyData> resData;
   resData.recordsFiltered = query.Skip(result.start).Take(50).AsQueryable();
   resData.recordsTotal = query.Count(); 
   ... 
   return Json(resData, JsonRequestBehavior.AllowGet);         
}

内部视图我有js代码初始化jquery数据表

function drawTable() {
            var table = $('#myTable').dataTable({
                processing: true,
                serverSide: true,
                searching: false,
                lengthChange: false,
                displayLength: 25,
                order: [[5, 'desc']],
                ajax: {
                    url: '@Url.Action("GetListOfData", "Index")',
                    data: function (d) {
                        ...
                        d.status = $('#Status').val(),                        
                        d.dateFrom = $('#DateFrom').val(),
                        d.dateTo = $('#DateTo').val(),
                        ...                     
                    }
                },

                columns: [
                    { data: 'Id' },                    
                    { data: 'Status' },                                        
                    { data: 'CreatedDate' },
                    ...
                ],
                columnDefs: [
                    {
                        targets: [0, 3, 4],
                        orderable: false
                    },
                    {
                        render: function (data, type, row) {
                            return '<a href="@Url.Action("Details", "Index")/' + data + '"><i class="glyphicon glyphicon-folder-open"></i></a>'
                        },
                        title: '',
                        targets: 0
                    }
                ]
            });
        }

问题是: 我在这里想要成功实现服务器端分页,我该如何识别视图中的点击数字并将其作为MyTable参数的一部分接收?

1 个答案:

答案 0 :(得分:1)

我刚修好!! recordFiltered和recordsTotal应该具有相同的长度。

我在服务器端做的事情:

return Json(new
        {
            draw = param.draw,
            recordsTotal = allData.Count(),
            recordsFiltered = filteredData.Count(),
            data = result
        }, JsonRequestBehavior.AllowGet);

现在我做了:

return Json(new
        {
            draw = param.draw,
            recordsTotal = allData.Count(),
            recordsFiltered = allData.Count(),
            data = result
        }, JsonRequestBehavior.AllowGet);