我在淘汰 js 并将可观察数组显示为列表时遇到问题;在 beforeRemove 动画运行时添加项目时,被移除的元素会移动到列表的底部,而不是停留在其位置,直到动画完成并移除元素。
这是一个更好地解释问题的jsfiddle:http: //jsfiddle.net/bPP5Q/8/
有谁知道我怎么能解决这个问题?
javascript:
jQuery(function ($) {
var ViewModel = function (data) {
var self = this;
self.data = ko.observableArray(data);
self.removeLine = function (elem) {
if (elem.nodeType === 1) $(elem).fadeOut(3000, function () {
$(elem).remove();
});
}
self.addLine = function (elem) {
if (elem.nodeType === 1)
$(elem).hide().fadeIn(3000);
}
self.removeItem = function() {
self.data.remove(function(item) { return item.test && item.test == 2; });
}
self.addItem = function() {
self.data.splice(1, 0, { test: 9 });
}
self.addremove = function () {
self.removeItem();
var id = setInterval(function() {
self.addItem();
clearInterval(id);
},1000);
}
}
var vm = new ViewModel([{ test: 9 }, { test: 2 }, { test: 1 }, { test: 1 }, { test: 1 }, { test: 1 }, { test: 1 }]);
ko.applyBindings(vm);
});
HTML:
<button id="button" data-bind="click: addremove">Click</button>
<table id="grid">
<tbody data-bind='template: { foreach: data, afterAdd: addLine, beforeRemove: removeLine }'>
<tr>
<td data-bind="text: test"></td>
<td>
</td>
</tr>
</tbody>
</table>