jQuery EasyUI DataGrid - Creating a Subgrid
With the datagrid's detail view, users can expand a row to show additional detail information. Any content can be loaded as row details, and a subgrid can also be loaded dynamically. This tutorial will show you how to create a subgrid on the main grid.
Step 1: Create the main grid
Item ID
Product ID
List Price
Unit Cost
Attribute
Status
Step 2: Set the detail view to display the subgrid
To use the detail view, remember to reference the view script file in the head of the page.
$('#dg').datagrid({
view: detailview,
detailFormatter:function(index,row){
return '
';
},
onExpandRow: function(index,row){
var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
ddv.datagrid({
url:'datagrid22_getdetail.php?itemid='+row.itemid,
fitColumns:true,
singleSelect:true,
rownumbers:true,
loadMsg:'',
height:'auto',
columns:[[
{field:'orderid',title:'Order ID',width:100},
{field:'quantity',title:'Quantity',width:100},
{field:'unitprice',title:'Unit Price',width:100}
]],
onResize:function(){
$('#dg').datagrid('fixDetailRowHeight',index);
},
onLoadSuccess:function(){
setTimeout(function(){
$('#dg').datagrid('fixDetailRowHeight',index);
},0);
}
});
$('#dg').datagrid('fixDetailRowHeight',index);
}
});
When the user clicks the expand button ('+'), the 'onExpandRow' event is triggered. We create a new subgrid with three columns. When the subgrid data is loaded successfully or the size changes, remember to call the 'fixDetailRowHeight' method on the main grid.
Step 3: Server-side code
datagrid22_getdata.php
$result = array();
include 'conn.php';
$rs = mysql_query("select * from item where itemid in (select itemid from lineitem)");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
echo json_encode($items);
datagrid22_getdetail.php
include 'conn.php';
$itemid = mysql_real_escape_string($_REQUEST['itemid']);
$rs = mysql_query("select * from lineitem where itemid='$itemid'");
$items = array();
while($row = mysql_fetch_object($rs)){
array_push($items, $row);
}
echo json_encode($items);