2017-11-13 16 views
0

Jqueryの自動完了機能を使用してSQLテーブルからデータを取得しようとしていますが、データを正しく検索してもドロップダウンボックスでグラフィカルに表示されません。jQueryオートコンプリートデータが返されましたが表示されません

私search.ctp

<?php use Cake\Routing\Router; ?> 

<?php echo $this->Form->input('id', ['type' => 'text']); ?> 

<script> 
jQuery('#id').autocomplete({ 
    source:'<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>' 
}); 
</script> 

私の検索機能

public function search() 
{ 
    if ($this->request->is('ajax')) 
    { 
     $name = $this->request->query['term']; 
     $resultArr = $this 
      ->Invoices 
      ->find() 
      ->where(
       ['Invoices.id LIKE' => ($name . '%')], 
       ['Invoices.id' => 'string'] 
      ); 

     $resultsArr = []; 
     foreach ($resultArr as $result) { 
      $resultsArr[] = (strval($result['id'])); 
     } 

     $this->set('invoices', $resultsArr); 
     // This line is what handles converting your array into json 
     // To get this to work you must load the request handler 
     $this->set('_serialize', ['invoices']); 
    } 
} 

あなたはそれがダウンしたデータが、ノードロップを返して見ることができるように。

enter image description here

+0

また、受信側でjson解析を行う可能性が高い「何かjson」というものがたくさん返されているように見えます。 – IncredibleHat

+0

@Randallは私の悪いことを忘れてしまいました。 –

+0

これは奇妙なことです。オートコンプリートの基本的な動作は、選択したフィールドの下にあるdivをタックすることだけです。あなたが何かをタイプすると、サーバにはっきりと当たってしまい、サーバはアイテムのjson配列を返しています。だから...私の唯一の推測は、選択肢の表示と競合する可能性のあるCSSがありますか? – IncredibleHat

答えて

0

は私の検索機能でデータをシリアライズする前json_encode($resultsArr);を追加することによってそれを解決しました。

+1

'_serialize'オプションの文字列を渡すだけです** https://stackoverflow.com/questions/19250477/how-to-output-serialized-json-view-data-as-an-array-of-オブジェクト - wrappの代わりに/ 19251077#19251077 **。 – ndm

関連する問題