Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 7ca11d4

Browse files
committed
Merge pull request #605 from JoeGaebel/emptyView-colspan-fix
Add dynamic colspan updating to emptyRow
2 parents 3fd4c14 + da772ae commit 7ca11d4

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/body.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ var Body = Backgrid.Body = Backbone.View.extend({
5959
this.listenTo(collection, "reset", this.refresh);
6060
this.listenTo(collection, "backgrid:sort", this.sort);
6161
this.listenTo(collection, "backgrid:edited", this.moveToNextCell);
62+
63+
this.listenTo(this.columns, "add remove", this.updateEmptyRow);
6264
},
6365

6466
_unshiftEmptyRowMayBe: function () {
6567
if (this.rows.length === 0 && this.emptyText != null) {
66-
this.rows.unshift(new EmptyRow({
68+
this.emptyRow = new EmptyRow({
6769
emptyText: this.emptyText,
6870
columns: this.columns
69-
}));
70-
return true;
71+
});
72+
73+
this.rows.unshift(this.emptyRow);
74+
return true
7175
}
7276
},
7377

@@ -173,6 +177,17 @@ var Body = Backgrid.Body = Backbone.View.extend({
173177
return this;
174178
},
175179

180+
/**
181+
Rerender the EmptyRow which empties the DOM element, creates the td with the
182+
updated colspan, and appends it back into the DOM
183+
*/
184+
185+
updateEmptyRow: function () {
186+
if (this.emptyRow != null) {
187+
this.emptyRow.render();
188+
}
189+
},
190+
176191
/**
177192
Reinitialize all the rows inside the body and re-render them. Triggers a
178193
Backbone `backgrid:refresh` event from the collection along with the body

test/body.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,23 @@ describe("A Body", function () {
224224
expect(body.$el.find("tr.empty > td").attr("colspan")).toBe("1");
225225
});
226226

227+
it("will update the colspan of the empty row as columns are changed", function () {
228+
col.reset();
229+
body = new Backgrid.Body({
230+
emptyText: " ",
231+
columns: [{
232+
name: "id",
233+
cell: "integer"
234+
}],
235+
collection: col
236+
});
237+
body.render();
238+
239+
expect(body.$el.find("tr.empty > td").attr("colspan")).toBe("1");
240+
body.columns.push({name: "age", cell: "integer"});
241+
expect(body.$el.find("tr.empty > td").attr("colspan")).toBe("2");
242+
});
243+
227244
it("will clear the empty row if a new model is added to an empty collection", function () {
228245
col.reset();
229246
body = new Backgrid.Body({
@@ -270,6 +287,13 @@ describe("A Body", function () {
270287
expect(body.$el.find("tr.empty").length).toBe(1);
271288
});
272289

290+
it("won't call render from updateEmptyRow if there is no emptyView", function () {
291+
var pushColumn = function () {
292+
body.columns.push({name: "age", cell: "integer"});
293+
};
294+
expect(pushColumn).not.toThrow();
295+
});
296+
273297
it("#sort will throw a RangeError is direction is not ascending, descending or null", function () {
274298
body = new Backgrid.Body({
275299
collection: col,

0 commit comments

Comments
 (0)