简体   繁体   English

使用Knockout JS无法从jquery自动完成中选择/绑定项目

[英]Not able to select / bind item from jquery autocomplete using Knockout JS

After I finally was able to add and remove items from a list ( see fiddle ), I moved on to implement jQuery Autocomplete. 在我最终能够从列表中添加和删除项目之后( 请参见小提琴 ),我继续实现jQuery Autocomplete。

After looking at Rune's example and Rene's example , I am able to trigger Autocomplete. 看完Rune的示例Rene的示例后 ,我就能触发自动完成功能。 But when I select an item, the values are not added to my hidden input and my search box. 但是,当我选择一个项目时,这些值不会添加到我的隐藏输入和搜索框中。

My current solution follows Rune's example, but I might simplyfy it and follow Rene's example once I'm able to bind the selected item. 我当前的解决方案遵循Rune的示例,但是我可以简单地将其绑定,并在能够绑定所选项目后遵循Rene的示例。

So my question is: How can I bind the selected items? 所以我的问题是:如何绑定所选项目? I've created a Fiddle to test. 我创建了一个小提琴来测试。

PS. PS。 I'm having issues in the Fiddle when putting search data in array (instead of getting from DB). 将搜索数据放入数组(而不是从数据库获取)时,小提琴琴出现了问题。 But that is not my main problem. 但这不是我的主要问题。

Here is the part of the code regarding Autocomplete: 这是有关自动完成功能的代码部分:

My HTML: 我的HTML:

<input type="hidden" value="" data-bind="value: item_id" />
<input class="form-control" type="search" data-bind="value: item_name, autoComplete: items" />     
...
<ul class="list-group" data-bind="template: { name: 'item-template', data: $root.items}">
...
</ul>

My JS; 我的JS;

// Retrieved from DB
var search_data = [
    {"id":"7186","name":"Bose"},
    {"id":"1069","name":"BOSS Black"},
    {"id":"1070","name":"BOSS Green"},
    {"id":"1071","name":"BOSS Hugo Boss"},
    {"id":"1072","name":"BOSS Orange"},
    {"id":"1074","name":"Boston Brothers"},
    {"id":"7678","name":"Bosweel"}
];

ko.bindingHandlers.autoComplete = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var selectedObservableArrayInViewModel = valueAccessor();
        var self = bindingContext;
        self.item_id = ko.observable();
        self.item_name = ko.observable();    

        $(element).autocomplete({
            minLength: 2,
            source: function (request, response) {
                $.ajax({
                    source: search_data,
                    data: { term: request.term },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        response(data);
                    }
                });
            },
            select: function(e, ui){
                var item = ui.item;
                self.item_name = ko.observable(item.name);
                self.item_id = ko.observable(item.id);
            }
        }).data( "uiAutocomplete" )._renderItem = function( ul, item ) {
            return jQuery( "<li></li>" )
            .data( "ui-autocomplete", item )
            .append( "<a>"+ item.name + "</a>" )
            .appendTo( ul );
        }

    }
};

I am not sure I understand your question, so I am not sure this answers it. 我不确定我是否理解您的问题,因此不确定是否能回答您的问题。

First, you need to replace this 首先,您需要替换此

var self = bindingContext;

With this 有了这个

var self = viewModel;

Then, in your select function, don't re-create your observables, update them and then call your addItem function: 然后,在您的select函数中,不要重新创建您的可观察对象,更新它们,然后调用您的addItem函数:

select: function(e, ui){
            var item = ui.item;
            self.item_name(item.name);
            self.item_id(item.id);
            self.addItem();
        }

Updated fiddle (I removed your ajax call to show all items without filtering, just for the demo -- ajax call was failing) 更新了小提琴 (我删除了您的ajax调用,以显示所有项目而不进行过滤,仅用于演示-ajax调用失败)

EDIT: 编辑:

In the previous fiddle, the input is cleared after the selection of an item. 在上一个小提琴中,选择项目后清除输入。

Add return false; 添加return false; to the end of the select function to avoid that. select函数的末尾可以避免这种情况。

New fiddle 提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM