我正在研究淘汰赛绑定..我有淘汰赛排序来实现,我已经这样做了..
function notesViewModel() {
_this.colName = "CreatedDate";
_this.sortOrder = "desc";
_this.notes = ko.observableArray();
_this.SortItems = function (colname) {
var newNotes = _this.notes();
if (_this.sortOrder === "desc") {
this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
return a[colname] < b[colname] ? -1 : 1;
}));
switchSortOrder();
} else {
this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
return a[colname] > b[colname] ? -1 : 1;
}));
switchSortOrder();
}
};
function switchSortOrder() {
if (_this.sortOrder === "asc") {
_this.sortOrder = "desc";
} else {
_this.sortOrder = "asc";
}
}
ko.applyBindings(_this, $("body").get(0));
return _this;
}
我的html代码是这样的:
<table id="notes" class="notes_table">
<tr class="head">
<th data-bind='click: function() { SortItems("CreatedDate")}'>
<span>Date</span>
</th>
<th data-bind='click: function() { SortItems("Type")}'>
<span>Type</span>
</th>
<th data-bind='click: function() { SortItems("Category")}'>
<span>Category</span>
</th>
<th data-bind='click: function() {SortItems("AddedBy")}'>
<span>Added by</span>
</th>
<th>
<span>Alerts</span>
</th>
<th></th>
</tr>
<tbody data-bind="template: { name: 'StudentNote', foreach: notes }"></tbody>
</table>
我想将排序功能重构为这样的东西..
_
this.SortItems = function (colname) {
var newNotes = _this.notes();
this.notes(newNotes.sort(notesViewModel._getSortFunction = compareFunction (a, b,_this.sortOrder)));
function comparerFunction(a,b,sortOrder)
{
if(sortOrder === "desc")
{
return a[colname] < b[colname] ? -1 : 1;
_this.sortOrder = "asc";
}
else
{
return a[colname] > b[colname] ? -1 : 1;
_this.sortOrder = "desc";
}
}
};
但它没有成功,因为它说 a 和 b 参数不可用.. 谁能告诉我如何将逻辑与匿名函数分离