• Stars
    star
    409
  • Rank 105,709 (Top 3 %)
  • Language
    CoffeeScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A complement to the jQuery library Chosen that adds ajax autocomplete

Ajax-Chosen

This project is no longer maintained and is likely extremely out of date with the original chosen library. There are much better alternatives out there now.

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 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>

Note:

Due to a bug in Chosen, it is necessary to change choosen.css.

Add

display: list-item;

to

.chzn-container .chzn-results .group-result {

class

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 cake to see all available commands.

More Repositories

1

CamanJS

Javascript HTML5 (Ca)nvas (Man)ipulation
HTML
3,551
star
2

psd.js

A Photoshop PSD file parser for NodeJS and browsers
CoffeeScript
2,720
star
3

CoffeeDrop

CoffeeDrop is an open-source, roll your own, Dropbox-like clone written in CoffeeScript. Currently not working and under heavy development.
CoffeeScript
509
star
4

flickr-store

Store arbitrary data on Flickr
Ruby
275
star
5

git-lfs-s3

A Git LFS server that uses S3 as the storage backend.
Ruby
216
star
6

CamanJS-Plugins

Plugins for CamanJS, a Javascript image manipulation library
CoffeeScript
195
star
7

RubyDrop

A roll your own Dropbox-like clone written in Ruby.
Ruby
127
star
8

nanocrawler

Web-based front end for viewing information about your Nano node and exploring the Nano network
JavaScript
83
star
9

CamanJS-Site

CamanJS Homepage
JavaScript
66
star
10

node-activerecord

A ORM written in Coffeescript that supports multiple database systems (SQL/NoSQL) and ID generation middleware.
CoffeeScript
58
star
11

JSONP-Fu

A Javascript library that provides access to multiple JSONP API's through a unified, extensible, and easy to use interface.
JavaScript
27
star
12

node-iracing

iRacing SDK bindings for NodeJS
C++
22
star
13

png-encode

Encode/decode arbitrary files using the PNG file format
Ruby
14
star
14

coffeescript-module

A base class for your Coffeescript projects
CoffeeScript
14
star
15

bindata.js

A structured and understandable way to read/write binary data in Javascript. Inspired by Ruby BinData.
CoffeeScript
10
star
16

Osimo-BBCode-Editor

An easy to use jQuery plugin BBCode editor
JavaScript
9
star
17

CamanJ

Java port of the CamanJS image manipulation library
Java
8
star
18

CamanDrop

Easy and automatic image filters using CamanJS, watch folders, and NodeJS.
JavaScript
7
star
19

NodeMonitor

A distributed server monitoring system written in Javascript for Node.JS that uses WebSockets to show server info in near real-time from the browser.
JavaScript
7
star
20

iracing-websocket-server

A WebSocket based server that streams data from iRacing to the browser.
JavaScript
7
star
21

Asyrch

Asynchronous web searching using web workers and ajax
JavaScript
6
star
22

qr-logo

A simple library for customizing a QR code with a logo.
JavaScript
5
star
23

campfire-notifier

A self-hosted Campfire push notification service
Ruby
4
star
24

wiki-philosophy

How many clicks does it take to get from any word to Philosophy on Wikipedia?
JavaScript
4
star
25

nano-ipc-js

Javascript library for interacting with the Nano node IPC
JavaScript
4
star
26

nano-seeds.lol

A take on keys.lol, but for Nano based currencies
JavaScript
4
star
27

MeltingIce-File-System

An online storage service with support for multiple users, Flash file uploading, and advanced user management. Project is no longer under active development.
PHP
4
star
28

Notetakr

An online note-taking application - no longer hosted, code is for experimentation and learning. This was a very old project of mine and is no longer under development.
JavaScript
3
star
29

TransmissionRemote-Pebble

Check on the status of your Transmission torrents from your Pebble smartwatch
JavaScript
3
star
30

meltingice.github.com

Github homepage
JavaScript
3
star
31

Osimo-BBCode-Parser

The BBCode parser made for Osimo Forum System, but can be used for any project.
PHP
3
star
32

node-memcached

A Node.JS memcached interface
3
star
33

Osimo-Forum-System-v2

Osimo is a forum system written from the ground up to be easy to theme and use.
PHP
3
star
34

Caman-C

C port of the Javascript image manipulation library CamanJS
C
3
star
35

nano-grpc

A gRPC server for the Nano cryptocurrency node
JavaScript
2
star
36

jSearch

A jQuery plugin to search an HTML element for a query and highlight the matches. Also, has a iPhone bookmarklet to search the current page and auto search engine detection.
JavaScript
2
star
37

nanocrawler-phoenix

Elixir/Phoenix based version of Nanocrawler, a Nano network explorer
JavaScript
1
star
38

nano-node-statsd

StatsD data collection for Nano currency nodes
JavaScript
1
star
39

cs-terrain-generator

A fractal terrain generator made especially for Cities: Skylines.
JavaScript
1
star
40

nano-ipc-rest

A simple IPC + REST RPC server for the Nano cryptocurrency node.
JavaScript
1
star
41

poopelo

Checks to see if our neighborhood pool is closed.
JavaScript
1
star
42

Osimo-Forum-System-v1

The old version of the Osimo Forum System that has been migrated to GitHub for archival purposes mostly. Please visit the Osimo Version 2 page to see the latest development!
PHP
1
star
43

Ghetto-FBChat

Back before Facebook supported its chat feature on mobile devices, I began to create a simple website that would allow this functionality without using the Facebook API. Chances are this code doesn't even work anymore, so it's here mostly for viewing pleasure.
JavaScript
1
star
44

deviant

An elasticsearch backed exception logging service
Ruby
1
star