更新数据时保留和暗示排序顺序?

时间:2013-06-22 03:01:14

标签: javascript knockout.js

更新模型中的数据时,是否可以保留排序顺序(最后的jsfiddle)?我有以下标记:

<table>
  <thead>
    <tr>
      <th data-bind="sort: { arr: Records, prop: 'Name' }">Name</th>
      <th data-bind="sort: { arr: Records, prop: 'Number' }">Number</th>
      <th data-bind="sort: { arr: Records, prop: 'Desc' }">Description</th>
    </tr>
  </thead>
  <tbody data-bind="foreach: Records">
    <tr>
      <td data-bind="text: Name"></td>
      <td data-bind="text: Number"></td>
      <td data-bind="text: Desc"></td>
    </tr>
  </tbody>
</table>
<button data-bind="click: changeMe">Remove</button>
<button data-bind="click: resetMe">Reset</button>

现在,排序本身是使用此original post中的bindingHandlers实现的:

var recs = [];

ko.bindingHandlers.sort = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var asc = false;
        element.style.cursor = 'pointer';

        element.onclick = function(){
            var value = valueAccessor();
            var prop = value.prop;
            var data = value.arr;

            asc = !asc;
            if(asc){
                data.sort(function(left, right){
                    return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
                });
            } else {
                data.sort(function(left, right){
                    return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
                });
            }
        }
    }
};

function ViewModel(){
  var self = this;
  self.Records = ko.observableArray();

    self.changeMe = function() {
        self.Records.pop()
    }

    self.resetMe = function() {
        var recs = []
        recs.push({Name: 'Bob', Number: 1, Desc: 'I am awesome'});
        recs.push({Name: 'Joe', Number: 2, Desc: 'Another description'});
        recs.push({Name: 'Mitch', Number: 3, Desc: 'Something'});
        recs.push({Name: 'Steven', Number: 4, Desc: 'Yo'});
        self.Records(recs);   
    }
}

var viewModel = new ViewModel();
recs.push({Name: 'Bob', Number: 1, Desc: 'I am awesome'});
recs.push({Name: 'Joe', Number: 2, Desc: 'Another description'});
recs.push({Name: 'Mitch', Number: 3, Desc: 'Something'});
recs.push({Name: 'Steven', Number: 4, Desc: 'Yo'});
recs.push({Name: 'Robin', Number: 6, Desc: 'Hello'});
recs.push({Name: 'Batman', Number: 7, Desc: 'Description'});
recs.push({Name: 'John', Number: 5, Desc: 'Ok'});
viewModel.Records(recs);
ko.applyBindings(viewModel);

例如,请考虑此JSFiddle以及以下一系列互动:

  • 单击列以使行按该列排序。单击“数字”两次,以便按降序排序。
  • 现在,点击“重置”来模拟ajax调用(即假设我正在更新模型)。现在,排序顺序将恢复为原始顺序。

如何保存排序顺序,即记住它应按降序排序“Number”这一事实?当然,在列旁边以某种方式显示这一点?类似于datatables的方式(注意每个列名旁边的向上和向下箭头)。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试一试,但应该有更好的方法。

重点是我制作了一个'dataUpdated'标志,因此每次更新数据时,都会再次对它们进行排序。 另外,我在一个可观察的对象中保存了'prop'和'order',这样你也可以在他们改变时做一些事情(例如有一个箭头图标并根据顺序改变它)

检查出来

- &GT; http://jsfiddle.net/blackjim/xgEdg/15/

var recs = [];
ko.bindingHandlers.changed = {
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {

        if(viewModel.dataUpdated()){
            console.log('data updated, sorting...');
            sortThis(viewModel.Records,viewModel.sortData().order,viewModel.sortData().col);
        }
        viewModel.dataUpdated(false);
    }
}

ko.bindingHandlers.sort = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var order = 'none';    //    initial order status for this column

        if(!viewModel.Records.dataSort){
            viewModel.Records.dataSort = { col: 'none', order: 'none' };
        }

        element.style.cursor = 'pointer';
        element.sortAsc = element.sortAsc || false;

        element.onclick = function () {
            var value = valueAccessor(),
                prop = value.prop,
                data = value.arr;

            // sortData updated
            viewModel.sortData({
                col: prop,
                order: order = (order === 'asc' ? 'desc' : 'asc')
            });

            sortThis(data, order, prop);
        }
    }
};

//    function for sorting the observable array
function sortThis(data, order, prop){

    if (order === 'asc') {
        data.sort(function (left, right) {
            return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
        });
    } else {
        data.sort(function (left, right) {
            return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
        });
    }
}

function ViewModel() {
    var self = this;

    self.Records = ko.observableArray();
    self.sortData = ko.observable({
        col: "none",
        order: "none"
    });
    self.dataUpdated = ko.observable(false);    //    flag for data updating

    self.changeMe = function () {
        self.Records.pop();
    }

    self.resetMe = function () {

        self.Records.removeAll();
        self.Records.push({
            Name: 'Bob',
            Number: 1,
            Desc: 'I am awesome'
        });
        self.Records.push({
            Name: 'Joe',
            Number: 2,
            Desc: 'Another description'
        });
        self.Records.push({
            Name: 'Mitch',
            Number: 3,
            Desc: 'Something'
        });
        self.Records.push({
            Name: 'Steven',
            Number: 4,
            Desc: 'Yo'
        });
        //    trigger the dataUpdated 'event'
        self.dataUpdated(true);

    }
}

var viewModel = new ViewModel();

recs.push({
    Name: 'Bob',
    Number: 1,
    Desc: 'I am awesome'
});
recs.push({
    Name: 'Joe',
    Number: 2,
    Desc: 'Another description'
});
recs.push({
    Name: 'Mitch',
    Number: 3,
    Desc: 'Something'
});
recs.push({
    Name: 'Steven',
    Number: 4,
    Desc: 'Yo'
});
recs.push({
    Name: 'Robin',
    Number: 6,
    Desc: 'Hello'
});
recs.push({
    Name: 'Batman',
    Number: 7,
    Desc: 'Description'
});
recs.push({
    Name: 'John',
    Number: 5,
    Desc: 'Ok'
});
viewModel.Records(recs);
ko.applyBindings(viewModel);