Git Product home page Git Product logo

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, so I felt it could use the addition of ajax autocomplete for awesomely dynamic forms.

This script bootstraps the existing Chosen plugin without making any modifications to the original code. Eventually, I would love to see this functionality built-in to the library, but until then, this seems to work pretty well.

Note about this fork

This fork has been made as PR were no more merged into the main repository.

It introduces the following new features:

  • Compatibility for Chosen 1.x
  • A new plugin structure closer to the default jQuery plugin structure
  • Bower support
  • Updated documentation

Installation

Manually

Download either the minified version or the full version.

Using Bower

bower install ajax-chosen

How to Use

This plugin exposes a new jQuery function named ajaxChosen that we call on a select element. The first argument consists of the options passed to the jQuery $.ajax function. The data parameter is optional, and the success callback is also optional.

The second argument is a callback that tells the plugin what HTML option elements to make. It is passed the data returned from the ajax call, and you have to return an array of objects for which each item has a value property corresponding to the HTML option elements' value attribute, and a text property corresponding to the text to display for each option. In other words:

[{"value": 3, "text": "Ohio"}]

becomes:

<option value="3">Ohio</option>

or for grouping:

[{
	group: true,
	text: "Europe",
	items: [
		{ "value": "10", "text": "Stockholm" },
		{ "value": "23", "text": "London" }
	]
},
{
	group: true,
	text: "Asia",
	items: [
		{ "value": "36", "text": "Beijing" },
		{ "value": "20", "text": "Tokyo" }
	]
}]

becomes:

    <optgroup label="Europe">
        <option value="10">Stockholm</option>
        <option value="23">London</option>
    </optgroup>
    <optgroup label="Asia">
        <option value="36">Beijing</option>
        <option value="20">Tokyo</option>
    </optgroup>

Options

There are some additional ajax-chosen specific options you can pass into the first argument to control its behavior.

  • minTermLength: minimum number of characters that must be typed before an ajax call is fired
  • afterTypeDelay: how many milliseconds to wait after typing stops to fire the ajax call
  • jsonTermKey: the ajax request key to use for the search query (defaults to term)

Example Code

$("#example-input").ajaxChosen({
	type: 'GET',
	url: '/ajax-chosen/data.php',
	dataType: 'json'
}, function (data) {
	var results = [];

	$.each(data, function (i, val) {
		results.push({ value: val.value, text: val.text });
	});

	return results;
});

To have the results grouped in optgroup elements, have the function return a list of group objects instead:

$("#example-input").ajaxChosen({
	type: 'GET',
	url: '/ajax-chosen/grouped.php',
	dataType: 'json'
}, function (data) {
	var results = [];

	$.each(data, function (i, val) {
		var group = { // here's a group object:
			group: true,
			text: val.name, // label for the group
			items: [] // individual options within the group
		};

		$.each(val.items, function (i1, val1) {
			group.items.push({value: val1.value, text: val1.text});
		});

		results.push(group);
	});

	return results;
});

Developing ajax-chosen

In order to install development dependencies, you can run in the ajax-chosen directory:

npm install -d

ajax-chosen is written in Coffeescript, so there is a Cakefile provided that will perform all necessary tasks for you. Simply run npm run build to see all available commands.

ajax-chosen's People

Contributors

apmem avatar benlumley avatar cvrebert avatar digidanny avatar elkelk avatar erickzanardo avatar fredwu avatar jhanggi avatar lingceng avatar meltingice avatar michaelperrin avatar mikejolley avatar sduck avatar shadyvb avatar vad4msiu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ajax-chosen's Issues

Change package name in package.json

Can you please change the package name in package.json. It's impossible to install your package through yarn or npm.

The version of meltingice no longer works.

Example for optgroup functionality is incorrect

You're missing required quotes in several places, and the optgroup needs to be called "name", not "text" (or the sample script needs to be changed to read text: val.text instead of text: val.name)

[{
"group": true,
"name": "Europe",
"items": [
{ "value": "10", "text": "Stockholm" },
{ "value": "23", "text": "London" }
]
},
{
"group": true,
"name": "Asia",
"items": [
{ "value": "36", "text": "Beijing" },
{ "value": "20", "text": "Tokyo" }
]
}]

Issues on mobile devices

I cannot use the plugin with chrome on my mobile phone. I activated the chosen plugin by removing the check for android/iphone. Now chosen works perfectly. However, I see the following behavior of ajax-chosen:

  • Opening a field with ajax-chosen does not show values.
  • Now I start typing, four letters, for example. The values do not get loaded from the server.
  • I close the field and then I see in the server log that the values are loaded.
  • Now I open the field again and I can see the loaded values.

Thanks for any help! Ralf

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.