TransformGrid.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * A Grid which creates itself from an existing HTML table element.
  3. */
  4. Ext.define('Ext.ux.grid.TransformGrid', {
  5. extend: 'Ext.grid.Panel',
  6. /**
  7. * Creates the grid from HTML table element.
  8. * @param {String/HTMLElement/Ext.Element} table The table element from which this grid will be created -
  9. * The table MUST have some type of size defined for the grid to fill. The container will be
  10. * automatically set to position relative if it isn't already.
  11. * @param {Object} [config] A config object that sets properties on this grid and has two additional (optional)
  12. * properties: fields and columns which allow for customizing data fields and columns for this grid.
  13. */
  14. constructor: function(table, config) {
  15. config = Ext.apply({}, config);
  16. table = this.table = Ext.get(table);
  17. var configFields = config.fields || [],
  18. configColumns = config.columns || [],
  19. fields = [],
  20. cols = [],
  21. headers = table.query("thead th"),
  22. i = 0,
  23. len = headers.length,
  24. data = table.dom,
  25. width,
  26. height,
  27. store,
  28. col,
  29. text,
  30. name;
  31. for (; i < len; ++i) {
  32. col = headers[i];
  33. text = col.innerHTML;
  34. name = 'tcol-' + i;
  35. fields.push(Ext.applyIf(configFields[i] || {}, {
  36. name: name,
  37. mapping: 'td:nth(' + (i + 1) + ')/@innerHTML'
  38. }));
  39. cols.push(Ext.applyIf(configColumns[i] || {}, {
  40. text: text,
  41. dataIndex: name,
  42. width: col.offsetWidth,
  43. tooltip: col.title,
  44. sortable: true
  45. }));
  46. }
  47. if (config.width) {
  48. width = config.width;
  49. } else {
  50. width = table.getWidth() + 1;
  51. }
  52. if (config.height) {
  53. height = config.height;
  54. }
  55. Ext.applyIf(config, {
  56. store: {
  57. data: data,
  58. fields: fields,
  59. proxy: {
  60. type: 'memory',
  61. reader: {
  62. record: 'tbody tr',
  63. type: 'xml'
  64. }
  65. }
  66. },
  67. columns: cols,
  68. width: width,
  69. height: height
  70. });
  71. this.callParent([config]);
  72. if (config.remove !== false) {
  73. // Don't use table.remove() as that destroys the row/cell data in the table in
  74. // IE6-7 so it cannot be read by the data reader.
  75. data.parentNode.removeChild(data);
  76. }
  77. },
  78. onDestroy: function() {
  79. this.callParent();
  80. this.table.remove();
  81. delete this.table;
  82. }
  83. });