TransformGrid.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ux-grid-TransformGrid'>/**
  19. </span> * A Grid which creates itself from an existing HTML table element.
  20. */
  21. Ext.define('Ext.ux.grid.TransformGrid', {
  22. extend: 'Ext.grid.Panel',
  23. <span id='Ext-ux-grid-TransformGrid-method-constructor'> /**
  24. </span> * Creates the grid from HTML table element.
  25. * @param {String/HTMLElement/Ext.Element} table The table element from which this grid will be created -
  26. * The table MUST have some type of size defined for the grid to fill. The container will be
  27. * automatically set to position relative if it isn't already.
  28. * @param {Object} [config] A config object that sets properties on this grid and has two additional (optional)
  29. * properties: fields and columns which allow for customizing data fields and columns for this grid.
  30. */
  31. constructor: function(table, config) {
  32. config = Ext.apply({}, config);
  33. table = this.table = Ext.get(table);
  34. var configFields = config.fields || [],
  35. configColumns = config.columns || [],
  36. fields = [],
  37. cols = [],
  38. headers = table.query(&quot;thead th&quot;),
  39. i = 0,
  40. len = headers.length,
  41. data = table.dom,
  42. width,
  43. height,
  44. store,
  45. col,
  46. text,
  47. name;
  48. for (; i &lt; len; ++i) {
  49. col = headers[i];
  50. text = col.innerHTML;
  51. name = 'tcol-' + i;
  52. fields.push(Ext.applyIf(configFields[i] || {}, {
  53. name: name,
  54. mapping: 'td:nth(' + (i + 1) + ')/@innerHTML'
  55. }));
  56. cols.push(Ext.applyIf(configColumns[i] || {}, {
  57. text: text,
  58. dataIndex: name,
  59. width: col.offsetWidth,
  60. tooltip: col.title,
  61. sortable: true
  62. }));
  63. }
  64. if (config.width) {
  65. width = config.width;
  66. } else {
  67. width = table.getWidth() + 1;
  68. }
  69. if (config.height) {
  70. height = config.height;
  71. }
  72. Ext.applyIf(config, {
  73. store: {
  74. data: data,
  75. fields: fields,
  76. proxy: {
  77. type: 'memory',
  78. reader: {
  79. record: 'tbody tr',
  80. type: 'xml'
  81. }
  82. }
  83. },
  84. columns: cols,
  85. width: width,
  86. height: height
  87. });
  88. this.callParent([config]);
  89. if (config.remove !== false) {
  90. // Don't use table.remove() as that destroys the row/cell data in the table in
  91. // IE6-7 so it cannot be read by the data reader.
  92. data.parentNode.removeChild(data);
  93. }
  94. },
  95. onDestroy: function() {
  96. this.callParent();
  97. this.table.remove();
  98. delete this.table;
  99. }
  100. });</pre>
  101. </body>
  102. </html>