2012-11-06 37 views
0

我想使用外部過濾器來過濾jqgrid數據。在我的情況下,使用內置的過濾器或搜索框將不會有什麼奇怪的。我有應用程序,我將有一個表單,將採取輸入,並相應地過濾jgrid數據。我已經實現了代碼,使我成功過濾數據,但不是在jqgrid。我們在定義jqgrid的同時指定了URL,這裏我有另一個用於提交過濾器表單的URL。這裏,過濾數據和在jqgrid中顯示數據的URL是不同的。那麼,我怎樣才能在jqgrid中顯示過濾的數據。我在java Hibernate中編碼。jqgrid中的外部過濾器

這是我的過濾器對話框代碼: {

   <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>&nbsp;</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}); 

}

回答

0

我不是蘇我正確理解你。 Buth我的解決方法是(II有多年使用的jqGrid wizh Java和C#的經驗)/處理程序,對於jqGrid的數據返回JSON

  • 參數多態的servlet

    1. 定義的servlet(實現過濾器)
    2. 設置新的jqGrid PARAMS(PARAMS從表單字段讀取)

      $("#" + table).setGridParam({ 
          postData:queryString 
      }); 
      
    3. 刷新的jqGrid

    $(「#」+ table).jqGrid()。trigger(「reloadGrid」);