【发布时间】:2018-09-12 07:37:43
【问题描述】:
我有一个关于如何搜索的问题,例如,在 INDEX 视图中按姓名或姓氏进行搜索。 是否有任何插件或易于使用的组件不会过多地加载应用程序?
目前,我在 index.ctp 中有一个带有一个输入字段的小表单,我在其中输入要搜索的内容,并且在控制器中有一个 if ($ this-> request-> is ('post' )) 之后 $this->Model->find 添加条件 WHERE。但我承认这不是一个好方法,它还会重新加载页面。
这是控制器:
public function index()
{
if ($this->request->is('post')) {
$s = '%'.$this->request->getData('Search').'%';
$students = $this->Students->find('all')->where(['OR' => ['name LIKE' => $s, 'lastname LIKE' => $s]]);
$this->set('students', $this->paginate($students));
} else {
$students = $this->paginate($this->Students);
$this->set(compact('students'));
}
}
这是 index.ctp:
<div class="input-group mb-3">
<?= $this->Form->button(__('<i class="fas fa-search"></i>'), ['escape' => false, 'class' => 'btn btn-primary']) ?>
<?= $this->Form->input('Search', ['type' => 'text', 'class' => 'form-control', 'label' => false]); ?>
<?= $this->Form->end() ?>
</div>
【问题讨论】:
标签: php cakephp search plugins cakephp-3.x