使用Cakephp 3实现自动完成时出错

问题描述

朋友,我正在尝试为CakePHP 3.8中的项目改编this tutorial我有一个用户表,我想按名称进行搜索

使用烘焙生成代码并插入搜索字段后,我的index.ctp如下所示:

<html>
<body>
    <?= $this->Form->create('',['type' => 'get']); ?>
    <?= $this->Form->control('keyword',array('class' => 'auto','value' => '','label' => false,'required' => true));?>
    <?= $this->Form->button(__('Search')); ?>
    <?= $this->Form->end();  ?>

    <nav class="large-3 medium-4 columns" id="actions-sidebar">
        <ul class="side-nav">
            <li class="heading"><?= __('Actions') ?></li>
            <li><?= $this->Html->link(__('New User'),['action' => 'add']) ?></li>
        </ul>
    </nav>
    <div class="large-9 medium-8 columns content">
        <h3><?= __('Users') ?></h3>
        <table cellpadding="0" cellspacing="0">
            <thead>
                <tr>
                    <th scope="col"><?= $this->Paginator->sort('id') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('name') ?></th>
                    <th scope="col"><?= $this->Paginator->sort('created') ?></th>
                    <th scope="col" class="actions"><?= __('Actions') ?></th>
                </tr>
            </thead>
            <tbody>
                <?PHP foreach ($users as $user): ?>
                <tr>
                    <td><?= $this->Number->format($user->id) ?></td>
                    <td><?= h($user->nome) ?></td>
                    <td><?= h($user->created) ?></td>
                    <td class="actions">
                        <?= $this->Html->link(__('View'),['action' => 'view',$user->id]) ?>
                        <?= $this->Html->link(__('Edit'),['action' => 'edit',$user->id]) ?>
                        <?= $this->Form->postLink(__('Delete'),['action' => 'delete',$user->id],['confirm' => __('Are you sure you want to delete # {0}?',$user->id)]) ?>
                    </td>
                </tr>
                <?PHP endforeach; ?>
            </tbody>
        </table>
        <div class="paginator">
            <ul class="pagination">
                <?= $this->Paginator->first('<< ' . __('first')) ?>
                <?= $this->Paginator->prev('< ' . __('prevIoUs')) ?>
                <?= $this->Paginator->numbers() ?>
                <?= $this->Paginator->next(__('next') . ' >') ?>
                <?= $this->Paginator->last(__('last') . ' >>') ?>
            </ul>
            <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}},showing {{current}} record(s) out of {{count}} total')]) ?>
            </p>
        </div>
    </div>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.1/jquery-ui.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $(".auto").autocomplete({
            source: "/users",minLength: 1
        });
    });
    </script>
</body>
</html>

在这部分中,我不知道如何适应。

 //autocomplete
    $(".auto").autocomplete({
        source: "users",minLength: 1
    });  

在这部分中,我不知道该如何适应。

我的UsersController.PHP看起来像这样:

    public function index()
    {
        $users= $this->paginate = [
            'conditions' => [
                'name like' => '%'. $this->request->getQuery('keyword') . '%',]
        ];
        
        $this->set('users',$this->paginate($this->Users));
        $this->set('_serialize',['users']);
    }

此刻,对于任何搜索,即使该名称存在于数据库中,我也会得到“没有搜索结果。”。 我感谢有人可以分析。欢迎发表评论

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)