paresh
paresh

Reputation: 31

Add own search parameter in jqGrid in Asp.net mvc

I have just started working on asp.net mvc and jqgrid.

I have a calendar which returns a date, a multiselect list box and apply filter button outside of the grid. Is there a way to pass these filter values to the server-side actionresult GridData() based on the selected date and multiple selected values and also it persist while paging or sorting.

public ActionResult GridData(string sidx, string sord, int? page, int? rows, Collection categoryOptions,string fromDate) {..}

Thanks!

Upvotes: 3

Views: 6307

Answers (2)

Ryan
Ryan

Reputation: 4313

Yes you can use the postData property to send additional filter parameters with each request. Note this will only work if you're using JSON to populate your grid. Just have an action that returns JsonResult.

In your jqgrid config include:

postData: {
   startDate: function() { return $('#startDate').val(); },
   anotherFilter: function() { return $('#anotherFilter').val(); }
}

For your apply filter button call $('#GridName').trigger('reloadGrid'). Alternatively I like to reload the grid anytime a filter changes. You can do this with jquery:

$('#filterName').change(function(){$('#GridName').trigger('reloadGrid');})

Your JSON should contain these properties for jqgrid to understand it:

total = pagedList.PageCount,
page = pagedList.PageNumber,
records = pagedList.TotalItemCount,
rows = pagedList.ToArray()

Upvotes: 5

Oleg
Oleg

Reputation: 221997

First of all with respect of parameter postData (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options) you can send additional information to server. In Should one replace the usage addJSONData of jqGrid to the usage of setGridParam(), and trigger('reloadGrid')? you could probably find also some information, which can help you to make data refresh in jqGrid.

It seems to me, that probably instead of custom filtering outside of jqGrid a standard filtering (searching) of data can helps you. I use mix from custom filtering on some web pages and use the "advanced searching" (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching) almost anywhere. "Advanced searching" is a way give you interface to search for multiple fields at the same time with different conditions.

You url will be appended with:

?_search={_search}&page={page}&rows={rows}&sidx={sortIndex}&sord={sortDirection}&searchField={searchField}&searchString={searchString}&searchOper={searchOper}&filters={filters}

and you should update you function prototype correspondent. The information from filters is JSON packed object like

filters = 
    {"groupOp":"AND",
     "rules":[
       {"field":"invdate","op":"ge","data":"2007-10-06"},
       {"field":"invdate","op":"le","data":"2007-10-20"}, 
       {"field":"name","op":"bw","data":"Client 3"}
      ]
    }

To analyze information from filter I personally use DataContractJsonSerializer. The code fragment is:

MemoryStream ms = new MemoryStream (Encoding.Unicode.GetBytes (filters));
DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof(jqGridSearchFilter));
ms.Position = 0;

jqGridSearchFilter searchFilter = (jqGridSearchFilter)serializer.ReadObject (ms);
string groupOp = null;
if (!String.IsNullOrEmpty(searchFilter.groupOp) &&
    String.Compare (searchFilter.groupOp, "AND", StringComparison.Ordinal) == 0)
    groupOp = "AND";
else if (!String.IsNullOrEmpty(searchFilter.groupOp) &&
         String.Compare (searchFilter.groupOp, "OR", StringComparison.Ordinal) == 0)
    groupOp = "OR";
else {
    arSearchStringParameters = null;
    return null;
}
for (int i=0; i "WHERE ".Length)
        sb.Append (groupOp);
    AppendWhere (sb, _search,
        searchFilter.rules[i].field, searchFilter.rules[i].op, searchFilter.rules[i].data,
        arColumnInfos, parameters);
}

where

internal enum GroupOperation {
    AND,
    OR
}

[DataContract]
internal class jqGridSearchFilterItem {
    [DataMember]
    internal string field = null;
    [DataMember]
    internal string op = null;
    [DataMember]
    internal string data = null;
}

[DataContract]
internal class jqGridSearchFilter {
    [DataMember]
    internal string groupOp = null; //GroupOperation groupOp;

    [DataMember]
    internal List rules = null;
}

Upvotes: 1

Related Questions