2017-07-06 22 views
0

我想使用Jquery自動完成在cakephp中顯示我的表中的數據。我可以使用標籤自動完成工作,但無法顯示錶格中的數據。在正確返回的表中搜索類似數據的函數,但是我不完全確定它是否將它正確地傳遞給自動完成腳本。我對這個還是比較陌生的,有些幫助或暗示會感謝:D。Jquery自動完成不顯示值CakePHP路由

這是InvoicesController.php我的搜索功能

public function search() 
{ 
    $this->loadComponent('RequestHandler'); 
    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('resultsArr', $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', ['resultsArr']); 


    } 
} 

這是我search.ctp代碼

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

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

<script> 
    var testTags = ['52332', '56346', '5734']; 
    var tags = '<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>' 
    jQuery('#id').autocomplete({ 
     source: tags, 
     minLength: 1 
    }); 
</script> 

這就是由函數返回,什麼出現在自動完成下拉。

enter image description here

這是它應該是什麼樣子使用testTags陣列

enter image description here

什麼看法貌似來源。

<div class="input text"><label for="id">Id</label><input type="text" name="id" id="id"/></div> 
<script> 
    var testTags = ['52332', '56346', '5734']; 
    var tags = '/invoices/search' 
    jQuery('#id').autocomplete({ 
     source: testTags, 
     minLength: 1 
    }); 
</script> </div> 

回答

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

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

<script> 
    var testTags = ['52332', '56346', '5734']; 
    var tags = $.post(url:'<?php echo Router::url(array('controller' => 'Invoices', 'action' => 'search')); ?>'); 
    jQuery('#id').autocomplete({ 
     source: tags, 
     minLength: 1 
    }); 
</script> 

嘗試使用jQuery .post的$或者$不用彷徨與阿賈克斯

+0

的jQuery( '#ID')自動完成({ 源獲取數據:$。員額(網址: '/ invoices/search'), minLength:1 }); 這給出了輸出,但在參數列表' –

+0

後給出錯誤'Uncaught SyntaxError:missing)當我使用Jquery $ post函數時,在代碼中出現錯誤。好的語法如下: '$ .post(「url」,{parameter1:「test」,parameter2:「something」}) .done(function(data){ //對您收到的數據做些什麼後端 });' –