Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use search on datatables with server-side processing?

When working with datatables server-side proccessing. How does the search value being passed to the server? I've looked in the doc.

The datatable sends automatically the draw, start and the length to the server. Can and should I do something simular with the search? The documentation mention search[value] but I don't know how to interpretive it.

CLIENT

$(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;
}
like image 438
radbyx Avatar asked Jan 13 '15 10:01

radbyx


1 Answers

You are not supposed to use search as parameter.But it automatically is part of your query string.

public JsonResult GetJsonData(string draw, int start, int length)
{
     string search = Request.QueryString["search[value]"];
     // your code for search filtering

}

thanks ravi

like image 188
ravichandra vydhya Avatar answered Nov 03 '22 00:11

ravichandra vydhya