I cannot get serverSide to work, can anyone help!?
I cannot get serverSide to work, can anyone help!?
johnmorsley
Posts: 4Questions: 1Answers: 0
Hello guys,
I've been trying to get serverSide working all day and I just can't. Can anyone see what I'm doing wrong here?
<table class="table table-striped table-bordered" id="tblUsers">
<thead>
<tr class="text-muted">
<th><small>Unique Identifier</small></th>
</tr>
</thead>
</table>
$('#tblUsers').dataTable({
processing: true,
serverSide: true,
ajax: {
url: '@Url.Action(actionName: "GetPageOfUsers", controllerName: "User")'
},
columns: [
{ "data": "UniqueId" }
]
});
[HttpGet]
public ActionResult GetPageOfUsers(DataTableParameters parameters)
{
var areaId = GetAreaId();
if (!areaId.HasValue) return null;
var users = GetUsers(areaId: areaId.Value, skip: 0, take: 1);
return Json(
new
{
draw = parameters.Draw,
recordsTotal = GetUserCount(areaId.Value),
recordsFiltered = GetUserCount(areaId.Value, parameters.Search),
data = JsonConvert.SerializeObject(users, Formatting.None)
},
JsonRequestBehavior.AllowGet);
}
Thanks :)
This question has accepted answers - jump to:
This discussion has been closed.
Answers
What does the '@Url.Action(..' get expanded to ? Forgive my ignorance - I use grails and server side processing without any problems ..
What language is this? I recently got server-side working with ruby on rails and it was not easy. Took a couple days of playing around then refactoring everything so I didn't have a giant block of code in my controller.
Server-Side means that your server side script has to handle sorting, filtering, and pagination by itself. Datatables will send json data that needs to be parsed, then a query should happen based off that parsing and that data returned. This is only needed if you are dealing with HUGE amounts of data. Some of my tables have 30 thousand entries so this was a must for me.
@johnmorsley - What JSON is your script currently responding with?
Allan
Hi guys,
Thanks for your responses! :)
Rob
Resolves to
Which is correct. This part works as this corresponds to the GetPageOfUsers action on the User controller. I can verify this is working correctly and returns JSON.
matthttam
This is ASP.NET MVC. Yes, my results are potentially huge. This was for a User table with thousands of users. So server side is perfect!
allan
The JSON 'looks' OK, but maybe one of you can see an issue with it, so here it is...
This is a very cut down version with the server side code returning 1 row with 1 column!
Any further help is much appreciated...
That is the issue in the JSON. It should be:
i.e.
data
should be an array, not a string.Try just
data = users
in the output.Allan
Thanks Allan, I'll give that a try right now... :)
Tried that Allan and it worked! So can't thank you enough :)