Andrus
Andrus

Reputation: 27931

How to pass complex search criteria to jqgrid from query string

I tried code below to pass filter to url which invokes jqgrid. jqGrid still shows all rows, passed filter is not passed to url to retrieve data form server. How to force jqGrid to filter by filter passed in query string ?

window.open( '/Grid?filters=' + encodeURIComponent(
  '{"groupOp":"AND","rules":[{"field":"Name","op":"cn","data":"John"}' )); 

Upvotes: 1

Views: 1896

Answers (1)

Oleg
Oleg

Reputation: 221997

You can parse window.location.href and get all parameters which you need. If the URL contains the parameter which you need you can decode it with respect of decodeURIComponent and use like you as need.

The following code can be used for tests. It demonstrate how to decode filters parameter.

if (window.location.href.indexOf('?') < 0) {
    // the code will open the current HTML page with additional
    // parameter "filters" and reopen the same page with the parameters
    window.location = window.location.href + '?' +
        $.param({
            filters: JSON.stringify({
                groupOp: "AND",
                rules: [
                    {field: "Name", op: "cn", data: "John Smith"}
                ]
            })
        });
} else {
    // decode URL parameters and place in the as properties in the
    // object "parameters":
    var namedParameters = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'),
        parameters = {},
        nameAndValue,
        i;
    for (i = 0; i < namedParameters.length; i += 1) {
        nameAndValue = namedParameters[i].split('=');
        parameters[nameAndValue[0]] = decodeURIComponent(nameAndValue[1]);
        if (nameAndValue[0] === "filters") {
            // display the data from the "filters" parameter
            var myFilters = $.parseJSON(decodeURIComponent(nameAndValue[1]));
            alert(myFilters.rules[0].data);
        }
    }
}

Upvotes: 2

Related Questions