gpt4 book ai didi

javascript - 更新数据时保留并提示排序顺序?

转载 作者:行者123 更新时间:2023-11-28 09:05:12 25 4
gpt4 key购买 nike

当更新模型内的数据时,是否可以保留排序顺序(最后的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 调用(即假设我正在更新模型)。现在,排序顺序已恢复为原来的顺序。

如何保留排序顺序,即记住它应该按“数字”降序排序?当然,以某种方式在专栏旁边显示这一点?类似于datatables的东西这样做(注意每个列名称旁边的向上和向下箭头)。

有什么建议吗?

最佳答案

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

重点是我做了一个“dataUpdated”标志,所以每次更新数据时,你都会再次对它们进行排序。另外,我将“prop”和“order”保存在一个可观察对象中,这样您也可以在它们更改时执行操作(例如,有一个箭头图标并根据顺序更改它)

检查一下

--> 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);

关于javascript - 更新数据时保留并提示排序顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com