Reputation: 480
my English is not good. sorry.
and I do not know Json and Ajax.
I want add More than two item to Select2 Cascade.
I created one more select/option(#model) and It does not work.
I created three files in the json folder: animal.json , Vehicles.json , horse.json
animal.json and Vehicles.json files run and executed, but horse.json file is not run and executed.
I want the third select/option(#model) to prop and open, when I select the Horse.
I saw the following page but I do not understand it. Can you modify or create my code here? http://ajaxray.com/blog/select2-dependent-cascading-select-list-reload/
My Json files is as follows:
{
"201" : "Horse",
"202" : "Elephant",
"203" : "Donkey",
"204" : "Camel",
"204" : "Tiger"
}
var Select2Cascade = ( function(window, $) {
function Select2Cascade(parent, child, url, select2Options) {
var afterActions = [];
var options = select2Options || {};
// Register functions to be called after cascading data loading done
this.then = function(callback) {
afterActions.push(callback);
return this;
};
parent.select2(select2Options).on("change", function (e) {
child.prop("disabled", true);
var _this = this;
$.getJSON(url.replace(':parentId:', $(this).val()), function(items) {
var newOptions = '<option value="">-- Select --</option>';
for(var id in items) {
newOptions += '<option value="'+ id +'">'+ items[id] +'</option>';
}
child.select2('destroy').html(newOptions).prop("disabled", false)
.select2(options);
afterActions.forEach(function (callback) {
callback(parent, child, items);
});
});
});
}
return Select2Cascade;
})( window, $);
$(document).ready(function() {
var select2Options = { width: 'resolve' };
var apiUrl = 'json/:parentId:.json';
$('select').select2(select2Options);
var cascadLoading = new Select2Cascade($('#type'), $('#subtype'), apiUrl, select2Options);
cascadLoading.then( function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
var cascadLoading = new Select2Cascade($('#subtype'), $('#model'), apiUrl, select2Options);
cascadLoading.then( function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
$('#subtype').prop('disabled', true);
$('#model').prop('disabled', true);
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select2</title>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-css/1.4.6/select2-bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<form class="form-horizontal">
<div class="form-group">
<label for="type" class="col-sm-5 control-label">I'd like to ride</label>
<div class="col-sm-5">
<select name="type" id="type" class="form-control">
<option>--Select your ride--</option>
<option value="animals">Animal</option>
<option value="vehicles">Vehicle</option>
</select>
</div>
</div>
<div class="form-group">
<label for="subtype" class="col-sm-5 control-label">More specifically</label>
<div class="col-sm-5">
<select name="subtype" id="subtype" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
<div class="form-group">
<label for="model" class="col-sm-5 control-label">More specifically</label>
<div class="col-sm-5">
<select name="model" id="model" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
</form>
</body></html>
Upvotes: 1
Views: 1587
Reputation: 9321
Here is changed code. Based on assumption you need third item. Generally id
should always unique whenever there is duplication of id
in code its doesn't work for second one. You have to keep different id
or modify code using class
var Select2Cascade = (function(window, $) {
function Select2Cascade(parent, child, url, select2Options) {
var afterActions = [];
var options = select2Options || {};
// Register functions to be called after cascading data loading done
this.then = function(callback) {
afterActions.push(callback);
return this;
};
parent.select2(select2Options).on("change", function(e) {
child.prop("disabled", true);
var _this = this;
$.getJSON(url.replace(':parentId:', $(this).val()), function(items) {
var newOptions = '<option value="">-- Select --</option>';
for (var id in items) {
newOptions += '<option value="' + id + '">' + items[id] + '</option>';
}
child.select2('destroy').html(newOptions).prop("disabled", false)
.select2(options);
afterActions.forEach(function(callback) {
callback(parent, child, items);
});
});
});
}
return Select2Cascade;
})(window, $);
$(document).ready(function() {
var select2Options = {
width: 'resolve'
};
var apiUrl = 'https://gist.githubusercontent.com/ajaxray/32c5a57fafc3f6bc4c430153d66a55f5/raw/260a653e6347fb6d2360e8ec376a2dc4888c1afa/:parentId:.json';
$('select').select2(select2Options);
var cascadLoading = new Select2Cascade($('#type'), $('#subtype'), apiUrl, select2Options);
cascadLoading.then(function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
var cascadLoading = new Select2Cascade($('#subtype'), $('#model'), apiUrl, select2Options);
cascadLoading.then(function(parent, child, items) {
// Open the child listbox immediately
child.select2('open');
// Dump response data
console.log(items);
});
var cascadLoading1 = new Select2Cascade($('#type'), $('#subtype_1'), apiUrl, select2Options);
cascadLoading1.then(function(parent, child, items) {
// Dump response data
//console.log(items);
});
$('#subtype').prop('disabled', true);
$('#subtype_1').prop('disabled', true);
$('#model').prop('disabled', true);
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Select2</title>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2-bootstrap-css/1.4.6/select2-bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<form class="form-horizontal">
<div class="form-group">
<label for="type" class="col-sm-5 control-label">I'd like to ride</label>
<div class="col-sm-5">
<select name="type" id="type" class="form-control">
<option>--Select your ride--</option>
<option value="animals">Animal</option>
<option value="vehicles">Vehicle</option>
</select>
</div>
</div>
<div class="form-group">
<label for="subtype" class="col-sm-5 control-label">More specifically</label>
<div class="col-sm-5">
<select name="subtype" id="subtype" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
<div class="form-group">
<label for="subtype" class="col-sm-5 control-label">More specifically 2</label>
<div class="col-sm-5">
<select name="subtype" id="subtype_1" class="form-control">
<option>-- Select type first--</option>
</select>
</div>
</div>
</form>
</body>
</html>
Upvotes: 1