Sortable Table
HTML
<div data-bind="foreach: filters">
<input type="button" data-bind="click: $parent.setActiveFilter, value: title"/>
</div>
<br/>
<table>
<thead>
<tr data-bind="foreach: headers">
<th data-bind="click: $parent.sort, text: title"></th>
</tr>
</thead>
<tbody data-bind="foreach: filteredPeople">
<tr>
<td data-bind="text: firstName"></td>
<td data-bind="text: lastName"></td>
<td data-bind="text: age"></td>
</tr>
</tbody>
</table>
CSS
th
{
cursor:pointer;
}
JavaScript
var viewModel = function(){
var self = this;
self.people = ko.observableArray([
{firstName:'James',lastName:'Smith',age:38},
{firstName:'Susan',lastName:'Smith',age:36},
{firstName:'Jeremy',lastName:'Smith',age:10},
{firstName:'Megan',lastName:'Smith',age:7},
{firstName:'James',lastName:'Jones',age:40},
{firstName:'Martha',lastName:'Jones',age:36},
{firstName:'Peggy',lastName:'Jones',age:10}
]);
self.headers = [
{title:'First Name',sortPropertyName:'firstName', asc: true, active: false},
{title:'Last Name',sortPropertyName:'lastName', asc: true, active: false},
{title:'Age',sortPropertyName:'age', asc: true, active: false}
];
self.filters = [
{title:'Show All', filter: null},
{title:'Only Smith', filter: function(item){return item.lastName == 'Smith';}},
{title:'Only Jones', filter: function(item){return item.lastName == 'Jones';}},
{title:'Only Adults', filter: function(item){return item.age >= 18; }}
];
self.activeSort = ko.observable(function(){return 0;}); //set the default sort
self.sort = function(header, event){
alert(header.title);
//if this header was just clicked a second time
if(header.active) {
header.asc = !header.asc; //toggle the direction of the sort
}
//make sure all other headers are set to inactive
ko.utils.arrayForEach(self.headers, function(item){ item.active = false; } );
//the header that was just clicked is now active
header.active = true;//our now-active header
var prop = header.sortPropertyName;
var ascSort = function(a,b){ return a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
var descSort = function(a,b){ return a[prop] > b[prop] ? -1 : a[prop] < b[prop] ? 1 : a[prop] == b[prop] ? 0 : 0; };
var sortFunc = header.asc ? ascSort : descSort;
...