Search code examples
phpjquerycodeigniterjquery-select2codeigniter-2

Cannot load data in select 2 jquery ajax in codeigniter


I want to load data in select2 using ajax on change on text in search filed for that i am using following code but it is not working.There is no error in console and jquery function is not called.

jQuery('#t_name').select2({
  ajax: {
    url: 'get-all_data',
    processResults: function (data) {
      return {
            text: item.text,
                        id: item.id
      };
    }
  }
});

public function getAllData()
    {
        $json = [];
        $results=$this->db->select('*')->from($this->user)->get()->result_array();
        foreach($results as $row)
        {
        $json[] = ['id'=>$row['id'], 'text'=>$row['name']];
        }
        return $json;  

    }

Solution

  • You should have to try something like below:

    processResults: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.NAME,
                            id: item.ID
                        }
                    })
                };
            }