Select2 displays value instead of text

Asked

Viewed 135 times

1

I have some fields that use Select2 to make a basic CRUD, but consulting some fields from another base. The question is, when I click update, I put in an array all table values (dataTables) and then when I step into Select2, it does not display the corresponding text, but the value it received. Follow 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);
});
  • How is the code of your search file ?

  • I added the code to the question

  • If in the return you put: console.log(data) what appears in the Chrome Inspect Element Console ?

  • I imagine you’re saying in return from Select2, it returns json. Actually, it returns nothing if I don’t click the field. It comes filled, but with the value.

  • Solved your doubt?

  • Solved your doubt?

  • Dude, I’m sorry, I’ve been transferred to the company, like, five times, and I dropped out of the project, and they finally asked me to come back with another scope. When I get to that part again I’ll remember and answer

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.