Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用模板对表格进行排序_Javascript_Knockout.js - Fatal编程技术网

Javascript 使用模板对表格进行排序

Javascript 使用模板对表格进行排序,javascript,knockout.js,Javascript,Knockout.js,我在带有列的表上有敲除绑定。我试图为每一列实现表排序。 视图看起来像: <table id="notes" class="notes_table"> <tr class="head"> <th data-bind='click: function() { SortItems("CreatedDate")}'>

我在带有列的表上有敲除绑定。我试图为每一列实现表排序。 视图看起来像:

<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>
                             </tr>
                            <tbody data-bind="template: { name: 'StudentNote', foreach: notes }"></tbody>
                        </table>
                        <script type="text/html" id="StudentNote">
                            <tr class="even">
                                <td><span data-bind="text: CreatedDate"></span></td>
                                <td><span data-bind="text: Type"></span></td>
                                <td><span data-bind="text: Category"></span></td>
                                <td><span data-bind="text: AddedBy"></span></td>
                            </tr>
                        </script>

日期
类型
类别
由添加
javascript就像:

function notesViewModel() {
    var _this = {};
    _this.colName = "CreatedDate";
    _this.sortOrder = "desc";
    _this.notes = ko.observableArray();
 _this.SortItems = function (ColumnName) {

        var newNotes = _this.notes();
        if (_this.sortOrder === "desc") {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                _this.sortOrder = "asc";
                return a[ColumnName] < b[ColumnName] ? -1 : 1;
            }));

        } else {
            this.notes(newNotes.sort(notesViewModel._getSortFunction = function (a, b) {
                _this.sortOrder = "desc";
                return a[ColumnName] > b[ColumnName] ? -1 : 1;
            }));
        }
    };
 ko.applyBindings(_this, $("body").get(0));
 return _this;
函数notesViewModel(){
var_this={};
_this.colName=“CreatedDate”;
_this.sortOrder=“desc”;
_this.notes=ko.observearray();
_this.SortItems=函数(ColumnName){
var newNotes=_this.notes();
如果(_this.sortOrder==“desc”){
this.notes(newNotes.sort(notesViewModel.\u getSortFunction=function(a,b){
_this.sortOrder=“asc”;
返回a[ColumnName]b[ColumnName]?-1:1;
}));
}
};
ko.applyBindings(_this,$(“body”).get(0));
把这个还给你;
即使它进行排序,它也只是在每列的升序和降序排序之间切换,但无法识别它正在排序的列..如何按每列进行排序..

尝试以下操作:

function notesViewModel() {
    var _this = {};
    _this.colName = "CreatedDate";
    _this.sortOrder = 1;
    _this.notes = ko.observableArray();
    _this.SortItems = function (ColumnName) {
        if(ColumnName == _this.colName)
            _this.sortOrder = _this.sortOrder * -1;
        else
            _this.colName = ColumnName;

        _this.notes.sort(function (a, b) {
            return (a[ColumnName] < b[ColumnName] ? -1 : 1) * _this.sortOrder;
        });
    };
    ko.applyBindings(_this, $("body").get(0));
    return _this;
}
函数notesViewModel(){
var_this={};
_this.colName=“CreatedDate”;
_这个.sortOrder=1;
_this.notes=ko.observearray();
_this.SortItems=函数(ColumnName){
if(ColumnName==\u this.colName)
_this.sortOrder=\u this.sortOrder*-1;
其他的
_this.colName=ColumnName;
_this.notes.sort(函数(a,b){
返回(a[ColumnName]
谢谢您的回复。但是,如果我单击一列并将其升序排序,然后单击另一列,则将后者排序为降序(意味着在升序和降序之间切换,而不考虑列)…我希望在第二列中也看到升序。我已调整代码,以便在您更改列时保留排序顺序。我已再次调整代码,以满足我猜测的是您的要求。您可能希望在原始问题中添加更多注释,以进一步明确您希望排序如何工作。