ADD Search functionality in DataGrid
In this tutorial we'll show you how to get the data from the database and display them to the 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. <table id= "tt" class= "Easyui-datagrid" style= "width:600px;height:250px" url= "datagrid24_getdata.php" toolbar= "#tb" title= "Load data" iconcls= "Icon-save" rownumbers= "true" pagination= "true" > <thead> <tr> <th field= "Itemid" width= ">Item ID</th> " <th field= "ProductID" width= ">Product ID</th> <th field= "ListPrice" width= " align=" "right" >List Price</th> <th field= "UnitCost" Width= " align=" "right" >Unit Cost</th> <th field= "attr1" width= ">Attribute</th> " <th field= "status" width= align= "center" >Stauts</th> </tr> </thead> </table>
The toolbar is defined as: <div id= "TB" style= "padding:3px" > <span>Item ID:</span> <input id= "Itemid" style= "line-height:26px;border:1px solid #ccc" > <span> product id:</span> <input id= "ProductID" style= " line-height:26px;border:1px solid #ccc "> <a href=" # " class= "Easyui-linkbutton" plain= "true" onclick= "Dosearch ()" >Search</a> </div>
When user enters search values and press search button, the ' Dosearch ' function would be called:function Dosearch () { $ (' #tt '). DataGrid (' Load ', {itemid: $ (' #itemid '). Val (), ProductID: $ (' #productid '). Val ()}); }
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 include ' conn.php '; $page = isset ($_post[' page ') ? intval ($_ post[' page ']) : 1; $rows = isset ($_post[' rows ') ? intval ($_post[') Rows ']) : 10; $itemid = isset ($_post[' itemid ') ? mysql_real_escape_ String ($_post[' itemid ')) : '; $productid = isset ($_post[' ProductID ']) ? mysql_real_escape_string ($_post[' ProductID ') : '; $offset = ($page-1) * $rows; $result = array (); $where = "itemid like ' $itemid% ' and productid like ' $productid% '"; $rs = mysql_query ("Select count (*) from item where " . $where); $row = mysql_fetch_row ($rs); $result ["Total"] = $row [0];