dynamicdropdown-aromn
Version:
This is a simple plugin that allows a dropdown be feeded by json data from URL get response.
48 lines (34 loc) • 1.5 kB
Markdown
# dynamicdropdown-jquery
## a simple jquery plugin to feed a dropdown with json data.
## dependencies:
* jquery
* bootstrap(not necessary but make it looks better).
## instructions
1. Clone repository:
```
$ git clone https://github.com/aromn/dynamicdropdown-jquery.git
```
2. Call the plugin in your HTML select dropdown:
```
<select class="form-control" id="myDropdown"></select>
$('#myDropdown').dynamicDropdown({
url: ''http://your-url/'
});
```
Optionally you can add **form-control** class from bootstrap, or the plugin will add it by itself.
3. Modify the getJSON function inside to play around with the data
```
// AJAX call to the url provided in the plugin's settings.
$.getJSON(settings.url, function(result) {
// If the returned data is a JSON object, it proceeds to build the dropdown
// IMPORTANT: It relies on the structure of the JSON object returned.
$.each(result, function(i, value) {
// Appending data to the dropdown referenced by the variable this_obj
this_obj.append('<option>' + result.something + ' - ' + result.somethingelse + '</option>');
});
// If the call to the URL don't return a json object, it returns an error.
}).fail(function() {
console.log('%c ERROR: ' + 'Response data from ' + settings.url + 'is not a JSON object',
'background: red');
})
```