user1765862
user1765862

Reputation: 14145

implement server side pagination on datatable

inside mvc controller I'm receiving MyTable as parameter

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

Inside view I have js code which initialize jquery data table

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

Question is: What I'm missing here to successfully implement server side pagination, what should I do to recognize clicked number inside view and receive that as part of MyTable parameter?

Upvotes: 0

Views: 638

Answers (1)

ar m
ar m

Reputation: 26

i just fixed it!! recordFiltered and recordsTotal should be of same length.

i was doing at serverside :

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

now i did:

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

Upvotes: 1

Related Questions