Skip to content

fatfreecrm/ajax-chosen

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

How to Use

This plugin exposes a new jQuery function named ajaxChosen that we call on a select element. The first argument are the options passed to the jQuery $.ajax function. The data parameter should be omitted, and the success callback is 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 object where the key is the HTML option value attribute and the value is the text to display. In other words:

terms[3] = "Ohio";

becomes:

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

Example Code

$("#example-input").ajaxChosen({
	method: 'GET',
	url: '/ajax-chosen/data.php',
	dataType: 'json'
}, function (data) {
	var terms = {};
			
	$.each(data, function (i, val) {
		terms[i] = val;
	});
			
	return terms;
});

About

A complement to the Chosen library that adds ajax autocomplete

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CoffeeScript 75.1%
  • JavaScript 14.7%
  • PHP 10.2%