PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/grid.js

https://github.com/antonyraj/backgrid
JavaScript | 199 lines | 64 code | 27 blank | 108 comment | 9 complexity | 5c2ecb48bbf5602379f74ad124f56dca MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. /**
  8. Grid represents a data grid that has a header, body and an optional footer.
  9. By default, a Grid treats each model in a collection as a row, and each
  10. attribute in a model as a column. To render a grid you must provide a list of
  11. column metadata and a collection to the Grid constructor. Just like any
  12. Backbone.View class, the grid is rendered as a DOM node fragment when you
  13. call render().
  14. var grid = Backgrid.Grid({
  15. columns: [{ name: "id", label: "ID", type: "string" },
  16. // ...
  17. ],
  18. collections: books
  19. });
  20. $("#table-container").append(grid.render().el);
  21. Optionally, if you want to customize the rendering of the grid's header and
  22. footer, you may choose to extend Backgrid.Header and Backgrid.Footer, and
  23. then supply that class or an instance of that class to the Grid constructor.
  24. See the documentation for Header and Footer for further details.
  25. var grid = Backgrid.Grid({
  26. columns: [{ name: "id", label: "ID", type: "string" }],
  27. collections: books,
  28. header: Backgrid.Header.extend({
  29. //...
  30. }),
  31. footer: Backgrid.Paginator
  32. });
  33. Finally, if you want to override how the rows are rendered in the table body,
  34. you can supply a Body subclass as the `body` attribute that uses a different
  35. Row class.
  36. @class Backgrid.Grid
  37. @extends Backbone.View
  38. See:
  39. - Backgrid.Column
  40. - Backgrid.Header
  41. - Backgrid.Body
  42. - Backgrid.Row
  43. - Backgrid.Footer
  44. */
  45. var Grid = Backgrid.Grid = Backbone.View.extend({
  46. /** @property */
  47. tagName: "table",
  48. /** @property */
  49. className: "backgrid",
  50. /** @property */
  51. header: Header,
  52. /** @property */
  53. body: Body,
  54. /** @property */
  55. footer: null,
  56. /**
  57. Initializes a Grid instance.
  58. @param {Object} options
  59. @param {Backbone.Collection.<Backgrid.Column>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
  60. @param {Backbone.Collection} options.collection The collection of tabular model data to display.
  61. @param {Backgrid.Header} [options.header=Backgrid.Header] An optional Header class to override the default.
  62. @param {Backgrid.Body} [options.body=Backgrid.Body] An optional Body class to override the default.
  63. @param {Backgrid.Row} [options.row=Backgrid.Row] An optional Row class to override the default.
  64. @param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
  65. */
  66. initialize: function (options) {
  67. Backgrid.requireOptions(options, ["columns", "collection"]);
  68. // Convert the list of column objects here first so the subviews don't have
  69. // to.
  70. if (!(options.columns instanceof Backbone.Collection)) {
  71. options.columns = new Columns(options.columns);
  72. }
  73. this.columns = options.columns;
  74. var passedThruOptions = _.omit(options, ["el", "id", "attributes",
  75. "className", "tagName", "events"]);
  76. this.header = options.header || this.header;
  77. this.header = new this.header(passedThruOptions);
  78. this.body = options.body || this.body;
  79. this.body = new this.body(passedThruOptions);
  80. this.footer = options.footer || this.footer;
  81. if (this.footer) {
  82. this.footer = new this.footer(passedThruOptions);
  83. }
  84. this.listenTo(this.columns, "reset", function () {
  85. this.header = new (this.header.remove().constructor)(passedThruOptions);
  86. this.body = new (this.body.remove().constructor)(passedThruOptions);
  87. if (this.footer) {
  88. this.footer = new (this.footer.remove().constructor)(passedThruOptions);
  89. }
  90. this.render();
  91. });
  92. },
  93. /**
  94. Delegates to Backgrid.Body#insertRow.
  95. */
  96. insertRow: function (model, collection, options) {
  97. return this.body.insertRow(model, collection, options);
  98. },
  99. /**
  100. Delegates to Backgrid.Body#removeRow.
  101. */
  102. removeRow: function (model, collection, options) {
  103. return this.body.removeRow(model, collection, options);
  104. },
  105. /**
  106. Delegates to Backgrid.Columns#add for adding a column. Subviews can listen
  107. to the `add` event from their internal `columns` if rerendering needs to
  108. happen.
  109. @param {Object} [options] Options for `Backgrid.Columns#add`.
  110. @param {boolean} [options.render=true] Whether to render the column
  111. immediately after insertion.
  112. @chainable
  113. */
  114. insertColumn: function (column, options) {
  115. options = options || {render: true};
  116. this.columns.add(column, options);
  117. return this;
  118. },
  119. /**
  120. Delegates to Backgrid.Columns#remove for removing a column. Subviews can
  121. listen to the `remove` event from the internal `columns` if rerendering
  122. needs to happen.
  123. @param {Object} [options] Options for `Backgrid.Columns#remove`.
  124. @chainable
  125. */
  126. removeColumn: function (column, options) {
  127. this.columns.remove(column, options);
  128. return this;
  129. },
  130. /**
  131. Renders the grid's header, then footer, then finally the body. Triggers a
  132. Backbone `backgrid:rendered` event along with a reference to the grid when
  133. the it has successfully been rendered.
  134. */
  135. render: function () {
  136. this.$el.empty();
  137. this.$el.append(this.header.render().$el);
  138. if (this.footer) {
  139. this.$el.append(this.footer.render().$el);
  140. }
  141. this.$el.append(this.body.render().$el);
  142. this.delegateEvents();
  143. this.trigger("backgrid:rendered", this);
  144. return this;
  145. },
  146. /**
  147. Clean up this grid and its subviews.
  148. @chainable
  149. */
  150. remove: function () {
  151. this.header.remove.apply(this.header, arguments);
  152. this.body.remove.apply(this.body, arguments);
  153. this.footer && this.footer.remove.apply(this.footer, arguments);
  154. return Backbone.View.prototype.remove.apply(this, arguments);
  155. }
  156. });