NoBullMan
NoBullMan

Reputation: 2188

Get count of specific key from request query string

I am working on my first server side jQuery datatable code and need some help. It does not seem datatable request string contains any info about count of columns, but does have individual column info, such as columns[i][data], columns[i][name], etc.

On server side I need to handle column filtering and the only way I can think of is iterating through column and see which one has non-empty columns[i][search][value].

I checked the query string sent by datatable to server side API (I am using WebMethod), and it looks like this:

http://localhost:57714/WebService/ABC.asmx/GetSitesData?
draw=1&
columns[0][data]=ID&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=&columns[0][search][regex]=false&
columns[1][data]=Zip&columns[1][name]=&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&
columns[2][data]=SiteName&columns[2][name]=&columns[2][searchable]=true&columns[2][orderable]=true&columns[2][search][value]=&columns[2][search][regex]=false&
columns[3][data]=Address&columns[3][name]=&columns[3][searchable]=true&columns[3]
...
columns[10][data]=PlusFour&columns[10][name]=&columns[10][searchable]=true&columns[10][orderable]=true&columns[10][search][value]=&columns[10][search][regex]=false&
order[0][column]=2&
order[0][dir]=asc&
order[1][column]=7&
order[1][dir]=desc&
start=0&
length=25&
search[value]=&
search[regex]=false

Is there a way from above request query string I can get the count of "columns[?][search][value]" or "[search][value]"? In this case count of 11.

Right now, I am doing it like this to get the column index and search text for whatever column user is trying to filter by, but not sure how correct or efficient it is:

System.Collections.Specialized.NameValueCollection ReqParams = HttpContext.Current.Request.Params;
...
...
string sColSrchTxt = string.Empty;
int i = 0;

while (null != ReqParams["columns[" + i + "]"])
{
    if (string.Empty != ReqParams["columns[" + i + "][search][value]"])
    {
        sColSrchTxt = ReqParams["columns[" + i + "][search][value]"];
        i++;
        break;
    }
}
int iColSrchIndx = i - 1;

Upvotes: 0

Views: 46

Answers (0)

Related Questions