Reputation: 19368
I have a table generated with data from the server. Each column on the table has a button at the top to sort the entire table from high to low or low to high based on the vales in that column. There is also a name column which sorts alphabetically.
Currently I have all this hooked up with handlebars, and when you click the button, it sorts an array of table-data, and then re-interpolates the template and inserts it to the page.
I am trying to move this over to using ember.js. My first issue is that the my table-data array is not a simple array anymore, its an ember object with all kinds of get and set methods and other stuff inside. How can I sort this using a function like the one I have:
function sortAtoZ(arr, one, two) {
arr.sort(function(a, b) {return a[one].localeCompare(b[two])});
}
Upvotes: 0
Views: 1783
Reputation: 19050
Support for basic sorting is built into ember array controllers via the Sortable Mixin. Out-of-box it supports sorting by one or more properties using Ember.compare but can use a custom sortFunction like:
App.TableController = Ember.ArrayController.extend({
sortProperties: ['trackNumber'],
sortAscending: true,
sortFunction: function(a,b) {
// your custom sort logic here
// return 0 if the two parameters are equal, return a negative value if the first parameter is smaller than the second or return a positive value otherwise
}
});
Also if you want to do something more advanced with tables, have a look at ember-table
Upvotes: 1