radbyx
radbyx

Reputation: 9650

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

Upvotes: 5

Views: 29918

Answers (5)

Hamza Khanzada
Hamza Khanzada

Reputation: 1529

If the search[value] parameter is always null then make sure you are using POST Ajax call on the client side and your action method of the controller is marked as [HttpPost].

Clientside:

$('#example').dataTable({
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": url,
        "type": "POST"
    },
  //Other configurations
});

Server:

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

}

Upvotes: 2

Gobind Gupta
Gobind Gupta

Reputation: 223

if you want to get value of serach box at server-side:

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

This will give you the value of search box.

Upvotes: 3

radbyx
radbyx

Reputation: 9650

I use this, because it's never null but an empty string.

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

Upvotes: 6

ravichandra vydhya
ravichandra vydhya

Reputation: 969

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

Upvotes: 6

dingensundso
dingensundso

Reputation: 186

As written in the docs datatables passes a search array with two values. search[value] is the search string which you'll need for filtering and search[regex] is just a boolean, expressing whether the search[value] should be interpreted as regex or just string.

If you're searching for values in a specific column the search string will be in columns[i][search][value]

Upvotes: 1

Related Questions