我想使用外部过滤器来过滤jqgrid数据。在我的情况下,使用内置的过滤器或搜索框会有点奇怪。我有应用程序,其中我将有一个将接受输入的表单,并将相应地过滤jgrid数据。我已经实现了代码,它成功地为我提供了过滤数据,但没有在jqgrid中。我们在定义jqgrid时指定URL,这里我有另一个用于提交过滤器表单的URL。这里,用于过滤数据和在jqgrid中显示数据的URL是不同的。那么,如何在jqgrid中显示过滤后的数据。我在java Hibernate中编码。
这是我的过滤器对话框代码: {
<form name="t_filter_form" id="t_filter_form">
<table cellspacing="15">
<tr>
<td width="50%"><label><input type="radio" value="Current_Month" id="t_filter_current_Month" checked="checked" name="filter_type" />Current Month</label></td>
</tr>
<tr>
<td width="50%"><label><input type="radio" value="Yearly" id="t_filter_yearly" name="filter_type" />Yearly</label></td>
</tr>
<tr>
<td align="right"><label>Enter Year:</label></td>
<td><input type="text" name="year" id="t_filter_year"/></td>
</tr>
<tr>
<td width="50%"><label><input type="radio" value="Monthly" id="t_filter_monthly" name="filter_type" />Monthly</label></td>
</tr>
<tr>
<td align="right" ><label>Select Month:</label></td>
<td><select name="month" id="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select></td>
</tr>
<tr>
<td width="50%"><label><input type="radio" value="Range" id="t_filter_range" name="filter_type" />Range</label></td>
</tr>
<tr><td align="right">
<label for="from">From</label></td>
<td><input type="text" id="from" name="from"/></td>
</tr>
<tr>
<td align="right"><label for="to">To</label></td>
<td><input type="text" id="to" name="to"/></td>
</tr>
<tr>
<td width="50%"><label><input type="radio" value="category" id="t_filter_category" name="filter_type" />Category</label></td>
</tr>
<tr>
<td align="right" ><label>Enter Category:</label></td><td><input type="text" name="t_filter_category" id="t_filter_category_txt"/></td>
</tr>
<tr><td> </td>
<td><input type="button" name="filter_transaction" id="filter_transaction" value="Filter transaction" onclick="filter();" />
<input type="button" onclick="return close_dialog();" id="cancel_filter" value="Cancel" /></td>
</tr>
</table> </form>
</div>
现在,当用户点击按钮&#39;过滤交易&#39;我希望网格重新加载。如果用户选择每月过滤网格,那么它将以月为输入并相应地重新加载网格
这是我的servlet代码:
if(request.getParameter("action").equals("filter")){
if(login!=null)
{
String query=null;
if("Current_Month".equals(request.getParameter("filter_type")))
{
System.out.println("Current month");
int month=cal.get(Calendar.MONTH)+1;
int year=cal.get(Calendar.YEAR);
System.out.println("Current month is: " +month);
query = "from TransactionDetails where register_id="+hb_id+" and transaction_date>='"+year+"-"+month+"-1' and transaction_date<='"+year+"-"+month+"-31'" ;
System.out.println("Current month query is: " +query);
}
if("Yearly".equals(request.getParameter("filter_type")))
{
System.out.println("Yearly");
int year=Integer.parseInt(request.getParameter("year"));
System.out.println("Entered year is: " +year);
query = "from TransactionDetails where register_id="+hb_id+" and transaction_date>='"+year+"-1-1' and transaction_date<='"+year+"-12-31'" ;
System.out.println("Yearly query is: " +query);
}
if("Monthly".equals(request.getParameter("filter_type")))
{
System.out.println("Monthly");
int month=Integer.parseInt(request.getParameter("month"));
System.out.println("Entered month is: " +month);
int year=cal.get(Calendar.YEAR);
query = "from TransactionDetails where register_id="+hb_id+" and transaction_date>='"+year+"-"+month+"-1' and transaction_date<='"+year+"-"+month+"-31'" ;
System.out.println("Current month query is: " +query);
}
if("Range".equals(request.getParameter("filter_type")))
{
System.out.println("Range");
String from_date=request.getParameter("from");
String[] date = from_date.split("/");
from_date = date[2]+"-"+date[1]+"-"+date[0];
String to_date=request.getParameter("to");
date = to_date.split("/");
to_date = date[2]+"-"+date[1]+"-"+date[0];
System.out.println("Entered range is from " +from_date+" to "+to_date);
query = "from TransactionDetails where register_id="+hb_id+" and transaction_date>='"+from_date+"' and transaction_date<='"+to_date+"'" ;
System.out.println("Current month query is: " +query);
}
if("category".equals(request.getParameter("filter_type")))
{
System.out.println("Category");
String category =request.getParameter("t_filter_category");
System.out.println("Entered category is: "+category);
query = "from TransactionDetails where register_id="+hb_id+" and category='"+category+"'" ;
System.out.println("Current month query is: " +query);
}
我通过数据库获得正确的输出..但我不知道如何绑定这些数据或者说我的jgrid中获取此数据 这是我的网格
function fillGridOnEvent(){
$("#Transaction_grid").html("<table id=\"transaction_list\"></table><div id=\"page\"></div>");
jQuery("#transaction_list").jqGrid({
url:'http://localhost:8084/HomeBudget/TransactionController?action=show&rid=<%=hb_id%>',
datatype: "xml",
height: 300,
colNames:['ID','Date','Type','Category','Amount','Comments'],
colModel:[
{name:'transaction_id',index:'transaction_id', width:20,sortable:false},
{name:'date',index:'date', width:100,sortable:false},
{name:'type',index:'type', width:100,sortable:false},
{name:'category',index:'category', width:150,sortable:false},
{name:'amount',index:'amount', width:100,sortable:false, formatter: 'number'},
{name:'comments',index:'comments', width:210,sortable:false}
],
paging: true,
rowNum:15,
rowList:[15,30,45],
pager: $("#page"),
loadonce:true,
multiselect: false,
gridview:true,
viewrecords:true,
caption: "Transaction"
}).navGrid('#page',{edit:false,add:false,del:false},{multipleSearch:true, multipleGroup:true, showQuery: true});
}
答案 0 :(得分:0)
我不确定我是否正确理解你。但我的解决方案是(我有多年使用jqGrid和Java和C#的经验)
设置新的jqGrid参数(从表单字段中读取参数)
$("#" + table).setGridParam({
postData:queryString
});
$(“#”+ table).jqGrid()。trigger(“reloadGrid”);