代码之家  ›  专栏  ›  技术社区  ›  XMozart

Select2不显示来自Ajax的结果

  •  0
  • XMozart  · 技术社区  · 6 年前

    为什么我的select2对象在过滤时总是显示“找不到结果”,但是当我在Google Chrome上检查网络时,它会有响应。我用的是select2 ver。4.0.6RC,1

    下面是创建select2对象的代码:

    <div class=“form group”id=“divjualcust”>
    <label>客户</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>
    &L/DIV & GT;
    < /代码> 
    
    

    javascript代码:

    $(document).ready(function()。{
    $(“jualcust”)。选择2({
    Ajax:{
    url:“controller/customer_list.php”,
    数据类型:“json”,
    类型:“获取”,
    延迟:250,
    数据:函数(参数){
    返回{
    搜索:params.term
    }
    }
    处理结果:函数(数据){
    //将结果分析为select2所需的格式。
    //由于我们使用的是自定义格式设置函数,因此不需要
    //修改远程JSON数据
    返回{
    结果:.map(data.items,函数(item){
    返回{
    身份证:
    文本:项目文本
    }
    })
    };
    }
    高速缓存:错误
    }
    最小输入长度:3,
    (});
    < /代码> 
    
    

    下面是mycontroller/customer_list.php上的函数

    函数getcustlist($search){
    //声明var public
    $cn=new database();
    $CUST= ARRAY();
    $row=$cn->getall(“select cust_id,concat(cust_kode,-”,cust_nama)作为来自t_客户的cust nama
    其中concat(cust_kode,“-”,cust_nama)类似于“%$search%”,order by concat(cust_kode,“-”,cust_nama)“);
    如果(是数组($row))。{
    foreach($row as$dt){
    $id=$dt[“客户ID”];
    $TEXT=$DT[“CUSTNAMA”];
    $cust[]=数组(“id”=>$id,“text”=>$text);
    }
    echo json_编码($cust);
    }
    }
    < /代码> <用海绵吸干。我用的是select2 ver。4.0.6RC,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 回复  |  直到 6 年前
        1
  •  0
  •   madalinivascu    6 年前

    你没有 items 数组,因此只需循环 data

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