Knockout可排序绑定顺序 [英] Knockout Sortable bind Order
问题描述
这是一个问题的后续问题: KnockoutJS Sortable group observableArray by field and conditionally sort
This is a follow up to a question: KnockoutJS Sortable group observableArray by field and conditionally sort
还有两件事我正在努力实现。首先,当嵌套列表项被拖动到另一个列表而父项为空时,我想删除父项。我通过创建一个afterMove函数并检查sourceParent长度是否为0来完成此操作。然后我查看路由以查看是否有任何空任务数组并相应地删除。我想知道这是否是一个有效的解决方案。我还必须删除任务,否则任务订阅会在删除计划任务时添加重复项。
There are two further things I am battling to achieve. First when a nested list item is dragged to another list leaving the parent empty I would like to remove the parent. I have done this by creating an afterMove function and checking if the sourceParent length is 0. Then I look through the routes to see if any have an empty tasks array and remove accordingly. I would like to know if this is an efficient solution. I also have to remove the tasks otherwise the tasks subscription adds duplicates when removing scheduled tasks.
其次,最重要的是基础对象(Task)具有订单属性。我想将此绑定到可排序的顺序,以便在Scheduled列表中拖动任务时更新order属性。如何从可排序的回调中访问其他列表项的顺序?
Second and most importantly the base object (Task) has an order property. I would like to bind this to the sortable order so that when the tasks are dragged around the Scheduled list the order property is updated. How do I access the order of other list items from the sortable callback?
请参阅以下小提琴。
非常感谢任何帮助。
推荐答案
对于第一个问题,您可以采用稍微优化的方式。 arg.sourceParent
包含在您要删除的 tasksByRoute
项目中。 remove
函数可以使用函数来对项目运行。所以,你可以这样写:
For the first issue, there is a slightly optimized way that you could do it. arg.sourceParent
is contained in the tasksByRoute
item that you want to remove. The remove
function can take a function to run against the items. So, you could write it like:
self.afterMoveCallback = function(arg) {
if (arg.sourceParent().length === 0) {
self.tasksByRoute.remove(function(route) {
return route.tasks === arg.sourceParent;
});
}
};
对于第二个问题,我真的想使用一个会自动跟踪observableArray中的顺序的扩展名。它看起来像:
For the second issue, I really like to use an extension that will automatically track the order in an observableArray. It would look something like:
//track an index on items in an observableArray
ko.observableArray.fn.indexed = function(prop) {
prop = prop || 'index';
//whenever the array changes, make one loop to update the index on each
this.subscribe(function(newValue) {
if (newValue) {
var item;
for (var i = 0, j = newValue.length; i < j; i++) {
item = newValue[i];
if (!ko.isObservable(item[prop])) {
item[prop] = ko.observable();
}
item[prop](i); //add 1 here if you don't want it to be zero based
}
}
});
//initialize the index
this.valueHasMutated();
return this;
};
在您的情况下,您可以使用它:
in your case, you would use it like:
self.scheduledTasks = ko.observableArray([
new Task(8, 4, "Route 4", "Cust 8", 1),
new Task(9, 4, "Route 4", "Cust 9", 2),
new Task(10, 5, "Route 5", "Cust 10")
]).indexed("order");
以下是更新这两项更改的示例: http://jsfiddle.net/rniemeyer/usVKQ/
Here is a sample updated with both of these changes: http://jsfiddle.net/rniemeyer/usVKQ/
这篇关于Knockout可排序绑定顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!