KnockoutJS JQuery Combobox Binding

2019-03-30 17:20发布

I'd like to make a binding for knockout which uses the JQuery Autocomplete Combobox and allows for 2 way binding.

http://jsfiddle.net/rniemeyer/PPsRC/ from this question has gotten a start, but doesn't fully implement the combobox functionality as on the jQuery demo site. (ie. selection highlighting, button styling, button not submitting the form, etc.).

2条回答
干净又极端
2楼-- · 2019-03-30 18:01

This is a bit late, but I have a two way autocomplete combo box binding in my Knockout UI library (see Dropdown). Take a look and see if it helps.

Thanks

查看更多
地球回转人心会变
3楼-- · 2019-03-30 18:05

I have used http://harvesthq.github.com/chosen/ in my projects. It works perfectly over standard HTML control SELECT. So I have used standard bindings for managing SELECT (options, value, selectionOptions) and additional custom binding chosen to convert standard control to fancy one.

You could checkout usage example: http://jsfiddle.net/romanych/PcXrP/6/

There is code of binding. It is pretty simple

ko.bindingHandlers.chosen = {
    init: function(elemenet, valueAccessor) {
        var chosenOptions = ko.utils.unwrapObservable(valueAccessor());
        $(elemenet).chosen(chosenOptions);
    },
    update: function(elemenet, valueAccessor, allValuesAccessor) {
        // Subscribe to any change of underlying SELECT-element
        ko.utils.unwrapObservable(allValuesAccessor().value);
        ko.utils.unwrapObservable(allValuesAccessor().options);
        ko.utils.unwrapObservable(allValuesAccessor().selectedOptions);
        $(elemenet).trigger("liszt:updated");
    }
};
查看更多
登录 后发表回答