Im using Select2 Bootstrap https://select2.github.io/ for Remote Data Fetching Via Ajax and All this inside a Jquery Repeater http://briandetering.net/repeater
<div data-repeater-item class="mt-repeater-item">
<!-- jQuery Repeater Container -->
<div class="mt-repeater-input">
<label class="control-label">First Team</label>
<br/>
<select name="equipe_1" id="select2-button-addons-single-input-group-sm" class="form-control js-data-example-ajax">
</select>
</div>
<div class="mt-repeater-input">
<label class="control-label">Second Team</label>
<br/>
<select name="equipe_2" id="select2-button-addons-single-input-group-sm" class="form-control js-data-example-ajax">
</select>
</div>
<div class="mt-repeater-input">
<a href="javascript:;" data-repeater-delete class="btn btn-danger mt-repeater-delete">
<i class="fa fa-close"></i> Delete</a>
</div>
</div>
This is my HTML but when i Click on the Add button i have the form cloned but the slect2 dropdonw is not working.
My Componement Select2 JS File
$(".js-data-example-ajax").select2({
placeholder: "Choose a Team...",
width: "off",
allowClear: true,
multiple:false,
ajax: {
url: "http://test.dev/teamsearch",
dataType: 'json',
type: "POST",
delay: 2000,
data: function(params) {
return {
q: params.term, // search term
page: params.page,
_token: CSRF_TOKEN
};
},
processResults: function(data, page) {
// parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to
// alter the remote JSON data
return {
results: data
};
},
cache: true
},
escapeMarkup: function(markup) {
return markup;
}, // let our custom formatter work
minimumInputLength: 4,
maximumSelectionLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
my Jquery Repeater File:
var FormRepeater = function () {
return {
//main function to initiate the module
init: function () {
$('.mt-repeater').each(function(){
$(this).repeater({
show: function () {
$(this).slideDown();
},
hide: function (deleteElement) {
if(confirm('Are you sure you want to delete this element?')) {
$(this).slideUp(deleteElement);
}
},
ready: function (setIndexes) {
}
});
});
}
};
}();
jQuery(document).ready(function() {
FormRepeater.init();
});
Im using Select2 ajax to load a list of team inside.
How can i make select2 working when jquery repeater clone my form ? Thank's