I want to create a table that is sortable using the attributes of a collection.
So far i’ve being able to make the tab table sortable using two attributes, but i would like to be sortable based the value of the sort key attribute.
e.g when the “task_status = ‘open'”
Here what i having working now
var TaskCollection = Backbone.Collection.extend({
//Model
model:Task,
//url
url:"./api/tasks",
//construct
initialize: function() {
this.sort_key = 'end';
this.fetch();
},
comparator: function(a,b) {
a = a.get(this.sort_key);
b = b.get(this.sort_key);
return a > b ? 1
: a < b ? -1
: 0;
},
sort_by_status: function() {
this.sort_key = 'task_status';
this.sort();
},
sort_by_task_tag: function() {
this.sort_key = 'task_group';
this.sort();
}
});
This sorts the collection but does not reverse the order, or allow me to sort by the particular value of an attribute. How can this be modified to work
In the comparator, have a state variable for “reversed” and have it take values 1 and negative 1. Multiply it by the previous return value. Setting the state variable on the collection and then again sorting should get things done.