jquery select2 -通过AJAX格式化结果

jquery select2 - Format the results via AJAX php

本文关键字:格式化 结果 AJAX -通过 select2 jquery      更新时间:2023-09-26

我使用select2,我想格式化我的结果

,第一。

$("#id").select2({
    minimumInputLength : 0,
    allowClear: true,
    ajax : {
        url : "Form/page.php",
        dataType : 'json',
        data : function (term, page) {
            return {
                q : term
            };
        },
        results: function (data, page) {
            return { results : data.ex};
        },
        formatResult :  function formatResult(ex) {
            return '<b>' + ex.name + '</b>';
        }
    }

});

我的PHP文件像

    while($r=mysql_fetch_array($m)) {
        $rows['id']=$r['id'];
        $rows['text']=$r['name'];
        $rows['first']=", ". $r['first'];
        $rows2[]=$rows;
    }
    print json_encode($rows2);

我该怎么做呢,谢谢

我认为php代码应该是这样的:

while($r=mysql_fetch_array($m)) {
    $rows['id']=$r['id'];
    $rows['name']=$r['name'];
    $rows['first']=$r['first'];
    $rows2[]=$rows;
}
print json_encode($rows2);

传递一个json对象数组,包含id, name和first。

formatResult的返回值更改为:

return '<b>' + ex.name + '</b>, ' + ex.first;

从Select2 - source示例中转发的PHP示例:

https://github.com/ivaynberg/select2/wiki/PHP-Example

在JS:

$('#categories').select2({
        placeholder: 'Search for a category',
        ajax: {
            url: "/ajax/select2_sample.php",
            dataType: 'json',
            quietMillis: 100,
            data: function (term, page) {
                return {
                    term: term, //search term
                    page_limit: 10 // page size
                };
            },
            results: function (data, page) {
                return { results: data.results };
            }
        },
        initSelection: function(element, callback) {
            return $.getJSON("/ajax/select2_sample.php?id=" + (element.val()), null, function(data) {
                    return callback(data);
            });
        }
    });

和PHP中的

<?php 
    $row = array();
    $return_arr = array();
    $row_array = array();
    if((isset($_GET['term']) && strlen($_GET['term']) > 0) || (isset($_GET['id']) && is_numeric($_GET['id'])))
    {
        if(isset($_GET['term']))
        {
            $getVar = $db->real_escape_string($_GET['term']);
            $whereClause =  " label LIKE '%" . $getVar ."%' ";
        }
        elseif(isset($_GET['id']))
        {
            $whereClause =  " categoryId = $getVar ";
        }
        /* limit with page_limit get */
        $limit = intval($_GET['page_limit']);
        $sql = "SELECT id, text FROM mytable WHERE $whereClause ORDER BY text LIMIT $limit";
        /** @var $result MySQLi_result */
        $result = $db->query($sql);
            if($result->num_rows > 0)
            {
                while($row = $result->fetch_array())
                {
                    $row_array['id'] = $row['id'];
                    $row_array['text'] = utf8_encode($row['text']);
                    array_push($return_arr,$row_array);
                }
            }
    }
    else
    {
        $row_array['id'] = 0;
        $row_array['text'] = utf8_encode('Start Typing....');
        array_push($return_arr,$row_array);
    }
    $ret = array();
    /* this is the return for a single result needed by select2 for initSelection */
    if(isset($_GET['id']))
    {
        $ret = $row_array;
    }
    /* this is the return for a multiple results needed by select2
    * Your results in select2 options needs to be data.result
    */
    else
    {
        $ret['results'] = $return_arr;
    }
    echo json_encode($ret);
    $db->close();
?>