我需要知道为什么下面的代码不起作用。这段代码将从索引控制器中提取数据以搜索获取json数据。

我都是Cakephp 3.0的新手

我正在尝试在CakePHP 3.0中实现自动完成/自动建议功能,但我发现的所有内容要么都适用于1.3,要么根本不适用于Cake,我不知道该怎么做才能使其正常运行.cakephp 3.0恰恰需要它
片段
1.CarsController.php
这是当ajax请求时carscontroller访问并将在json数据中编码
2.CarsTable.php
从车座上获取数据
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>

最佳答案

此代码段适用于cakephp 3.0中的自动完成功能

查看搜索\ Template \ post \
Search.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中添加此行

路由器:: extensions('json','xml');

关于javascript - Cakephp 3. 0 auto 完成jQuery UI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32464016/

10-12 00:21
查看更多