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

jQuery EasyUI DataGrid - Setting Sorting

jQuery EasyUI DataGrid - Setting Sorting

This example demonstrates how to sort a DataGrid by clicking on a column header.

All columns of the DataGrid can be sorted by clicking on the column header. You can define which columns are sortable. By default, columns are not sortable unless you set the sortable property to true.

Create the DataGrid



    

        

            

                

                

                

                

                

                

            

        

    
Item IDProduct IDList PriceUnit CostAttributeStauts

We define some sortable columns, including itemid, productid, listprice, unitcost, and so on. The 'attr1' column and the 'status' column are not sortable.

When sorting, the DataGrid will send two parameters to the remote server:

Server-side code



    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;

    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;

    $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'itemid';

    $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';

    $offset = ($page-1)*$rows;

    

    $result = array();

    

    include 'conn.php';

    

    $rs = mysql_query("select count(*) from item");

    $row = mysql_fetch_row($rs);

    $result["total"] = $row[0];

    

    $rs = mysql_query("select * from item order by $sort $order 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-datagrid8.zip