/ext-4.0.7/examples/ux/grid/TransformGrid.js

https://bitbucket.org/srogerf/javascript · JavaScript · 114 lines · 73 code · 12 blank · 29 comment · 9 complexity · 55e20b3ecd25d39890efb13c5c859e02 MD5 · raw file

  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.grid.TransformGrid
  11. * @extends Ext.grid.Panel
  12. * A Grid which creates itself from an existing HTML table element.
  13. * @history
  14. * 2007-03-01 Original version by Nige "Animal" White
  15. * 2007-03-10 jvs Slightly refactored to reuse existing classes * @constructor
  16. * @param {String/HTMLElement/Ext.Element} table The table element from which this grid will be created -
  17. * The table MUST have some type of size defined for the grid to fill. The container will be
  18. * automatically set to position relative if it isn't already.
  19. * @param {Object} config A config object that sets properties on this grid and has two additional (optional)
  20. * properties: fields and columns which allow for customizing data fields and columns for this grid.
  21. */
  22. Ext.define('Ext.ux.grid.TransformGrid', {
  23. extend: 'Ext.grid.Panel',
  24. constructor: function(table, config) {
  25. config = Ext.apply({}, config);
  26. table = this.table = Ext.get(table);
  27. var configFields = config.fields || [],
  28. configColumns = config.columns || [],
  29. fields = [],
  30. cols = [],
  31. ct = table.insertSibling(),
  32. headers = table.query("thead th"),
  33. i = 0,
  34. len = headers.length,
  35. data = table.dom,
  36. width,
  37. height,
  38. store,
  39. col,
  40. text,
  41. name;
  42. for (; i < len; ++i) {
  43. col = headers[i];
  44. text = col.innerHTML;
  45. name = 'tcol-' + i;
  46. fields.push(Ext.applyIf(configFields[i] || {}, {
  47. name: name,
  48. mapping: 'td:nth(' + (i + 1) + ')/@innerHTML'
  49. }));
  50. cols.push(Ext.applyIf(configColumns[i] || {}, {
  51. text: text,
  52. dataIndex: name,
  53. width: col.offsetWidth,
  54. tooltip: col.title,
  55. sortable: true
  56. }));
  57. }
  58. if (config.width) {
  59. width = config.width;
  60. } else {
  61. width = table.getWidth() + 1;
  62. }
  63. if (config.height) {
  64. height = config.height;
  65. }
  66. if (config.remove !== false) {
  67. // Don't use table.remove() as that destroys the row/cell data in the table in
  68. // IE6-7 so it cannot be read by the data reader.
  69. data.parentNode.removeChild(data);
  70. }
  71. Ext.applyIf(config, {
  72. store: {
  73. data: data,
  74. fields: fields,
  75. proxy: {
  76. type: 'memory',
  77. reader: {
  78. record: 'tbody tr',
  79. type: 'xml'
  80. }
  81. }
  82. },
  83. columns: cols,
  84. width: width,
  85. autoHeight: height ? false : true,
  86. height: height,
  87. el: ct
  88. });
  89. this.callParent([config]);
  90. },
  91. onDestroy: function() {
  92. this.callParent();
  93. this.table.remove();
  94. delete this.table;
  95. }
  96. });