Coder Social home page Coder Social logo

gbicou / ajax-chosen Goto Github PK

View Code? Open in Web Editor NEW

This project forked from earnold/ajax-chosen

4.0 3.0 1.0 144 KB

A complement to the jQuery library Chosen that adds ajax autocomplete

Home Page: http://dev.meltingice.net/ajax-chosen

PHP 7.27% CoffeeScript 53.55% JavaScript 39.18%

ajax-chosen's Introduction

Ajax-Chosen

This project is an addition to the excellent Chosen jQuery plugin that makes HTML input forms more friendly. Chosen adds search boxes to select HTML elements. This extends those search boxes to work with ajax requests.

This script bootstraps the existing Chosen plugin without making any modifications to the original code.

How to Use

This plugin exposes a new jQuery function named ajaxChosen that we call on a select element.

The first argument is an option parameter for the plugin specific parameters

ajaxChosen specific parameters are:

  • minLength: this is the number of characters that need to be typed before search occurs. Default is 3.
  • queryLimit: this is the max number of items that your server will ever return, and it is used for client side caching. Default is 10.
  • delay: time to wait between key strokes
  • chosenOptions: passed to vanilla chosen
  • searchingText: "Searching..."
  • noresultsText: "No results."
  • initialQuery: boolean of wether or not to fire a query when the chosen is first focused

The second argument is a callback that the plugin uses to get option elements. The callback is passed the set options, the response callback, and the event that triggered the callback (focus or keyup). The callback is expected to make ajax call, it receives as first argument the options of plugin and must call its second argument to pass the values back to the plugin, e.g. if it were a list of states it would be

states[state.id] = state.name;

which would become

<option value="state.id">state.name</option>

You can use the event parameter to differentiate between initialQuery and subsequent queries to change behavior accordingly

Example Code

$("#example-input").ajaxChosen({
	minLength: 3,
	queryLimit: 10,
	delay: 100,
	chosenOptions: {},
	searchingText: "Searching...",
	noresultsText: "No results.",
	initialQuery: false
}, function (options, response, event) {
    $.getJSON("/ajax-chosen/data.php", {q: options.term}, function (data) {

	    var terms = {};
			
	    $.each(data, function (i, val) {
		    terms[i] = val;
	    });
			
	    response(terms);
    });
});

ajax-chosen's People

Contributors

earnold avatar gbicou avatar meltingice avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jobvite

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.