??yy 2016-07-05 13:34 采纳率: 0%
浏览 13

Cakephp3 jQuery自动完成

I'm new to CakePHP3 (+stackoverflow) and try to implement autocomplete. I'd like to attach the autocomplete-function to the searchinput in my index.ctp.

  1. the request returns all car-objects (ignores the get.term)
  2. the response isn't attached to the searchinput

Would be fine to get some help - thanks!

index.ctp

<?php
echo $this->Form->create('Cars');
    echo $this->Form->input('name', [
            'label' => 'Search',
            'id' => 'autocomplete',
            'class' => 'ui-autocomplete'
    ]);
echo $this->Form->button('Search', ['type' => 'submit']);
echo $this->Form->end();
?>
<script>
$("#autocomplete").autocomplete(
    {
        search: function () {},
        source: function (request, response)
        {
            $.ajax(
            {
                source: "/cars/autocomplete",
                dataType: "json",
                data:
                {
                    term: request.term,
                },
                success: function (data)
                {
                    response(data);
                    console.log(data);
                }
            });
        },
        minLength: 2
    });
</script>

CarsController.php

function autocomplete() {
    if ($this->request->is('ajax','get')) {
        $term = $this->request->data["term"];
        $terms = $cars->find('all', [
            'conditions' => ['Cars.name >' => $term],
            'limit' => 10
        ]);

    $data = array();
        foreach($terms as $term) {
            $row = $term->name;
            array_push($data, $row);
        }

        // $this->layout = 'ajax';
        $this->set('terms', $terms);
        echo json_encode($data);
    // return json_encode($data);
    }
    else {
        echo json_encode('Nothings found');
    }
}
  • 写回答

1条回答 默认 最新

  • weixin_33721427 2016-07-06 10:29
    关注

    Okay, I just found a solution (may be not a perfect?!). Here's my code

    index.ctp

    <script>
    $( function() {
    $( "#autocomplete" ).autocomplete({
        minLength: 1,
        source: function( request, response ) {
            $.ajax({
                url: "<?php echo Router::url(['controller' => 'Cars', 'action' => 'autocomplete']);?>" + '?term=' +
                    request.term,
                    dataType: "json",
                    success: function (data)
                    {
                        response(data);
                        console.log(data);
                    }
              });
          }
      });
    });
    </script>
    

    CarsController.php action autocomplete

    public function autocomplete() {
        $this->autoRender = false;
        $terms = $this->Cars->find('list', array(
                'conditions' => array(
                        'Cars.name LIKE' => trim($this->request->query['term']) . '%'
                )
        ));
        echo json_encode($terms);   
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效