ExtJS中实现嵌套表格

时间:2022-06-28 06:44:15

先看效果:

ExtJS中实现嵌套表格

代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>test</title>
  6. <script type="text/javascript">
  7. </script>
  8. <link rel="stylesheet" type="text/css" href="Lib/ExtJs/2_2/resources/css/ext-all.css" />
  9. <script type="text/javascript" src="Lib/ExtJs/2_2/adapter/ext/ext-base.js"></script>
  10. <script type="text/javascript" src="Lib/ExtJs/2_2/ext-all-debug.js"></script>
  11. <script type="text/javascript" src="Lib/ExtJs/2_2/source/locale/ext-lang-zh_CN.js"></script>
  12. <script type="text/javascript" src="Lib/ExtJs/plus/RowExpander.js"></script>
  13. <script type="text/javascript">
  14. Ext.onReady(function(){
  15. var testData=[
  16. ["lugreen","男",26,[["数学",100],["语文",150]]]
  17. ,["lisi","男",25,[["数学",100],["语文",150]]]
  18. ,["zhangsan","男",27,[["数学",120],["语文",158]]]
  19. ];
  20. //
  21. storeTest= new Ext.data.SimpleStore({
  22. fields: ["name","sex","age","grade"]
  23. ,data: testData
  24. });
  25. var expander = new Ext.grid.RowExpander({
  26. tpl : new Ext.XTemplate(
  27. '<div class="detailData">',
  28. '',
  29. '</div>'
  30. )
  31. });
  32. expander.on("expand",function(expander,r,body,rowIndex){
  33. //查找 grid
  34. window.testEle=body;
  35. //alert(body.id);
  36. if (Ext.DomQuery.select("div.x-panel-bwrap",body).length==0){
  37. //alert("a");
  38. var data=r.json[3];
  39. var store=new Ext.data.SimpleStore({
  40. fields: ["class","degrade"]
  41. ,data:data
  42. });
  43. var cm = new Ext.grid.ColumnModel([
  44. {header: "科目",dataIndex: 'class',width: 130,hideable:false,sortable:false,resizable:true}
  45. ,{header: "成绩",dataIndex: 'degrade',width: 130,hideable:false,sortable:false,resizable:true}
  46. ]);
  47. Ext.DomQuery.select("div.detailData")[0];
  48. var grid = new Ext.grid.GridPanel(
  49. {
  50. store:store,
  51. cm:cm,
  52. renderTo:Ext.DomQuery.select("div.detailData",body)[0],
  53. autoWidth:true,
  54. autoHeight:true
  55. }
  56. );
  57. }
  58. });
  59. //var sm=new Ext.grid.CheckboxSelectionModel({singleSelect:true});
  60. var cm = new Ext.grid.ColumnModel([
  61. expander
  62. ,{header: "姓名",dataIndex: 'name',width: 50,hideable:false,sortable:false}
  63. ,{header: "性别",dataIndex: 'sex',width: 130,hideable:false,sortable:false,resizable:true}
  64. ,{header: "年龄",dataIndex: 'age',width: 130,hideable:false,sortable:false,resizable:true}
  65. ]);
  66. var grid = new Ext.grid.GridPanel(
  67. {
  68. id:'testgrid',
  69. store:storeTest,
  70. cm:cm,
  71. renderTo:"grid1",
  72. width:780,
  73. autoHeight:false,
  74. height:300,
  75. listeners:{},
  76. plugins:[expander]
  77. }
  78. );
  79. });
  80. </script>
  81. <style type="text/css">
  82. #div2 h2 {
  83. font-weight:200;
  84. font-size:12px;
  85. }
  86. .c1 h2 {
  87. font-weight:200;
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <div id="grid1">
  93. </div>
  94. <div id="grid2">
  95. </div>
  96. </body>
  97. </html>

其中使用到的"RowExpander.js"为extjs官方示例中自带的。

实现这个嵌套表格要注意两点技巧:

1 提供给外层表格的dataStore的数据源以嵌套数组的形式表示细节区的数据,如下面的黑体所示。

  
  1. var testData=[
  2. ["lugreen","男",26,[["数学",100],["语文",150]]]
  3. ,["lisi","男",25,[["数学",100],["语文",150]]]
  4. ,["zhangsan","男",27,[["数学",120],["语文",158]]]
  5. ];

使用数组集中record对象的json属性来获取以细节区数据

var data=r.json[3];

2 在rowExpander的 expand事件中添加嵌套表格.