2017-07-06 34 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){ //对您收到的数据做些什么后端 });' –