So I have implemented Server-Side processing in MVC for jQuery Datatables, I am Showing 1 to 10 of 8,037 entries (with pagination). However when I navigate to the page with the table the first load takes far to long (5 to 10 seconds). In my controller I set it to only take 10 records (iDisplayLength):
var displayedProviders = filteredProviders
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);
So when I first navigate to the page with the table or I use pagination it should only load 10 records at a time.
Can anyone guide me to why the first load is taking such a long time? My fear is that its loading the entire table into the Json Request which is slowing it down at first. This wasnt what I had in mind when implementing server side processing. Is there a way around this? If I implement:
var model = _db.CareProviders
.OrderBy(row => row.ProviderId).Skip((pageNumber - 1) * pageResults)
.Take(pageResults).ToList();
This will only show 10 results and no more?
Model:
public class jQueryDataTableParamModel
{
/// <summary>
/// Request sequence number sent by DataTable,
/// same value must be returned in response
/// </summary>
public string sEcho { get; set; }
/// <summary>
/// Text used for filtering
/// </summary>
public string sSearch { get; set; }
/// <summary>
/// Number of records that should be shown in table
/// </summary>
public int iDisplayLength { get; set; }
/// <summary>
/// First record that should be shown(used for paging)
/// </summary>
public int iDisplayStart { get; set; }
/// <summary>
/// Number of columns in table
/// </summary>
public int iColumns { get; set; }
/// <summary>
/// Number of columns that are used in sorting
/// </summary>
public int iSortingCols { get; set; }
/// <summary>
/// Comma separated list of column names
/// </summary>
public string sColumns { get; set; }
}
}
Controller:
public ActionResult AjaxHandler(jQueryDataTableParamModel param)
{
var allProviders = _db.CareProviders.ToList();
IEnumerable<CareProvider> filteredProviders;
if (!string.IsNullOrEmpty(param.sSearch))
{
filteredProviders = _db.CareProviders.ToList()
.Where(c => c.ProviderId.Contains(param.sSearch)
||
c.CareServiceType.Contains(param.sSearch)
||
c.CareServiceName.Contains(param.sSearch));
}
else
{
filteredProviders = allProviders;
}
var displayedProviders = filteredProviders
.Skip(param.iDisplayStart)
.Take(param.iDisplayLength);
var result = from c in displayedProviders
select new[] { Convert.ToString(c.ProviderId), c.CareServiceType, c.CareServiceName, c.Email };
return Json(new
{
sEcho = param.sEcho,
iTotalRecords = allProviders.Count(),
iTotalDisplayRecords = filteredProviders.Count(),
aaData = result
},
JsonRequestBehavior.AllowGet);
}
Jquery DataTable Script:
<script type="text/javascript">
$(function () {
// Initialize Example 2
// $('#example2').dataTable();
$('#example2').dataTable({
"bServerSide": true,
"sAjaxSource": "AdminPanel/AjaxHandler",
"bProcessing": true,
"aoColumns": [
{ "sName": "ProviderId" },
{ "sName": "CareServiceType" },
{ "sName": "CareServiceName" },
{ "sName": "Email" }
]
});
});
</script>