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

Column attribute support & Header row sort support #527

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/backgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2152,7 +2152,11 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({

this.$el.append(label);
this.$el.addClass(column.get("name"));
this.$el.attr("data-columnname", column.get("name"));
this.$el.addClass(column.get("direction"));
if (column.get("attributes")) {
this.$el.attr(column.get("attributes"));
}
this.delegateEvents();
return this;
}
Expand Down Expand Up @@ -2221,7 +2225,18 @@ var Header = Backgrid.Header = Backbone.View.extend({
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Columns(this.columns);
}
this.createHeaderRow();

this.listenTo(this.columns, "sort", _.bind(function() {
this.createHeaderRow();
this.render();
}, this));
},

/**
Sets up a new headerRow and attaches it to the view
*/
createHeaderRow: function() {
this.row = new Backgrid.HeaderRow({
columns: this.columns,
collection: this.collection
Expand All @@ -2232,8 +2247,10 @@ var Header = Backgrid.Header = Backbone.View.extend({
Renders this table head with a single row of header cells.
*/
render: function () {
this.$el.empty();
this.$el.append(this.row.render().$el);
this.delegateEvents();
this.trigger("backgrid:header:rendered", this);
return this;
},

Expand Down
17 changes: 17 additions & 0 deletions src/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({

this.$el.append(label);
this.$el.addClass(column.get("name"));
this.$el.attr("data-columnname", column.get("name"));
this.$el.addClass(column.get("direction"));
if (column.get("attributes")) {
this.$el.attr(column.get("attributes"));
}
this.delegateEvents();
return this;
}
Expand Down Expand Up @@ -196,7 +200,18 @@ var Header = Backgrid.Header = Backbone.View.extend({
if (!(this.columns instanceof Backbone.Collection)) {
this.columns = new Columns(this.columns);
}
this.createHeaderRow();

this.listenTo(this.columns, "sort", _.bind(function() {
this.createHeaderRow();
this.render();
}, this));
},

/**
Sets up a new headerRow and attaches it to the view
*/
createHeaderRow: function() {
this.row = new Backgrid.HeaderRow({
columns: this.columns,
collection: this.collection
Expand All @@ -207,8 +222,10 @@ var Header = Backgrid.Header = Backbone.View.extend({
Renders this table head with a single row of header cells.
*/
render: function () {
this.$el.empty();
this.$el.append(this.row.render().$el);
this.delegateEvents();
this.trigger("backgrid:header:rendered", this);
return this;
},

Expand Down
59 changes: 55 additions & 4 deletions test/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,16 @@ describe("A HeaderRow", function () {
row = new Backgrid.HeaderRow({
columns: [{
name: "name",
cell: "string"
cell: "string",
attributes: {
colspan: "1"
}
}, {
name: "year",
cell: "integer"
cell: "integer",
attributes: {
colspan: "2"
}
}],
collection: books
});
Expand All @@ -292,6 +298,8 @@ describe("A HeaderRow", function () {
expect(th1.hasClass("sortable")).toBe(true);
expect(th1.hasClass("renderable")).toBe(true);
expect(th1.hasClass("name")).toBe(true);
expect(th1.data("columnname")).toBe("name");
expect(th1.attr("colspan")).toBe("1");
expect(th1.find("a").text()).toBe("name");
expect(th1.find("a").eq(1).is($("b", {className: "sort-caret"})));

Expand All @@ -300,6 +308,8 @@ describe("A HeaderRow", function () {
expect(th2.hasClass("sortable")).toBe(true);
expect(th2.hasClass("renderable")).toBe(true);
expect(th2.hasClass("year")).toBe(true);
expect(th2.data("columnname")).toBe("year");
expect(th2.attr("colspan")).toBe("2");
expect(th2.find("a").text()).toBe("year");
expect(th2.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);
});
Expand Down Expand Up @@ -352,7 +362,6 @@ describe("A Header", function () {
var head;

beforeEach(function () {

books = new Books([{
title: "Alice's Adventures in Wonderland",
year: 1865
Expand All @@ -363,6 +372,10 @@ describe("A Header", function () {
title: "The Catcher in the Rye",
year: 1951
}]);
});

it("creates a header row on initialization", function() {
spyOn(Backgrid.Header.prototype, "createHeaderRow");

head = new Backgrid.Header({
columns: [{
Expand All @@ -374,11 +387,46 @@ describe("A Header", function () {
}],
collection: books
});
expect(head.createHeaderRow.callCount).toEqual(1);
});

head.render();
it("renders again when sort is triggered on the column collection", function() {
head = new Backgrid.Header({
columns: [{
name: "name",
cell: "string"
}, {
name: "year",
cell: "integer"
}],
collection: books
});

spyOn(head, "createHeaderRow");
spyOn(head, "render");

head.columns.trigger("sort");

expect(head.createHeaderRow).toHaveBeenCalled();
expect(head.render).toHaveBeenCalled();
});

it("renders a header with a row of header cells", function () {
head = new Backgrid.Header({
columns: [{
name: "name",
cell: "string"
}, {
name: "year",
cell: "integer"
}],
collection: books
});

spyOn(head, "trigger");

head.render();

expect(head.$el[0].tagName).toBe("THEAD");

var th1 = $(head.row.el.childNodes[0]);
Expand All @@ -396,6 +444,9 @@ describe("A Header", function () {
expect(th2.hasClass("year")).toBe(true);
expect(th2.find("a").text()).toBe("year");
expect(th2.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);

expect(head.trigger.calls.length).toBe(1);
expect(head.trigger).toHaveBeenCalledWith("backgrid:header:rendered", head);
});

});