I'm trying to make a master detail
One of its inputs is Select2
, but it does not generate Select2
, because it is only generated when the script finishes reading, and the second way I did, was to do a function that calls the select2, for it to always be generated, however it ends up clearing the others already filled
Well my code looks like this:
<button type="button" id="add">Adicionar</button>
<div class="vinculo">
</div>
$(function () {
$("#add").on("click", function () {
var i = $('.vinculo input').size() + 1;
$('<input type="text" name="vinculo[' + i + ']" id="select2vinculo' + i + '" />').appendTo(".vinculo");
});
});
and my select2:
var pageSize = 10;
$('.select2vinculo').select2(
{
minimumInputLength: 0,
allowClear: true,
ajax: {
quietMillis: 150,
url: '@Url.Action("Vinculos", "Select2", new { area = "" })',
dataType: 'json',
data: function (filtro, page) {
return {
pageSize: pageSize,
pageNum: page,
Filtro: filtro
};
},
results: function (data, page) {
var more = (page * pageSize) < data.total;
return {
results: data.result,
more: more
};
}
},
initSelection: function (element, callback) {
var vinculo = $('#select2vinculo').val();
if (vinculo !== null && vinculo > 0) {
$.ajax({
url: '@Url.Action("BuscaVinculoId", "Select2", new { area = "" })',
dataType: "json",
data: { Id: vinculo },
success: function (data) {
callback(data);
}
});
}
},
})
I also do not know how I will capture all the values when editing, because the initSelection function takes the value of select2 and calls to set the value of it, which is what makes the code, since I have to capture all to leave selected , ie "# select2link + i, where i is generated by" Add "input
initSelection: function (element, callback) {
var vinculo = $('#select2vinculo').val();
Does anyone have a solution? whether in jquery, knockout, or any other way to do a master details
I'm grateful.