TransformGrid.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @class Ext.ux.grid.TransformGrid
  3. * @extends Ext.grid.Panel
  4. * A Grid which creates itself from an existing HTML table element.
  5. * @history
  6. * 2007-03-01 Original version by Nige "Animal" White
  7. * 2007-03-10 jvs Slightly refactored to reuse existing classes * @constructor
  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. Ext.define('Ext.ux.grid.TransformGrid', {
  15. extend: 'Ext.grid.Panel',
  16. constructor: function(table, config) {
  17. config = Ext.apply({}, config);
  18. table = this.table = Ext.get(table);
  19. var configFields = config.fields || [],
  20. configColumns = config.columns || [],
  21. fields = [],
  22. cols = [],
  23. ct = table.insertSibling(),
  24. headers = table.query("thead th"),
  25. i = 0,
  26. len = headers.length,
  27. data = table.dom,
  28. width,
  29. height,
  30. store,
  31. col,
  32. text,
  33. name;
  34. for (; i < len; ++i) {
  35. col = headers[i];
  36. text = col.innerHTML;
  37. name = 'tcol-' + i;
  38. fields.push(Ext.applyIf(configFields[i] || {}, {
  39. name: name,
  40. mapping: 'td:nth(' + (i + 1) + ')/@innerHTML'
  41. }));
  42. cols.push(Ext.applyIf(configColumns[i] || {}, {
  43. text: text,
  44. dataIndex: name,
  45. width: col.offsetWidth,
  46. tooltip: col.title,
  47. sortable: true
  48. }));
  49. }
  50. if (config.width) {
  51. width = config.width;
  52. } else {
  53. width = table.getWidth();
  54. }
  55. if (config.height) {
  56. height = config.height;
  57. }
  58. if (config.remove !== false) {
  59. // Don't use table.remove() as that destroys the row/cell data in the table in
  60. // IE6-7 so it cannot be read by the data reader.
  61. data.parentNode.removeChild(data);
  62. }
  63. Ext.applyIf(config, {
  64. store: {
  65. data: data,
  66. fields: fields,
  67. proxy: {
  68. type: 'memory',
  69. reader: {
  70. record: 'tbody tr',
  71. type: 'xml'
  72. }
  73. }
  74. },
  75. columns: cols,
  76. width: width,
  77. autoHeight: height ? false : true,
  78. height: height,
  79. el: ct
  80. });
  81. this.callParent([config]);
  82. },
  83. onDestroy: function() {
  84. this.callParent();
  85. this.table.remove();
  86. delete this.table;
  87. }
  88. });