2016-07-14 82 views
0

我有兩個問題。現在,無論我輸入什麼內容,我的自動完成功能都會顯示全部600個結果,而不會縮減爲更少的結果。我也需要它顯示與我輸入內容相匹配的最接近的5個結果。任何想法如何改善這一點。如何使用JQuery自動完成縮小結果(它顯示所有結果)

PHP

<?php 
include('inc/connections.php'); 




    $sql = "select top 5 a.item_desc_1, b.item_no from bmprdstr_sql b, imitmidx_sql a 
        where a.item_no=b.comp_item_no and b.user_def_fld = 'x' 
        AND b.item_no like '%{$_POST['auto_me']}%' 
"; 


    $query = odbc_exec($conn, $sql); 
    $json = array(); 
    while ($row = odbc_fetch_array($query)) { 

     $json[] = $row['item_no']; 

    } 
    echo json_encode($json); 

jQuery文檔

<html> 
<head> 
    <title> 
     test 
    </title> 
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 

<script> 
    $(document).ready(function(){ 
     $(function() { 
      $("#auto_me").autocomplete({ 
       source: 'auto_complete.php', 
       autoFocus:true 

      }); 
     }); 
    }); 


</script> 
</head> 




<body> 
<form id="auto"> 
<input type="text" id="auto_me" name="auto_me"/> 
    </form> 
</body> 
</html> 

回答

0
$("#auto_me").autocomplete({ 
    source: function(request, response) { 
     var results = $.ui.autocomplete.filter(myarray, request.term); 
     response(results.slice(0, 10)); 
    } 
}); 

Limit results in jQuery UI Autocomplete

+0

所以我只需要與auto_complete.php更換myArray的? – Ryan

+0

您可以獲取所有結果和results.slice(0,5);在客戶端或始終返回auto_complete.php中的前5個。 http://php.net/manual/en/function.array-slice.php。使用echo json_encode(array_slice($ json,5));如果結果集非常大,我更喜歡第二個。 – SGalea