【问题标题】:Cakephp 3.0 autocomplete jquery uiCakephp 3.0 自动完成 jquery ui
【发布时间】:2015-09-08 17:38:56
【问题描述】:

我需要知道为什么以下不起作用。此代码将从索引控制器中提取数据以搜索获取 json 数据。也没有发出任何请求,也没有任何反应

我是 Cakephp 3.0 的新手

我正在尝试让自动完成/自动提示在 CakePHP 3.0 中工作,但我发现的一切要么适用于 1.3,要么根本不适用于 Cake,我不知道该怎么做才能让它正常工作。我正是需要它对于 cakephp 3.0 sn-p 1.汽车控制器.php 这是汽车控制器在 ajax 请求时访问,并将在 json 数据中进行编码 2.CarsTable.php 从 carstable 中获取数据 3.index.ctp 是查看页面和自动完成方法

<?php
namespace App\Controller;

use App\Controller\AppController;

  class CarsController extends AppController {    

    public function index() {
       $this->loadComponent('RequestHandler'); 
      if ($this->request->is('ajax')) {
        $term = $this->request->query('term');
        $carNames = $this->Car->getCarNames($term);
        $this->set(compact('carNames'));
        $this->set('_serialize', 'carNames');
      }
    }
  }
  ?>
<?php
namespace App\Model\Table;

use Cake\ORM\Table;


  class Carstable extends AppModel {

    public function getCarNames ($term = null) {
      if(!empty($term)) {
        $cars = $this->find('list', array(
          'conditions' => array(
            'name LIKE' => trim($term) . '%'
          )
        ));
        return $cars;
      }
      return false;
    }
  }
  ?>

<?php
  //let's load jquery libs from google
  $this->Html->script('https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('inline' => false));
  $this->Html->script('https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js', array('inline' => false));

  //load file for this view to work on 'autocomplete' field
  

  //form with autocomplete class field
  echo $this->Form->create();
  echo $this->Form->input('name', array('class' => 'ui-autocomplete',
               'id' => 'autocomplete'));
  echo $this->Form->end();
  ?>


  <script type="text/javascript">
(function($) {
  $('#autocomplete').autocomplete({
        source: "<?php (array('controller'=>'Cars','action'=>'search')); ?>",
        datatype:"json",
        minLength: 1
  })
})
  </script>

【问题讨论】:

  • 我们需要更多细节。什么“不工作”?有错误信息吗?

标签: javascript php jquery json cakephp


【解决方案1】:

此代码 sn-p 将适用于 cakephp 3.0 中的自动完成

查看搜索\Template\post\ 搜索.ctp

<?php
use Cake\Routing\Router;
use Cake\View\Helper\UrlHelper;

?><div class="ui-widget">
<?php
        echo $this->Form->create('Posts', array('action' => 'search'));
        echo $this->Form->input('name',array('id' => 'Autocomplete')); 
       
        echo $this->Form->end();
    ?></div><div class="ui-widget" style="margin-top:2em; font-family:Arial">
  Result:
  <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script type="text/javascript">
      $(document).ready(function($){
    $('#Autocomplete').autocomplete({
  source:'<?php echo Router::url(array("controller" => "posts", "action" => "search")); ?>',
  minLength: 1
    });  });
</script>

在您的控制器中添加此功能。正确更改数据库和其他变量。 src\Controller\PostController.php

 public function search() 
       {
        if ($this->request->is('ajax')) {
            
            $this->autoRender = false;            
            $name = $this->request->query('term');            
            $results = $this->Posts->find('all', array(
                                           'conditions' => array('Posts.title LIKE ' => '%' . $name . '%')
                                         
                                           ));
            
            $resultArr = array();
            foreach($results as $result) {
               $resultArr[] = array('label' =>$result['title'] , 'value' => $result['title'] );
            }
            echo json_encode($resultArr);              
}}

在 routes.php 中添加这一行

Router::extensions('json', 'xml');

【讨论】:

  • 这很好用,但我不断收到警告错误记录:2016-11-09 16:24:44 Warning: Warning (2): Cannot modify header information - headers already sent by (output started at /apps/water-assets/src/Controller/RealEstateRequestsController.php:152) in [/apps/water-assets/vendor/cakephp/cakephp/src/Http/ResponseEmitter.php, line 170] 这是echo json_encode($resultArr); 行。
猜你喜欢
  • 2011-04-13
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
相关资源
最近更新 更多