Skip to content Skip to sidebar Skip to footer

Jquery Datatables Does Not Recognize Data Loaded

For some reason, my JQuery DataTable is adding data to the table, but it still says '0 to 0 of 0 entries. Trying to use the search box does not actually search through my data, and

Solution 1:

When using a server-side datasource, you need to handle the searching, sorting & paging in the server code.

Have a look at the parameters that get passed to & from the server here. You're not passing any of this information - for example you get Showing 0 of 0 entries because you're not returning iTotalRecords and iTotalDisplayRecords in the json data. The returned object should look something like this:

returnJson(new
{
    param.sEcho,
     iTotalRecords = rowCount,
     iTotalDisplayRecords = rowCount,
    aaData = result,
 }, JsonRequestBehavior.AllowGet)

If you look at Firebug's Net panel, you can see all the parameters that are sent in the request when loading the datatable. You need to get these in your server code and use them in your query, e.g sSearch for searching, iDisplayStart and iDisplayLength for paging.

I'd do something like this:

public JsonResult GetUserList(jQueryDataTableParamModel p)
{
    var addUserList = from u in db.t_user
       join m in db.t_user_membership on u.user_id equals m.user_id
       where m.membership_date > DateTime.Today
       selectnew mymodel
       { 
           user_id = u.user_id, 
           full_name = u.first_name + " " + u.last_name 
       };
//pagingvar displayedItems = addUserList.Skip(p.iDisplayStart).Take(p.iDisplayLength);
    var rowCount = addUserList.Count();

//  project into json for datatablevar result = from d in displayedItems
                         selectnewobject[]
                         {
                            d.user_id,
                            d.full_name
                          };
    return Json(new
    {
        param.sEcho,
        iTotalRecords = rowCount,
        iTotalDisplayRecords = rowCount,
        aaData = result,
     }, JsonRequestBehavior.AllowGet);
}

The jQueryDataTableParamModel parameter comes from a good tutorial on using server-side datatables in MVC here

Post a Comment for "Jquery Datatables Does Not Recognize Data Loaded"