2012-03-26 105 views
1

如何捕获在jqGrid工具栏搜索中搜索的字段的值?jqGrid过滤工具栏与Spring 3的集成MVC

我使用Spring 3 MVC作为我的后端工作。

目前我的方法的签名看起来像

public @ResponseBody PageResponse getEmpList(
    @RequestParam("page") int pageNo, @RequestParam("rows") int rowLimit, 
    @RequestParam("sidx") String sortCol, @RequestParam("sord") String sortDir) 

如何修改它来捕捉不同的搜索参数,即已经检索的字段(S)和相应的搜索字符串

我使用的jqGrid 4.3.1

回答

1

我正在提供我自己提出的问题的答案。

有两种方式来捕获滤波器参数

如果您设置stringResult选项,真正在

jQuery("#grid_id").jqGrid('filterToolbar', {stringResult: true}); 

滤波器参数得到提交为具有以下格式的JSON字符串

{"groupOp":"AND", // the group operation when you have multiple feilds to search on 
"rules":[ 
    {"field":"countrycode", // the field/columns being searched on 
     "op":"bw", // operator for searching, here bw mean beginning with 
     "data":"ind”}] // the search string for the above field 
} 

然后我们可以决定如何处理来自上述JSON字符串的过滤器参数。推荐的方法是使用可用的JSON库,可以将Java对象转换为JSON字符串,反之亦然。

在这种情况下,方法签名来处理,这将是为

@RequestMapping(value= "/cityData", method = RequestMethod.POST) 
    public @ResponseBody PageResponse getCityList(@RequestParam("page") int pageNo, 
                @RequestParam("rows") int rowLimit, 
               @RequestParam("sidx") String sortCol, 
               @RequestParam("sord") String sortDir, 
               @RequestParam("_search") boolean search, 

//captures the filter parameters as a json string          
    @RequestParam(value="filters",required=false) String filters) 

的_search参数被触发,通过过滤器工具栏上的真实值时,输入的搜索串

另一种方法处理这些过滤器是被搜索将此选项设置为false列/场stringResult选项为假设置为

jQuery("#grid_id").jqGrid('filterToolbar', {stringResult: false}); 

并且搜索字符串作为名称值对传递

因此,在这种情况下,我们正在搜索countryCode,我们正在搜索的字符串是ind。

所以函数签名会变成

@RequestMapping(value= "/cityData", method = RequestMethod.POST) 
    public @ResponseBody PageResponse getCityList(@RequestParam("page") int pageNo, 
                @RequestParam("rows") int rowLimit, 
               @RequestParam("sidx") String sortCol, 
               @RequestParam("sord") String sortDir, 
               @RequestParam("_search") boolean search, 

    //capturing the search string for the country code column           @RequestParam(value="countryCode",required=false) String countryCode,) 

希望这有助于有人!

0

我会建议你使用stringResult: true选项filterToolbar

jQuery("#grid_id").jqGrid('filterToolbar', {stringResult: true}); 

在向服务器的请求参数将与Advanced Searching对话框(使用multipleSearch: true选项进行搜索)相同的情况下。

我自己并没有使用Spring 3 MVC,但我想你需要将其他参数@RequestParam(value="filters", required=false) String filters添加到参数列表getEmpList。参数的值是JSON字符串,您应该另外反序列化(解析)为对象。 (有关更多信息,请参见here

+0

感谢您指出我一个非常好的资源。 – 2012-04-02 07:25:39

+0

@Anand:不客气!我自己并没有在春季开发,但我希望这个链接能帮助你一点。 – Oleg 2012-04-02 07:32:27