[转载]Add search functionality in DataGrid - jQuery EasyUI

[转载]Add search functionality in DataGrid – jQuery EasyUI.

In this tutorial we will show you how to get data from database and display them to datagrid. And then demonstrate how to search through the results according the search terms the user enters.

View Demo

Create DataGrid

Create the datagrid with paging feature and then add a toolbar to it.

  1. <table id=“tt” class=“easyui-datagrid” style=“width:600px;height:250px”  
  2.         url=“datagrid24_getdata.php” toolbar=“#tb”  
  3.         title=“Load Data” iconCls=“icon-save”  
  4.         rownumbers=“true” pagination=“true”>  
  5.     <thead>  
  6.         <tr>  
  7.             <th field=“itemid” width=“80”>Item ID</th>  
  8.             <th field=“productid” width=“80”>Product ID</th>  
  9.             <th field=“listprice” width=“80” align=“right”>List Price</th>  
  10.             <th field=“unitcost” width=“80” align=“right”>Unit Cost</th>  
  11.             <th field=“attr1” width=“150”>Attribute</th>  
  12.             <th field=“status” width=“60” align=“center”>Stauts</th>  
  13.         </tr>  
  14.     </thead>  
  15. </table>  

The toolbar is defined as:

  1. <div id=“tb” style=“padding:3px”>  
  2.     <span>Item ID:</span>  
  3.     <input id=“itemid” style=“line-height:26px;border:1px solid #ccc”>  
  4.     <span>Product ID:</span>  
  5.     <input id=“productid” style=“line-height:26px;border:1px solid #ccc”>  
  6.     <a href=“#” class=“easyui-linkbutton” plain=“true” onclick=“doSearch()”>Search</a>  
  7. </div>  

When user enters search values and press search button, the ‘doSearch’ function will be called:

  1. function doSearch(){  
  2.     $(‘#tt’).datagrid(‘load’,{  
  3.         itemid: $(‘#itemid’).val(),  
  4.         productid: $(‘#productid’).val()  
  5.     });  
  6. }  

The code above we call ‘load’ method to load new datagrid data. We need to pass ‘itemid’ and ‘productid’ parameters to server.

The Server Code

  1. include ‘conn.php’;  
  2.   
  3. $page = isset($_POST[‘page’]) ? intval($_POST[‘page’]) : 1;  
  4. $rows = isset($_POST[‘rows’]) ? intval($_POST[‘rows’]) : 10;  
  5. $itemid = isset($_POST[‘itemid’]) ? mySQL_real_escape_string($_POST[‘itemid’]) : ;  
  6. $productid = isset($_POST[‘productid’]) ? mySQL_real_escape_string($_POST[‘productid’]) : ;  
  7.   
  8. $offset = ($page-1)*$rows;  
  9.   
  10. $result = array();  
  11.   
  12. $where = “itemid like ‘$itemid%’ and productid like ‘$productid%'”;  
  13. $rs = mysql_query(“select count(*) from item where “ . $where);  
  14. $row = mysql_fetch_row($rs);  
  15. $result[“total”] = $row[0];  
  16.   
  17. $rs = mysql_query(“select * from item where “ . $where . ” limit $offset,$rows”);  
  18.   
  19. $items = array();  
  20. while($row = mysql_fetch_object($rs)){  
  21.     array_push($items, $row);  
  22. }  
  23. $result[“rows”] = $items;  
  24.   
  25. echo json_encode($result);  

Download the EasyUI example:

easyui-datagrid-demo.zip
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏