npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knockout-jqautocomplete

v0.5.0

Published

knockout-jqAutocomplete ================ *knockout-jqAutocomplete* is a [Knockout.js](http://knockoutjs.com/) plugin designed to work with jQuery UI's [autocomplete widget](http://jqueryui.com/autocomplete/).

Downloads

344

Readme

knockout-jqAutocomplete

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

Options

  • value - an observable that holds the current value

  • source - either a local array (or observableArray) or a function that retrieves an array of options. This function will receive the search term as the first argument and a callback as the second argument that should be called with the array of data to use. This can be called after requesting data via AJAX.

  • inputProp - If specified, this is a property name to use for populating the input box after making a selection. If not specified, this will fallback to the labelProp, then the valueProp.

  • labelProp - If specified, this is a property name to use for populating the menu choices that appear for a user to select. For example, you may want to show the name of an item in the input, but display the description in the list presented to the user. If not specified, this will fallback to the valueProp.

  • valueProp - If specified, this is a property name to use to populate the value when a selection is made. If not specified, the actual item itself will be used as the value.

  • dataValue - If specified, this observable will be populated with the currently selected data item. This option will allow you to populate value with the valueProp and have access to the selected object at the same time through dataValue.

  • template - If specified, this is the name of a template to use when building each item in the menu choices. This allows full customization of the options shown to the user.

  • options - Anything passed in options will be included as options passed to the autocomplete widget. See http://api.jqueryui.com/autocomplete/ for a list of the available options.

  • filter - If specified, this function is used to search local data. The function takes in an item and the search term as arguments and should return whether the item matches. The default filter will call ko.toJSON on each item and determine if the search term is contained anywhere in the JSON string. When a function is used for the source (remote data), then the data is not filtered, as the response should already be appropriately filtered.

Global options

  • ko.bindingHandlers.jqAuto.options - this object contains any global options that should be passed into the autocomplete widget. Options specified in individual bindings will override these values.

Basic Usage

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

  • with a local array of strings
var viewModel = {
    myValue: ko.observable(),
    myOptions: ["one", "two", "three"]
};
<input data-bind="jqAuto: { value: myValue, source: myOptions }" />
  • with a local array of objects
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' }" />
  • with a local array of objects and custom filtering
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' }" />
  • with a remote array of strings
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 }" />
  • with a remote array of objects
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' }" />
  • using a template for the menu items
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

  • options to validate value when not making a selection
  • if local observableArray changes, re-validate value

Dependencies

  • Knockout 2.0+
  • jQuery - no specific version identified as minimum
  • jQuery UI - no specific version identified as minimum

Build: This project uses grunt for building/minifying.

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