rniemeyer / knockout-jqAutocomplete

knockout-jqAutocomplete is a Knockout.js plugin designed to work with jQuery UI's autocomplete widget.
MIT License
49 stars 20 forks source link

knockout-jqAutocomplete

knockout-jqAutocomplete is a Knockout.js plugin designed to work with jQuery UI's autocomplete widget.

Options

Global options

Basic Usage

Samples in jsFiddle: http://jsfiddle.net/rniemeyer/uGGb8/

var viewModel = {
    myValue: ko.observable(),
    myOptions: ["one", "two", "three"]
};
<input data-bind="jqAuto: { value: myValue, source: myOptions }" />
var viewModel = {
    myValue: ko.observable(),
    myOptions: [
        {
            id: 1,
            name: "one",
            description: "one description"
        },
        {
            id: 2,
            name: "two",
            description: "two description"
        },
        {
            id: 3,
            name: "three",
            description: "three description"
        }
    ]
};
<input data-bind="jqAuto: { value: myValue, source: myOptions, inputProp: 'name', labelProp: 'description', valueProp: 'id' }" />
var viewModel = {
    myValue: ko.observable(),
    myOptions: [
        {
            id: 1,
            name: "one",
            description: "one description"
        },
        {
            id: 2,
            name: "two",
            description: "two description"
        },
        {
            id: 3,
            name: "three",
            description: "three description"
        }
    ],
    myFilter: function(item, searchTerm) {
        var searchString = item.name + " " + item.description;
        return searchString.indexOf(searchTerm) > -1;
    }
};
<input data-bind="jqAuto: { value: myValue, source: myOptions, filter: myFilter, labelProp: 'name' }" />
var viewModel = {
    myValue: ko.observable(),
    getOptions: function(searchTerm, callback) {
        $.ajax({
          dataType: "json",
          url: "/mysearch",
          data: {
            query: searchTerm
          },
        }).done(callback);
    }
};
<input data-bind="jqAuto: { value: myValue, source: getOptions }" />
var viewModel = {
    myValue: ko.observable(),
    getOptions: function(searchTerm, callback) {
        $.ajax({
          dataType: "json",
          url: "/mysearch",
          data: {
            query: searchTerm
          },
        }).done(callback);
    }
};
<input data-bind="jqAuto: { value: myValue, source: getOptions, inputProp: 'name', labelProp: 'description', valueProp: 'id' }" />
var viewModel = {
    myValue: ko.observable(),
    myOptions: [
        {
            id: 1,
            name: "one",
            description: "one description"
        },
        {
            id: 2,
            name: "two",
            description: "two description"
        },
        {
            id: 3,
            name: "three",
            description: "three description"
        }
    ]
};
<input data-bind="jqAuto: { value: myValue, source: myOptions, inputProp: 'name', labelProp: 'description', valueProp: 'id', template: 'rowTmpl' }" />

<script id="rowTmpl" type="text/html">
    <a>
        <span data-bind="text: id"></span>:
        <span data-bind="text: name"></span>
        ( <span data-bind="text: description"></span> )
    </a>
</script>

Future Plans

Dependencies

Build: This project uses grunt for building/minifying.

License: MIT http://www.opensource.org/licenses/mit-license.php