Skip to content

Commit

Permalink
Fix merge
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Dec 15, 2012
2 parents a18cd42 + 35d9fac commit 8ac190e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 32 deletions.
53 changes: 35 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@ This script bootstraps the existing Chosen plugin without making any modificatio

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 object where the keys are the HTML `option` elements' `value` attributes, and the values are the text to display for each option. In other words:
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:

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

becomes:

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

or for grouping:

{"3": {
[{
group: true,
text: "City",
items: {
"10": "Stockholm",
"23": "Moskow"
}
}
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="City">
<optgroup label="Europe">
<option value="10">Stockholm</option>
<option value="23">Moskow</option>
<option value="23">London</option>
</optgroup>
<optgroup label="Asia">
<option value="36">Beijing</option>
<option value="20">Tokyo</option>
</optgroup>

Note:
Expand Down Expand Up @@ -64,10 +76,10 @@ $("#example-input").ajaxChosen({
url: '/ajax-chosen/data.php',
dataType: 'json'
}, function (data) {
var results = {};
var results = [];

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

return results;
Expand All @@ -81,17 +93,22 @@ $("#example-input").ajaxChosen({
url: '/ajax-chosen/grouped.php',
dataType: 'json'
}, function (data) {
var results = {};
var results = [];

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

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

results.push(group);
});

return results;
});

Expand Down
27 changes: 22 additions & 5 deletions lib/ajax-chosen.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/ajax-chosen.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions src/ajax-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,37 @@ do ($ = jQuery) ->

# Iterate through the given data and inject the <option> elements into
# the DOM if it doesn't exist in the selector already
$.each items, (value, element) ->
$.each items, (i, element) ->
if element.group
group = select.find("optgroup[label='#{element.text}']")
group = $("<optgroup />") unless group.size()

group.attr('label', element.text)
.appendTo(select)
$.each element.items, (value, text) ->
$.each element.items, (i, element) ->
if typeof element == "string"
value = i;
text = element;
else
value = element.value;
text = element.text;
if $.inArray(value + "-" + text, selected_values) == -1
$("<option />")
.attr('value', value)
.html(text)
.appendTo(group)
else if $.inArray(value + "-" + element, selected_values) == -1
$("<option />")
.attr('value', value)
.html(element)
.appendTo(select)
else
if typeof element == "string"
value = i;
text = element;
else
value = element.value;
text = element.text;
if $.inArray(value + "-" + text, selected_values) == -1
$("<option />")
.attr('value', value)
.html(text)
.appendTo(select)

if Object.keys(items).length
# Tell chosen that the contents of the <select> input have been updated
Expand Down

0 comments on commit 8ac190e

Please sign in to comment.