我想使用外部过滤器来过滤 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>
现在,当用户单击“过滤事务”按钮时,我希望重新加载网格。在这里,如果用户选择每月过滤网格,那么它将以月份作为输入,并相应地重新加载网格
这是我的 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});
}