Select2 initSelection

≯℡__Kan透↙ 提交于 2019-12-01 08:27:47

Pay attention, the data you send to the callback function, needs to be an object, with an id and text attributes.

This is what worked for me:

initSelection: function(element, callback) {
    var id;
    id = $(element).val();
    if (id !== "") {
      return $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: {
          id: id
        }
      }).done(function(data) {
        var results;
        results = [];
        results.push({
          id: data.id,
          text: data.name
        });
        callback(results[0]);
      });
    }
  }

Here the way to solve:

initSelection: function(element, callback) {

        return $.ajax({
            type: "POST",
            url: path,
            dataType: 'json',
            data: { id: (element.val())},
            success: function(data){

            }
        })

care must be taken that the array is created in the controller and it should be something like this:

foreach ($entities as $member) {
        $json['id'] = $member->getId();
        $json['name'] = $member->getComune();
        $json['region'] = $member->getRegione()->getRegione();
        $json['prov'] = $member->getProvincia()->getSigla();
}

        $result = $json;
        return json_encode($result);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!