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

Commit bfe039a

Browse files
committed
Fix #422. Remove sort caret for column when sort on a different column
1 parent c14f9fc commit bfe039a

3 files changed

Lines changed: 54 additions & 8 deletions

File tree

lib/backgrid.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,19 +2073,35 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
20732073
}
20742074
}
20752075
});
2076-
this.listenTo(column, "change:direction", this.resetCellDirection);
2076+
this.listenTo(column, "change:direction", this.setCellDirectionMaybe);
20772077
this.listenTo(column, "change:name change:label", this.render);
20782078

20792079
if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
20802080
if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
20812081
if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
2082+
2083+
this.listenTo(collection, "backgrid:sort", this.removeCellDirectionMaybe);
2084+
},
2085+
2086+
/**
2087+
Event handler for the collection's `backgrid:sort` event. Removes all the
2088+
CSS direction classes if the column being sorted on is not the same as this
2089+
header cell's.
2090+
*/
2091+
removeCellDirectionMaybe: function (columnToSort) {
2092+
if (columnToSort.cid != this.column.cid) {
2093+
this.$el.removeClass("ascending").removeClass("descending");
2094+
this.column.set("direction", null);
2095+
}
20822096
},
20832097

20842098
/**
2085-
Event handler for the column's `change:direction` event. Resets this cell's
2086-
direction to default if sorting is being done on another column.
2099+
Event handler for the column's `change:direction` event. If this
2100+
HeaderCell's column is being sorted on, it applies the direction given as a
2101+
CSS class to the header cell. Removes all the CSS direction classes
2102+
otherwise.
20872103
*/
2088-
resetCellDirection: function (columnToSort, direction) {
2104+
setCellDirectionMaybe: function (columnToSort, direction) {
20892105
this.$el.removeClass("ascending").removeClass("descending");
20902106
if (columnToSort.cid == this.column.cid) this.$el.addClass(direction);
20912107
},
@@ -2490,6 +2506,10 @@ var Body = Backgrid.Body = Backbone.View.extend({
24902506
*/
24912507
sort: function (column, direction) {
24922508

2509+
if (!_.contains(["ascending", "descending", null], direction)) {
2510+
throw new RangeError('direction must be one of "ascending", "descending" or `null`');
2511+
}
2512+
24932513
if (_.isString(column)) column = this.columns.findWhere({name: column});
24942514

24952515
var collection = this.collection;

src/header.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,35 @@ var HeaderCell = Backgrid.HeaderCell = Backbone.View.extend({
4949
}
5050
}
5151
});
52-
this.listenTo(column, "change:direction", this.resetCellDirection);
52+
this.listenTo(column, "change:direction", this.setCellDirectionMaybe);
5353
this.listenTo(column, "change:name change:label", this.render);
5454

5555
if (Backgrid.callByNeed(column.editable(), column, collection)) $el.addClass("editable");
5656
if (Backgrid.callByNeed(column.sortable(), column, collection)) $el.addClass("sortable");
5757
if (Backgrid.callByNeed(column.renderable(), column, collection)) $el.addClass("renderable");
58+
59+
this.listenTo(collection, "backgrid:sort", this.removeCellDirectionMaybe);
60+
},
61+
62+
/**
63+
Event handler for the collection's `backgrid:sort` event. Removes all the
64+
CSS direction classes if the column being sorted on is not the same as this
65+
header cell's.
66+
*/
67+
removeCellDirectionMaybe: function (columnToSort) {
68+
if (columnToSort.cid != this.column.cid) {
69+
this.$el.removeClass("ascending").removeClass("descending");
70+
this.column.set("direction", null);
71+
}
5872
},
5973

6074
/**
61-
Event handler for the column's `change:direction` event. Resets this cell's
62-
direction to default if sorting is being done on another column.
75+
Event handler for the column's `change:direction` event. If this
76+
HeaderCell's column is being sorted on, it applies the direction given as a
77+
CSS class to the header cell. Removes all the CSS direction classes
78+
otherwise.
6379
*/
64-
resetCellDirection: function (columnToSort, direction) {
80+
setCellDirectionMaybe: function (columnToSort, direction) {
6581
this.$el.removeClass("ascending").removeClass("descending");
6682
if (columnToSort.cid == this.column.cid) this.$el.addClass(direction);
6783
},

test/header.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ describe("A HeaderCell", function () {
190190
expect(cell.$el.hasClass("descending")).toBe(false);
191191
});
192192

193+
it("will remove its direction CSS class if `backgrid:sort` is triggered with a different column", function () {
194+
cell.column.set("direction", "ascending");
195+
cell.collection.trigger("backgrid:sort", new Backgrid.Column({
196+
name: "name",
197+
cell: "string"
198+
}), "descending");
199+
expect(cell.$el.hasClass("ascending")).toBe(false);
200+
expect(cell.$el.hasClass("descending")).toBe(false);
201+
});
202+
193203
it("with `sortType` set to `toggle`, triggers `backgrid:sort` with the column and direction set to 'ascending' if the column's direction is not set", function () {
194204
var column, direction;
195205
cell.column.set("sortType", "toggle");

0 commit comments

Comments
 (0)