在Asp.net mvc中的jqGrid中添加自己的搜索参数

时间:2022-11-30 15:36:43

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

我刚刚开始研究asp.net mvc和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.

我有一个日历,它返回一个日期,一个多选列表框,并在网格外应用过滤器按钮。有没有办法根据所选日期和多个选定值将这些过滤器值传递给服务器端actionresult GridData(),并在分页或排序时保持这种值。

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

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

Thanks!

2 个解决方案

#1


5  

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.

是的,您可以使用postData属性为每个请求发送其他过滤器参数。请注意,这仅适用于使用JSON填充网格的情况。只需要一个返回JsonResult的动作。

In your jqgrid config include:

在你的jqgrid配置包括:

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:

对于apply filter按钮,调用$('#GridName')。trigger('reloadGrid')。或者,我喜欢在过滤器更改时重新加载网格。你可以用jquery做到这一点:

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

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

您的JSON应包含jqgrid的这些属性以理解它:

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

#2


1  

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.

首先,关于参数postData(请参阅http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options),您可以向服务器发送其他信息。在是否应该将jqGrid的使用addJSONData替换为setGridParam()和trigger('reloadGrid')的用法?您可能还可以找到一些信息,这些信息可以帮助您在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.

在我看来,可能不是jqGrid之外的自定义过滤,标准的数据过滤(搜索)可以帮助您。我在一些网页上使用自定义过滤的混合,并在几乎任何地方使用“高级搜索”(请参阅​​http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching)。 “高级搜索”是一种为您提供在不同条件下同时搜索多个字段的界面的方法。

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

你应该更新你的功能原型通讯员。来自过滤器的信息是JSON打包对象

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:

要分析来自过滤器的信息,我个人使用DataContractJsonSerializer。代码片段是:

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

#1


5  

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.

是的,您可以使用postData属性为每个请求发送其他过滤器参数。请注意,这仅适用于使用JSON填充网格的情况。只需要一个返回JsonResult的动作。

In your jqgrid config include:

在你的jqgrid配置包括:

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:

对于apply filter按钮,调用$('#GridName')。trigger('reloadGrid')。或者,我喜欢在过滤器更改时重新加载网格。你可以用jquery做到这一点:

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

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

您的JSON应包含jqgrid的这些属性以理解它:

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

#2


1  

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.

首先,关于参数postData(请参阅http://www.trirand.com/jqgridwiki/doku.php?id=wiki:options),您可以向服务器发送其他信息。在是否应该将jqGrid的使用addJSONData替换为setGridParam()和trigger('reloadGrid')的用法?您可能还可以找到一些信息,这些信息可以帮助您在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.

在我看来,可能不是jqGrid之外的自定义过滤,标准的数据过滤(搜索)可以帮助您。我在一些网页上使用自定义过滤的混合,并在几乎任何地方使用“高级搜索”(请参阅​​http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching)。 “高级搜索”是一种为您提供在不同条件下同时搜索多个字段的界面的方法。

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

你应该更新你的功能原型通讯员。来自过滤器的信息是JSON打包对象

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:

要分析来自过滤器的信息,我个人使用DataContractJsonSerializer。代码片段是:

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