PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/backgrid-0.3.5/src/grid.js

https://github.com/kuzbida/backgrid_test
JavaScript | 206 lines | 74 code | 26 blank | 106 comment | 12 complexity | 2dd7decbd7c821ecd16dac8412c6a23b 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.Columns>|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. // Convert the list of column objects here first so the subviews don't have
  68. // to.
  69. if (!(options.columns instanceof Backbone.Collection)) {
  70. options.columns = new Columns(options.columns);
  71. }
  72. this.columns = options.columns;
  73. var filteredOptions = _.omit(options, ["el", "id", "attributes",
  74. "className", "tagName", "events"]);
  75. // must construct body first so it listens to backgrid:sort first
  76. this.body = options.body || this.body;
  77. this.body = new this.body(filteredOptions);
  78. this.header = options.header || this.header;
  79. if (this.header) {
  80. this.header = new this.header(filteredOptions);
  81. }
  82. this.footer = options.footer || this.footer;
  83. if (this.footer) {
  84. this.footer = new this.footer(filteredOptions);
  85. }
  86. this.listenTo(this.columns, "reset", function () {
  87. if (this.header) {
  88. this.header = new (this.header.remove().constructor)(filteredOptions);
  89. }
  90. this.body = new (this.body.remove().constructor)(filteredOptions);
  91. if (this.footer) {
  92. this.footer = new (this.footer.remove().constructor)(filteredOptions);
  93. }
  94. this.render();
  95. });
  96. },
  97. /**
  98. Delegates to Backgrid.Body#insertRow.
  99. */
  100. insertRow: function () {
  101. this.body.insertRow.apply(this.body, arguments);
  102. return this;
  103. },
  104. /**
  105. Delegates to Backgrid.Body#removeRow.
  106. */
  107. removeRow: function () {
  108. this.body.removeRow.apply(this.body, arguments);
  109. return this;
  110. },
  111. /**
  112. Delegates to Backgrid.Columns#add for adding a column. Subviews can listen
  113. to the `add` event from their internal `columns` if rerendering needs to
  114. happen.
  115. @param {Object} [options] Options for `Backgrid.Columns#add`.
  116. */
  117. insertColumn: function () {
  118. this.columns.add.apply(this.columns, arguments);
  119. return this;
  120. },
  121. /**
  122. Delegates to Backgrid.Columns#remove for removing a column. Subviews can
  123. listen to the `remove` event from the internal `columns` if rerendering
  124. needs to happen.
  125. @param {Object} [options] Options for `Backgrid.Columns#remove`.
  126. */
  127. removeColumn: function () {
  128. this.columns.remove.apply(this.columns, arguments);
  129. return this;
  130. },
  131. /**
  132. Delegates to Backgrid.Body#sort.
  133. */
  134. sort: function () {
  135. this.body.sort.apply(this.body, arguments);
  136. return this;
  137. },
  138. /**
  139. Renders the grid's header, then footer, then finally the body. Triggers a
  140. Backbone `backgrid:rendered` event along with a reference to the grid when
  141. the it has successfully been rendered.
  142. */
  143. render: function () {
  144. this.$el.empty();
  145. if (this.header) {
  146. this.$el.append(this.header.render().$el);
  147. }
  148. if (this.footer) {
  149. this.$el.append(this.footer.render().$el);
  150. }
  151. this.$el.append(this.body.render().$el);
  152. this.delegateEvents();
  153. this.trigger("backgrid:rendered", this);
  154. return this;
  155. },
  156. /**
  157. Clean up this grid and its subviews.
  158. @chainable
  159. */
  160. remove: function () {
  161. this.header && this.header.remove.apply(this.header, arguments);
  162. this.body.remove.apply(this.body, arguments);
  163. this.footer && this.footer.remove.apply(this.footer, arguments);
  164. return Backbone.View.prototype.remove.apply(this, arguments);
  165. }
  166. });