Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery EasyUI DataGrid - Adding Search Functionality

jQuery EasyUI DataGrid - Adding Search Functionality

This example demonstrates how to get data from a database and display it in a datagrid. It then shows how to search the displayed results based on keywords entered by the user.

Create the DataGrid

Create a datagrid with pagination, then add a toolbar to it.



    

        

            

                

                

                

                

                

                

            

        

    
Item IDProduct IDList PriceUnit CostAttributeStauts

The toolbar is defined as follows:



    
        Item ID:                  Product ID:                  Search     

When the user enters a search value and presses the search button, the 'doSearch' function is called:



    function doSearch(){

        $('#tt').datagrid('load',{

            itemid: $('#itemid').val(),

            productid: $('#productid').val()

        });

    }

The above code calls the 'load' method to load new datagrid data. We need to pass the 'itemid' and 'productid' parameters to the server.

Server-side 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];

    

    $rs = mysql_query("select * from item where " . $where . " limit $offset,$rows");

    

    $items = array();

    while($row = mysql_fetch_object($rs)){

        array_push($items, $row);

    }

    $result["rows"] = $items;

    

    echo json_encode($result);

Download the jQuery EasyUI example

jeasyui-datagrid-datagrid24.zip