I'm trying to create an html table grid with server side sorting and paging using knockout.
I based my work on knockout simpleGrid example.
So far it work but I have a problem to bind css class to show which column is selected for sorting.
Here is my code :
html :
<thead>
<tr data-bind="foreach: columns">
<th data-bind="text: headerText, click: $parent.sortBy, css: $parent.sortClass($data)"></th>
</tr>
</thead>
knockout class :
viewModel: function (configuration) {
...
// Sort properties
this.sortProperty = configuration.sortProperty;
this.sortDirection = configuration.sortDirection;
...
this.sortClass = function (data) {
return data.propertyName == this.sortProperty() ? this.sortDirection() == 'ascending' ? 'sorting_asc' : 'sorting_desc' : 'sorting';
};
}
My main knockout class :
this.gridViewModel = new ko.simpleGrid.viewModel({
data: self.items,
pageSize: self.itemsPerPages,
totalItems: self.totalItems,
currentPage: self.currentPage,
sortProperty: self.sortProperty,
sortDirection: self.sortDirection,
columns: [
new ko.simpleGrid.columnModel({ headerText: "Name", rowText: "LastName", propertyName: "LastName" }),
new ko.simpleGrid.columnModel({ headerText: "Date", rowText: "EnrollmentDate", propertyName: "EnrollmentDate" })
],
sortBy: function (data) {
data.direction = data.direction == 'ascending' ? 'descending' : 'ascending';
self.sortProperty = data.propertyName;
self.sortDirection = data.direction;
// Server call
$.getGridData({
url: apiUrl,
start: self.itemStart,
limit: self.itemsPerPages,
column: data.propertyName,
direction: data.direction,
success: function (studentsJson) {
// data binding
self.items(studentsJson.gridData);
}
});
},
}
With this, the first time data are bind my css class is correctly apply. But when I click on a column the css class won't update.
Do you have an idea of what I'm doing wrong ?