gpt4 book ai didi

javascript - jquery排序后获取底层对象

转载 作者:行者123 更新时间:2023-11-28 02:13:28 24 4
gpt4 key购买 nike

我可以获取使用以下 JS Bin 代码移动的 UI 元素,但不能获取 JS 对象。

http://jsbin.com/edopuh/14/edit

ko.bindingHandlers.Sortable = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = ko.utils.unwrapObservable(valueAccessor() || {});
$(element).sortable({
items: 'li:not(".notsortable")',
update: function(event, ui){
alert("you just moved " + $(ui.item[0]).text());
}
});
}
};

移动后,如何获取 Person 对象列表和新位置?

最佳答案

正如您在评论中指出的那样,您希望在移动后将每个人的位置保存在可排序中。为此,您需要访问附加到正在移动的 DOM 元素的模型。 Knockout 提供了一个实用函数来访问与名为 ko.dataFor([elem]) 的元素关联的 View 模型。 .

为了获取移动元素的索引,您只需调用 jQuery 的 .index()作用于移动的项目。

我在这里更新了您的示例,以演示如何根据您的情况结合使用两者:http://jsbin.com/edopuh/27/edit

ko.bindingHandlers.Sortable = {
init: function (element, valueAccessor, allBindingsAccessor) {
var options = ko.utils.unwrapObservable(valueAccessor() || {});
$(element).sortable({
items: 'li:not(".notsortable")',
update: function(event, ui){
var $item = $(ui.item[0]),
person = ko.dataFor(ui.item[0]);

alert("you just moved " + $item.text());

person.position = $item.index();
}
});
}
};

关于javascript - jquery排序后获取底层对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741280/

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