Subgrid, that is, a new layer of DataGrid in the DataGrid, is suitable for displaying data with layers. For example, to display a batch of orders, the Order contains specific goods. Subgrid can be used for such a requirement.
We can first look at the official website subgrid to enhance our visual experience.
How to implement it? It is still simple to show the data.
The amount of DEMO code is just dozens of lines, which is easy to understand.
$(function(){ $('#dg').datagrid({ view: detailview, detailFormatter:function(index,row){ return '<div style="padding:2px"><table class="ddv"></table></div>'; }, 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:200}, {field:'quantity',title:'Quantity',width:100,align:'right'}, {field:'unitprice',title:'Unit Price',width:100,align:'right'} ]], onResize:function(){ $('#dg').datagrid('fixDetailRowHeight',index); }, onLoadSuccess:function(){ setTimeout(function(){ $('#dg').datagrid('fixDetailRowHeight',index); },0); } }); $('#dg').datagrid('fixDetailRowHeight',index); } }); });
1. To use this subgrid view, you need to use the datagrid-detailview.js provided on the official website, which helps us define the display view of detailview.
During use, we only need to replace the default view with detailview in the main DataGrid.
2. detailformatter, that is, the initial code in the generated detail. Put a table here and then dynamically create it as a DataGrid.
3. When the primary DataGrid Initialization is complete, the subgrid is not displayed. At this time, the subgrid does not exist and we will not create it.
When will it be created? When we expand a piece of data in the primary DataGrid, that is, in the onexpandrow event, we create a subgrid under its "jurisdiction.
4. The method for creating a subgrid is similar to that for creating a DataGrid, but some fixdetailrowheight operations are added later.
Fixdetailrowheight this method is mainly used to adjust the height of the subgrid. If it is not added, the height of the subgrid may be misplaced.
But here the setTimeout time is 0. What is the difference between it and the direct function call?
DataGrid advanced -- subgrid