Select2 не показывает результат от AJAX - PullRequest
0 голосов
/ 22 октября 2018

, почему мой объект select2 при фильтрации всегда показывает «Результаты не найдены», но когда я проверил в сети «Проверка сети» в Google Chrome, есть ответ от него.Я использую select2 ver.4.0.6-rc.1

Select2

вот мой код для создания select2 obj:

<div class="form-group" id="divjualcust">
    <label>Customer</label>
    <select class="js-example-basic-single" name="customer" id="jualcust" style="width:100%"></select>
    <label id="lblerrjual" style="display:none"><i>*sudah terdaftar</i></label>
</div>

javascript коды:

$(document).ready(function() {
    $("#jualcust").select2({
        ajax: {
            url: "controller/customer_list.php",
            dataType: "json",
            type:"GET",
            delay: 250,
            data: function (params) {
                return {
                    search: params.term
                }
            },
            processResults: function (data) {
                // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to
                // alter the remote JSON data
                return {
                    results: $.map(data.items, function (item) {
                      return {
                        id:item.id,
                        text:item.text
                      }
                    })
                };
            },
            cache: false
        },
        minimumInputLength: 3,
    });

и вот функция на моем controller/customer_list.php:

function getCustList($search) {
    //declare var public
    $cn=new Database();
    $cust=array();
    $row=$cn->getAll("select cust_id, concat(cust_kode,' - ',cust_nama) as custnama from t_customer 
                      where concat(cust_kode,' - ',cust_nama) like '%$search%' order by concat(cust_kode,' - ',cust_nama)");
    if (is_array($row)) {
        foreach ($row as $dt) {
            $id=$dt["cust_id"];
            $text=$dt["custnama"];
            $cust[]=array("id"=>$id,"text"=>$text);
        }
        echo json_encode($cust);
    }
}

1 Ответ

0 голосов
/ 22 октября 2018

У вас нет массива items, поэтому вам нужно выполнять цикл только над data

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