Select2 Ajax 不匹配结果

Posted

技术标签:

【中文标题】Select2 Ajax 不匹配结果【英文标题】:Select2 Ajax not matching results 【发布时间】:2016-04-12 17:37:17 【问题描述】:

我的 Select2 表单使用 ajax 从数据库加载结果,但在前端,select2 搜索返回所有结果,而不是特定于搜索词的结果。在做了一些研究之后,我意识到匹配器必须在服务器端,但我似乎无法让它工作。这是我的代码

查询数据库并返回json数据的代码

<?php

  $servername = "localhost";
  $username = "myusername";
  $password = "mypassword";

   try 
$conn = new PDO("mysql:host=$servername;dbname=mydatabase", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   
    catch(PDOException $e)
   
   echo "Connection failed: " . $e->getMessage();
   



  // strip tags may not be the best method for your project to apply extra 
   layer of security but fits needs for this tutorial 
   $search = strip_tags(trim($_GET['q'])); 

 // Do Prepared Query
   $query = $conn->prepare("
   SELECT * FROM (
    SELECT wp_terms.name
   FROM wp_terms
   JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_id = wp_terms.term_id
    WHERE taxonomy = 'job_listing_region'
    AND count = 0
    ) as T"
     );

    // Add a wildcard search to the search variable
     $query->execute(array(':search'=>"%".$search."%"));


   // Do a quick fetchall on the results
    $list = $query->fetchall(PDO::FETCH_ASSOC);

   // Make sure we have a result
   if(count($list) > 0)
    foreach ($list as $key => $value) 
    $data[] = array('id' => $value['name'], 'text' => $value['name']);              
    
     else 
   $data[] = array('id' => '0', 'text' => 'No Products Found');
   


// return the result in json
echo json_encode($data);

我的 jquery

<script>
 jQuery(function($) 
 $(document).ready(function() 
     function matchStart (term, text) 
  if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) 
    return true;
  

  return false;

$( "#regions1" ).select2(        
    ajax: 
        url: "/ajax/connect.php",
        dataType: 'json',
        delay: 250,
        data: function (params) 
            return 
                q: params.term // search term
            ;
        ,
        processResults: function (data) 
    return 
        results: $.map(data, function(obj) 
            return  id: obj.id, text: obj.text ;

            )

        ;

        


,
  minimumInputLength: 3,
  width: "300px",
  placeholder: "Please Select Up To Four Additional Cities"

 );

    );
 );
</script>

这是我的 json 结果的示例

["id":"Abanda, AL","text":"Abanda, AL","id":"Abbeville, AL","text":"Abbeville, AL","id":"Adak, AK","text":"Adak, AK",

【问题讨论】:

【参考方案1】:

你没有在你的sql语句中提到like条件。

你的 sql 应该是这样的

$query = $conn->prepare("
   SELECT * FROM (
    SELECT wp_terms.name
   FROM wp_terms
   JOIN wp_term_taxonomy
    ON wp_term_taxonomy.term_id = wp_terms.term_id
    WHERE taxonomy = 'job_listing_region'
    AND count = 0 AND search_column LIKE ?    <==== this line
    ) as T"
     );

$query->execute(array("%".$search."%"));

【讨论】:

谢谢!最后的编辑看起来像这样SELECT * FROM ( SELECT wp_terms.name FROM wp_terms JOIN wp_term_taxonomy ON wp_term_taxonomy.term_id = wp_terms.term_id WHERE taxonomy = 'job_listing_region' AND count = 0 AND wp_terms.name LIKE ? ) as T" ); // Add a wildcard search to the search variable $query-&gt;execute(array($search."%"));

以上是关于Select2 Ajax 不匹配结果的主要内容,如果未能解决你的问题,请参考以下文章

Select2 Ajax 不根据查询过滤结果

即使正确的 JSON 格式,Select2 也不显示 AJAX 结果

select2 ajax 显示结果但无法选择

Select2 Ajax Laravel 5.2 - 分页结果不起作用?

Select2:使用 AJAX 未显示结果

jQuery.map() 解析 select2 ajax 调用的结果