Implementing nested tables in ExtJS

Source: Internet
Author: User

First look at the effect:

The code is as follows:

  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", "Male", 26,[["mathematics", 100],["Chinese", 150]]
  17. , ["Lisi", "male", 25,[["mathematics", 100],["Chinese", 150]]
  18. , ["Zhangsan", "Male", 27,[["mathematics", 120],["Chinese", 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. Find 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: "Account", Dataindex: ' Class ', width:130,hideable:false,sortable:false,resizable:true}
  45. , {header: "Score", 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: "name", Dataindex: ' Name ', width:50,hideable:false,sortable:false}
  63. , {header: "Gender", Dataindex: ' Sex ', width:130,hideable:false,sortable:false,resizable:true}
  64. , {header: "Age", Dataindex: ' Ages ', 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>

The "rowexpander.js" used in the ExtJS is included in the official example.

Two tips to implement this nested table:

1 The data source that is provided to the datastore of the outer table represents the data of the detail area in the form of nested arrays, as shown in the following bold.

    1. var testdata=[
    2. ["Lugreen", "Male", "a", [["Mathematics", 100],["Chinese", [[] ]]
    3. , ["Lisi", "male", [["Mathematics", 100],["Chinese", [] ]]
    4. , ["Zhangsan", "male", [["Mathematics", 120],["Chinese", 158] ]
    5. ];

Use the JSON property of the Record object in the array set to get the detail area data

var data=r.json[3];

2 Add a nested table to the expand event in Rowexpander.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.