How can I use datatables lookup with server side processing?
When working with handling datatables server-side
. How is the search value passed to the server? I have looked at doc .
The datatable is automatically sent to the server draw
, start
and length
. Can and should you do something with the help search
? It is mentioned in the documentation search[value]
, but I don't know how to interpret it.
CUSTOMER
$(document).ready(function () {
var url = '@Url.Action("GetJsonData", "Home")';
$('#example').dataTable({
'searching': true,
"paging": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": url,
"type": "GET"
},
"columns": [
{ "data": "id" },
{ "data": "name" }
]
});
});
SERVER
public JsonResult GetJsonData(string draw, int start, int length, string search)
{
var hugeDataArr = new object[100];
var returnDataArr = new object[length];
for (int i = 0; i < hugeDataArr.Length; i++)
{
hugeDataArr[i] = new
{
DT_RowId = i,
id = "id" + i.ToString().PadLeft(2, '0'),
name = "nameæøΓ₯" + i.ToString().PadLeft(2, '0')
};
}
for (int i = 0; i < length; i++)
{
returnDataArr[i] = hugeDataArr[start + i];
}
JsonResult json = Json(new
{
draw = Convert.ToInt32(draw),
recordsTotal = 100, // calculated field
recordsFiltered = 50, // calculated field
data = returnDataArr
}, JsonRequestBehavior.AllowGet);
return json;
}
source to share
As stated in the docs, datatables passes in a search array with two values. search [value] is the search string you will need to filter and search [regex] - it is just a boolean expression that expresses whether search [value] should be interpreted as a regular expression or just a string.
If you are looking for values ββin a specific column, the search string will be in columns [i] [search] [value]
source to share