from-markup.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.onReady(function() {
  7. var btn = Ext.get("create-grid");
  8. btn.on("click", function() {
  9. btn.dom.disabled = true;
  10. // create the grid
  11. var grid = new Ext.grid.TableGrid("the-table");
  12. grid.render();
  13. }, false, {
  14. single : true
  15. }); // run once
  16. });
  17. /**
  18. * @class Ext.grid.TableGrid
  19. * @extends Ext.grid.Grid A Grid which creates itself from an existing HTML
  20. * table element.
  21. * @constructor
  22. * @param {String/HTMLElement/Ext.Element}
  23. * table The table element from which this grid will be created - The
  24. * table MUST have some type of size defined for the grid to fill.
  25. * The container will be automatically set to position relative if it
  26. * isn't already.
  27. * @param {Object}
  28. * config A config object that sets properties on this grid and has
  29. * two additional (optional) properties: fields and columns which
  30. * allow for customizing data fields and columns for this grid.
  31. * @history 2007-03-01 Original version by Nige "Animal" White 2007-03-10 jvs
  32. * Slightly refactored to reuse existing classes
  33. */
  34. Ext.grid.TableGrid = function(table, config) {
  35. config = config || {};
  36. var cf = config.fields || [], ch = config.columns || [];
  37. table = Ext.get(table);
  38. var ct = table.insertSibling();
  39. var fields = [], cols = [];
  40. var headers = table.query("thead th");
  41. for (var i = 0, h; h = headers[i]; i++) {
  42. var text = h.innerHTML;
  43. var name = 'tcol-' + i;
  44. fields.push(Ext.applyIf(cf[i] || {}, {
  45. name : name,
  46. mapping : 'td:nth(' + (i + 1) + ')/@innerHTML'
  47. }));
  48. cols.push(Ext.applyIf(ch[i] || {}, {
  49. 'header' : text,
  50. 'dataIndex' : name,
  51. 'width' : h.offsetWidth,
  52. 'tooltip' : h.title,
  53. 'sortable' : true
  54. }));
  55. }
  56. var ds = new Ext.data.Store({
  57. reader : new Ext.data.XmlReader({
  58. record : 'tbody tr'
  59. }, fields)
  60. });
  61. ds.loadData(table.dom);
  62. var cm = new Ext.grid.ColumnModel(cols);
  63. if (config.width || config.height) {
  64. ct.setSize(config.width || 'auto', config.height || 'auto');
  65. } else {
  66. ct.setWidth(table.getWidth());
  67. }
  68. if (config.remove !== false) {
  69. table.remove();
  70. }
  71. Ext.applyIf(this, {
  72. 'ds' : ds,
  73. 'cm' : cm,
  74. 'sm' : new Ext.grid.RowSelectionModel(),
  75. autoHeight : true,
  76. autoWidth : false
  77. });
  78. Ext.grid.TableGrid.superclass.constructor.call(this, ct, {});
  79. };
  80. Ext.extend(Ext.grid.TableGrid, Ext.grid.GridPanel);