扩展行显示细节


数据网格(datagrid)可以改变它的视图(view)来显示不同的效果。使用详细视图,数据网格(datagrid)可以在数据行的左边显示展开按钮("+" 或者 "-")。用户可以展开行来显示附加的详细信息。

步骤 1:创建数据网格(DataGrid)

  1. <table id="dg" style="width:500px;height:250px"
  2. url="datagrid8_getdata.php"
  3. pagination="true" sortName="itemid" sortOrder="desc"
  4. title="DataGrid - Expand Row"
  5. singleSelect="true" fitColumns="true">
  6. <thead>
  7. <tr>
  8. <th field="itemid" width="60">Item ID</th>
  9. <th field="productid" width="80">Product ID</th>
  10. <th field="listprice" align="right" width="70">List Price</th>
  11. <th field="unitcost" align="right" width="70">Unit Cost</th>
  12. <th field="status" width="50" align="center">Status</th>
  13. </tr>
  14. </thead>
  15. </table>

步骤 2:为数据网格(DataGrid)设置详细视图

为了使用详细视图,请记得在页面头部引用视图脚本文件。

  1. <script type="text/javascript" src="http://www.shouce.ren/try/jeasyui/datagrid-detailview.js"></script>
  1. $('#dg').datagrid({
  2. view: detailview,
  3. detailFormatter:function(index,row){
  4. return '<div class="ddv" style="padding:5px 0"></div>';
  5. },
  6. onExpandRow: function(index,row){
  7. var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
  8. ddv.panel({
  9. border:false,
  10. cache:false,
  11. href:'datagrid21_getdetail.php?itemid='+row.itemid,
  12. onLoad:function(){
  13. $('#dg').datagrid('fixDetailRowHeight',index);
  14. }
  15. });
  16. $('#dg').datagrid('fixDetailRowHeight',index);
  17. }
  18. });

我们定义 'detailFormatter' 函数,告诉数据网格(datagrid)如何渲染详细视图。 在这种情况下,我们返回一个简单的 '<div>' 元素,它将充当详细内容的容器。 请注意,详细信息为空。当用户点击展开按钮('+')时,onExpandRow 事件将被触发。 所以我们可以写一些代码来加载 ajax 详细内容。 最后我们调用 'fixDetailRowHeight' 方法来固定当详细内容加载时的行高度。

步骤 3:服务器端代码

datagrid21_getdetail.php
  1. &lt;?php
  2. include_once 'conn.php';
  3.  
  4. $itemid = mysql_real_escape_string($_REQUEST['itemid']);
  5.  
  6. $rs = mysql_query("select * from item where itemid='$itemid'");
  7. $item = mysql_fetch_array($rs);
  8. ?&gt;
  9.  
  10. <table class="dv-table" border="0" style="width:100%;">
  11. <tr>
  12. <td rowspan="3" style="width:60px">
  13. &lt;?php
  14. $aa = explode('-',$itemid);
  15. $serno = $aa[1];
  16. $img = "images/shirt$serno.gif";
  17. echo "<img src=\"$img\" style=\"width:60px;margin-right:20px\" />";
  18. ?&gt;
  19. </td>
  20. <td class="dv-label">Item ID: </td>
  21. <td>&lt;?php echo $item['itemid'];?&gt;</td>
  22. <td class="dv-label">Product ID:</td>
  23. <td>&lt;?php echo $item['productid'];?&gt;</td>
  24. </tr>
  25. <tr>
  26. <td class="dv-label">List Price: </td>
  27. <td>&lt;?php echo $item['listprice'];?&gt;</td>
  28. <td class="dv-label">Unit Cost:</td>
  29. <td>&lt;?php echo $item['unitcost'];?&gt;</td>
  30. </tr>
  31. <tr>
  32. <td class="dv-label">Attribute: </td>
  33. <td colspan="3">&lt;?php echo $item['attr1'];?&gt;</td>
  34. </tr>
  35. </table>

下载 jQuery EasyUI 实例

下载地址