gpt4 book ai didi

javascript - Knockout.js JqueryUI 自动完成绑定(bind) - 返回对象而不是值

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

我正在使用一个很好的自定义绑定(bind)来填充 JQueryUI 自动完成,但我想自定义它以返回 Item 对象,然后我可以将其推送到不同的数组。有人能阐明如何做到这一点吗?谢谢!

http://jsfiddle.net/rniemeyer/kEdT5/

    <input data-bind="jqAuto: { autoFocus: true }, jqAutoSource: myOptions, jqAutoValue: mySelectedOption, jqAutoSourceLabel: 'name', jqAutoSourceValue: 'id'" />

<hr/>

<div data-bind="text: ko.toJSON(mySelectedOption)"></div>



//jqAuto -- additional options to pass to autocomplete
//jqAutoSource -- the array of choices
//jqAutoValue -- where to write the selected value
//jqAutoSourceLabel -- the property name that should be displayed in the possible choices
//jqAutoSourceValue -- the property name to use for the value
ko.bindingHandlers.jqAuto = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var options = valueAccessor() || {};
var allBindings = allBindingsAccessor();
var unwrap = ko.utils.unwrapObservable;

//handle value changing
var modelValue = allBindings.jqAutoValue;
if (modelValue) {
var handleValueChange = function(event, ui) {
var valueToWrite = ui.item ? ui.item.value : $(element).val();
if (ko.isWriteableObservable(modelValue)) {
modelValue(valueToWrite );

} else { //write to non-observable
if (allBindings['_ko_property_writers'] && allBindings['_ko_property_writers']['jqAutoValue'])
allBindings['_ko_property_writers']['jqAutoValue'](valueToWrite );
}
};

options.change = handleValueChange;
options.select = handleValueChange;
}

//handle the choices being updated in a DO, so the update function doesn't have to do it each time the value is updated
var mappedSource = ko.dependentObservable(function() {
var source = unwrap(allBindings.jqAutoSource);
var valueProp = unwrap(allBindings.jqAutoSourceValue);
var labelProp = unwrap(allBindings.jqAutoSourceLabel) || valueProp;

var mapped = ko.utils.arrayMap(source, function(item) {
var result = {};
result.label = labelProp ? unwrap(item[labelProp]) : unwrap(item).toString(); //show in pop-up choices
result.value = valueProp ? unwrap(item[valueProp]) : unwrap(item).toString(); //value
return result;
});
return mapped;
});

mappedSource.subscribe(function(newValue) {
$(element).autocomplete("option", "source", newValue);
});

options.source = mappedSource();

$(element).autocomplete(options);
},
update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
//update value based on a model change
var allBindings = allBindingsAccessor();
var modelValue = allBindings.jqAutoValue;
if (modelValue) {
$(element).val(ko.utils.unwrapObservable(modelValue));
}
}
};

function Item(id, name) {
return {
id: ko.observable(id),
name: ko.observable(name)
};
}

var viewModel = {
myOptions: ko.observableArray([
new Item("One", "1 - One description"),
new Item("Two", "2 - Two description"),
new Item("Three", "3- Three description"),
new Item("Four", "4- Four description"),
new Item("Five", "5- Five description")
]),
mySelectedOption: ko.observable()
};

ko.applyBindings(viewModel);

最佳答案

这篇文章中有该自动完成绑定(bind)的更新版本:How to create an auto-complete combobox? 。它将输入框中显示的值与写入的值分开。如果您关闭jqAutoSourceValue,那么它将写入整个对象。

这是一个示例:http://jsfiddle.net/rniemeyer/3vqpP/

关于javascript - Knockout.js JqueryUI 自动完成绑定(bind) - 返回对象而不是值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772175/

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