This repository was archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathgrid.js
More file actions
213 lines (170 loc) · 6 KB
/
Copy pathgrid.js
File metadata and controls
213 lines (170 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
backgrid
http://github.com/wyuenho/backgrid
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
Licensed under the MIT license.
*/
/**
Grid represents a data grid that has a header, body and an optional footer.
By default, a Grid treats each model in a collection as a row, and each
attribute in a model as a column. To render a grid you must provide a list of
column metadata and a collection to the Grid constructor. Just like any
Backbone.View class, the grid is rendered as a DOM node fragment when you
call render().
var grid = Backgrid.Grid({
columns: [{ name: "id", label: "ID", type: "string" },
// ...
],
collections: books
});
$("#table-container").append(grid.render().el);
Optionally, if you want to customize the rendering of the grid's header and
footer, you may choose to extend Backgrid.Header and Backgrid.Footer, and
then supply that class or an instance of that class to the Grid constructor.
See the documentation for Header and Footer for further details.
var grid = Backgrid.Grid({
columns: [{ name: "id", label: "ID", type: "string" }],
collections: books,
header: Backgrid.Header.extend({
//...
}),
footer: Backgrid.Paginator
});
Finally, if you want to override how the rows are rendered in the table body,
you can supply a Body subclass as the `body` attribute that uses a different
Row class.
@class Backgrid.Grid
@extends Backbone.View
See:
- Backgrid.Column
- Backgrid.Header
- Backgrid.Body
- Backgrid.Row
- Backgrid.Footer
*/
var Grid = Backgrid.Grid = Backbone.View.extend({
/** @property */
tagName: "table",
/** @property */
className: "backgrid",
/** @property */
header: Header,
/** @property */
body: Body,
/** @property */
footer: null,
/** @property */
columns: [],
/**
Initializes a Grid instance.
@param {Object} options
@param {Backbone.Collection.<Backgrid.Columns>|Array.<Backgrid.Column>|Array.<Object>} options.columns Column metadata.
@param {Backbone.Collection} options.collection The collection of tabular model data to display.
@param {Backgrid.Header} [options.header=Backgrid.Header] An optional Header class to override the default.
@param {Backgrid.Body} [options.body=Backgrid.Body] An optional Body class to override the default.
@param {Backgrid.Row} [options.row=Backgrid.Row] An optional Row class to override the default.
@param {Backgrid.Footer} [options.footer=Backgrid.Footer] An optional Footer class.
*/
initialize: function (options) {
// Set Columns, either passed by options or columns property.
options.columns = options.columns || this.columns;
// Convert the list of column objects here first so the subviews don't have
// to.
if (!(options.columns instanceof Backbone.Collection)) {
options.columns = new Columns(options.columns);
}
this.columns = options.columns;
var filteredOptions = _.omit(options, ["el", "id", "attributes",
"className", "tagName", "events"]);
// must construct body first so it listens to backgrid:sort first
this.body = options.body || this.body;
this.body = new this.body(filteredOptions);
this.header = options.header || this.header;
if (this.header) {
this.header = new this.header(filteredOptions);
}
this.footer = options.footer || this.footer;
if (this.footer) {
this.footer = new this.footer(filteredOptions);
}
this.listenTo(this.columns, "reset", function () {
if (this.header) {
this.header = new (this.header.remove().constructor)(filteredOptions);
}
this.body = new (this.body.remove().constructor)(filteredOptions);
if (this.footer) {
this.footer = new (this.footer.remove().constructor)(filteredOptions);
}
this.render();
});
},
/**
Delegates to Backgrid.Body#insertRow.
*/
insertRow: function () {
this.body.insertRow.apply(this.body, arguments);
return this;
},
/**
Delegates to Backgrid.Body#removeRow.
*/
removeRow: function () {
this.body.removeRow.apply(this.body, arguments);
return this;
},
/**
Delegates to Backgrid.Columns#add for adding a column. Subviews can listen
to the `add` event from their internal `columns` if rerendering needs to
happen.
@param {Object} [options] Options for `Backgrid.Columns#add`.
*/
insertColumn: function () {
this.columns.add.apply(this.columns, arguments);
return this;
},
/**
Delegates to Backgrid.Columns#remove for removing a column. Subviews can
listen to the `remove` event from the internal `columns` if rerendering
needs to happen.
@param {Object} [options] Options for `Backgrid.Columns#remove`.
*/
removeColumn: function () {
this.columns.remove.apply(this.columns, arguments);
return this;
},
/**
Delegates to Backgrid.Body#sort.
*/
sort: function () {
this.body.sort.apply(this.body, arguments);
return this;
},
/**
Renders the grid's header, then footer, then finally the body. Triggers a
Backbone `backgrid:rendered` event along with a reference to the grid when
the it has successfully been rendered.
*/
render: function () {
this.$el.empty();
if (this.header) {
this.$el.append(this.header.render().$el);
}
if (this.footer) {
this.$el.append(this.footer.render().$el);
}
this.$el.append(this.body.render().$el);
this.delegateEvents();
this.trigger("backgrid:rendered", this);
return this;
},
/**
Clean up this grid and its subviews.
@chainable
*/
remove: function () {
this.header && this.header.remove.apply(this.header, arguments);
this.body.remove.apply(this.body, arguments);
this.footer && this.footer.remove.apply(this.footer, arguments);
return Backbone.View.prototype.remove.apply(this, arguments);
}
});