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 ID
Product ID
List Price
Unit Cost
Attribute
Stauts
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:
- sort: The field name of the column to sort.
- order: The sort order, which can be 'asc' or 'desc'; the default value is 'asc'.
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);