1

我使用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);

我该怎么做,谢谢

4

2 回答 2

1

从 Select2 转发的 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();
?>
于 2013-10-19T16:25:08.473 回答
1

我认为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);

因此,您传递了一个带有 id、name 和 first 的 json 对象数组。

将formatResult的返回更改为:

return '<b>' + ex.name + '</b>, ' + ex.first;
于 2013-04-25T16:04:46.207 回答