如何将ajax数据附加到select2中

时间:2018-03-13 09:06:44

标签: javascript jquery ajax

我正在使用select2下拉列表,我正在尝试构建它,以便根据JSON响应动态显示潜在客户。

正如您在下图中看到的那样,正确插入的文本会返回JSON数组,但是,select2无法将结果分配给选项。因此,我完全坚持从这里做什么。

enter image description here https://i.imgur.com/9OnvJzK.pnghere

我已经尝试设置一个等于selectbox的变量并在那里附加数据,但我的编辑器指出代码无法访问。

Create.tpl - 包含前端代码

{literal}
<script>
    $(document).ready(function() {

        $('.select2lead').select2({
            minimumInputLength: 3,
            ajax: {
                type: 'GET',
                url: '/modules/support/ajax.php',
                dataType: 'json',
                delay: 250,
            }
        });

        $('#select2lead').select2({
            tags: true,
            minimumInputLength: 3,
            ajax: {
                type: 'GET',
                url: '/modules/support/ajax.php',
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    var query = {
                        search: params.term
                    };
                    // Query parameters will be ?search=[term]&type=public
                    return query;
                },
                processResults: function (data) {
                    var select2lead = $('#select2lead');
                    // Tranforms the top-level key of the response object from 'items' to 'results'
                    return {
                        results: data.items
                    }
                    // var option = new Option(data.name, data.id, true, true);
                    // select2lead.append(option).trigger('change');
                }
            }
        });

        $('#summernote').summernote({
            height: 150,
            minHeight: null,
            maxHeight: null,
            focus: true
        });
    })

</script>
{/literal}

ajax.php - 处理搜索字词并返回JSON

<?php
require_once('../../config.php');

$login = new Login();
if (!$login->checkLogin()) {
    echo lang($_SESSION['language'], "INSUFFICIENT_RIGHTS");
    exit();
}

$db = new Database();

$query = "
        SELECT
            LeadID AS lead_id,
            REPLACE(CONCAT_WS(' ', LeadInitials, LeadInsertion, LeadLastName), '  ', ' ') AS name,
            REPLACE(CONCAT_WS(' ', LeadStreet, LeadStreetNumber, LeadNumberAdjective), '  ', ' ') AS address,
            LeadZiPCode AS zipcode,
            LeadCity AS city
        FROM
            `LeadTBL`
        WHERE
            LeadID > 0
        AND
            LeadLastName LIKE :leadName
        ORDER BY
            LeadLastName
        ASC
    ";

$binds = array(':leadName' => $_GET['term'].'_%'.'_%');
$result = $db->select($query, $binds);
$json = [];

foreach ($result as $row){
    $json[] = ['id'=>$row['lead_id'], 'name'=>$row['name']];
}

echo json_encode($json);

1 个答案:

答案 0 :(得分:0)

问题已解决。结果我不得不禁用&#34;更多&#34 ;;见https://groups.google.com/forum/#!msg/select2/4mDifie32t0/jdJl8KIFN0EJ

关于实际将结果正确粘贴到下拉列表中的最终代码,请参阅:

    $('.select2lead').select2({
        minimumInputLength: 3,
        ajax: {
            type: 'GET',
            url: '/modules/support/ajax.php',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    term: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: data,
                    more: false
                };
            }
        }
    });