2

I have an array of data that has been made JSON and then I want to display it in select, then I can find the data according to input

this is my controller

$response = $this->GET(config('constant.url').'school-list');
return json_encode($response);

this is my blade view

<div class="form-group">
  <label for="inputGrade">Select School</label>
  <select class="cari form-control" style="width:525px;" name="cari">
      <option value="disable"></option>
  </select>
</div>

$('.cari').select2({
        placeholder: 'Cari...',
        ajax: {
        url: '/select/school',
        dataType: 'json',
        delay: 250,
        data:function(param){
            return{
                school_name:param.term
            }
        },
        processResults: function (data) {
            return {
                results: $.map(data, function (item) {
                    return {
                        school_name: item.school_name,
                        school_id: item.school_id
                    }
                })
            };
        },
        cache: true
        }
    })

and this is my json

{
    "STATUS": 200,
    "MESSAGE": "SUCCESS",
    "DATA": [
        {
            "school_id": 11,
            "school_name": "ACS Jakarta"
        },
        {
            "school_id": 13,
            "school_name": "Al Azhar 20"
        },
        {
            "school_id": 14,
            "school_name": "Al Azhar 23"
        },
        {
            "school_id": 15,
            "school_name": "Bangun Insan Mandiri"
        },
        {
            "school_id": 16,
            "school_name": "Bina Bangsa School Kebon Jeruk"
        },
     ]
}

I have tried it several times but the list of data that I have encoded is not there

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49

1 Answers1

0

Change school_name to text and school_id to id in processResults function.

processResults: function (data) {
  var response = JSON.parse(data);
    return {
        results: $.map(response.DATA, function (item) {
            return {
                text: item.school_name,
                id: item.school_id
            }
        })
    };
},

check more detailed answer will helped you.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49