weixin_33728268 2014-10-07 09:15 采纳率: 0%
浏览 143

Select2过滤器不起作用

I use Select2 to handle my dropdownmenu. The dropdownmenu get's populated via "Ajax-call" to PHP, who get's data from Mysql.

The Select2 dropdownmenu offers serach function and filtering. When i type in something in the search field, the letters get underlined, but the filter doesn't work. So the list won't get filtered by the letters i type in. Why?

JS:

$(document).ready(function(){
$("#test").select2({
placeholder: "test",
minimumInputLength: 1,
ajax: {
    url: "test.php",
    dataType: 'json',
    //search term
    data: function (term, page) {
        return {
        q: term, // search term
        page: page
        };
    },
    results: function (data, page) {
    return { results: data};
    } //End of results
}, //End of Ajax
}); //End of select2
  • 写回答

1条回答 默认 最新

  • ??yy 2014-10-08 06:47
    关注

    The filtering must be done in server-side. This PHP script will make the filtering work.

    PHP:

    <?php
    // setup databse connection
    require_once('db.php'); 
    
    // Select from database, i have set the limit to 40 to speed up results
    $result = $db->prepare("SELECT * FROM table WHERE option LIKE :term ORDER BY option ASC LIMIT 0,40");
    
    // bind the value for security with the wildcard % attached.
    $result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
    $result->execute();
    
    // make sure there are some results else a null query will be returned
    if($result->rowcount() != 0) {
    while($row = $result->fetch(PDO::FETCH_ASSOC)){
        $answer[] = array(
                        "id"=>$row['option_id'],
                        "text"=>$row['option']);
    // the text I want to show is in the form of option
    }
    } else {
    // 0 results send a message back to say so.
    $answer[] = array("id"=>"0","text"=>"No Results Found..");
    }
    
    // finally encode the answer to json and send back the result.
    echo json_encode($answer);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题