I have some fields that use Select2 to do a basic CRUD, but querying some fields from another base. The question is, when I click refresh, I put all the values in the table (dataTables) in an array, and then when I go to Select2, it does not display the corresponding text, but rather the value it received. Here are the codes:
$('#grid_tbl tbody').on( 'click', 'tr', function () {
var select = $('#grid_tbl').DataTable().row(this).data();
$('#equipamento').select2("val", select.equipamento);
});
$('#equipamento').select2({
placeholder: "Digite o Equipamento",
initSelection: function(element, callback){
callback({id: element.val(), text:element.val()})
},
quietMillis: 100,
ajax: {
url: "json/finalidade",
dataType: 'json',
type: 'POST',
data: function (term, page) {
return {
term: term, //search term
page_limit: 10 // page size
};
},
results: function (data, page) {
return { results: data };
}
}
});
$formJson->post('/finalidade', function(Request $request) use($app) {
$post = $request->request->all();
$term = $post["term"];
$sql = "SELECT
TRIM(cdfin) cdfin
,TRIM(dsfin) dsfin
FROM
gmt_pend_mnfinali
WHERE
cdfin||dsfin ILIKE '%{$term}%'
ORDER BY
cdfin ASC LIMIT 60";
$result = $app['db']->fetchAll($sql);
if (count($result) == 0) {
return $app->json(array(), 201);
}
foreach ($result as &$v) {
$v['id'] = utf8_encode($v['cdfin']);
$v['text'] = utf8_encode($v['cdfin'].' - '.$v['dsfin']) ;
}
return $app->json($result, 201);
});