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;
}

      

+3


source to share


4 answers


I use this because it never has a null but empty string.



Request.Form.GetValues("search[value]")[0]

      

+4


source


You shouldn't use search as a parameter. But it is automatically 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

+6


source


if you want to get the serach box server side value:

string search = Request.Form.GetValues ​​("search [value]"). FirstOrDefault ();

This will give you the value of the search box.

+2


source


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]

+1


source







All Articles