yakya
yakya

Reputation: 5210

JQuery Datatables serverside proccessing table doesn't refresh the content

When I open the page first time, it shows the results fine but when I use filtering or ordering a column etc. ajax method successfully returns the correct elements and json format looks correct but grid doesn't update the content. I initialized datatable in the following way:

$('#myDataTable').dataTable({
    "sAjaxSource": '@Url.Action("_Index")',
    "bServerSide": true,
    "aoColumns": [
                    { "sName": "Konu" },
                    { "sName": "Şikayet" },
                    { "sName": "Kullanıcı Adı" },
                    { "sName": "Müşteri Adı" },
                    { "sName": "Müşteri Soyadı" },
                    { "sName": "Şirket Adı" },
                    { "sName": "Cevaplandı" },
                    {
                        "sName": "Cevap Ver",
                        "bSearchable": false,
                        "bSortable": false,
                        "fnRender": function (oObj) {
                            return '<a href='+'@Url.Action("Edit")' + '/' +  
                                oObj.aData[7] + '>Cevap Ver</a>';
                        }
                    }
                ]
});

And in serverside:

public ActionResult _Index(jQueryDataTableParamModel table)
{
    var feedbacksList = (List<FeedbackAjaxVM>)AppService.QueryInfo("Admin", "GetFeedbackAjaxList", table);

    IEnumerable<string[]> feedbacks = from c in feedbacksList
                 select new string[] { 
                     c.Subject, 
                     c.Text, 
                     c.Username, 
                     c.CustomerName, 
                     c.CustomerSurname, 
                     c.CustomerFirmname, 
                     c.Response, 
                     c.Id.ToString() 
                 };

    return Json(new
   {
        sEcho = "1",
        iTotalRecords = feedbacksList.FirstOrDefault() != null ? feedbacksList.FirstOrDefault().TotalRecords : 0,
        iTotalDisplayRecords = table.iDisplayLength,
        aaData = feedbacks
    },JsonRequestBehavior.AllowGet);
}

I think my problem is about clientside code but couldn't figured that out.

Upvotes: 2

Views: 558

Answers (1)

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

sEcho is crucial to Datatables for rendering the data. The calls are asynchronous so Datatables needs to know the order of requests, this order is preserved by incrementing the sEcho variable in each server call.

However, you are returning 1 as sEcho all the time. According to Datatable, data with sEcho 1 is allready rendered so it is not rendering your data. You should return the sEcho value from jQueryDataTableParamModel. Simply, replace

sEcho = "1"  

with

sEcho = table.sEcho

Upvotes: 2

Related Questions