这里ajax搜索工作正常,但问题是相同的数据显示两次,因为我已经给出了分页限制2.如果我使它3,同样的结果显示3次。
这是我的index.ctp
$( "#search" ).keyup(function() {
var value=$('#search').val();
$.get("<?php echo Router::url(array('controller'=>'userTypes','action'=>'search'));?>",{search:value},function(data){
$('.search_data').html(data);
});
})
这是我的搜索操作
public function search()
{
$search=$_GET['search'];
$request=$this->UserType->find('all', array(
'conditions' => array('UserType.name LIKE' => "%$search%")
));
$this->set('usertype',$request);
}
这是search.ctp
<?php foreach($usertype as $usertypes) { ?>
<tr>
<td><?php echo $usertypes['UserType']['id']; ?></td>
<td><?php echo $usertypes['UserType']['name']; ?></td>
<td><?php echo $usertypes['UserType']['description']; ?></td>
</tr>
<?php } ?>
分页限制在appcontroller中
parent::beforeFilter();
$this->Paginator->settings = array(
'limit'=>2
);
结果显示我这样
愿任何人帮助我解决它吗?
答案 0 :(得分:1)
如果您想使用分页,您的搜索方法应如下所示:
public function search()
{
if(isset($this->request->query['search'])){
$search = $this->request->query['search'];
}else{
$search = '';
)
$this->Paginator->settings = array(
'conditions' => array('UserType.name LIKE' => "%$search%"),
'limit' => 2
);
));
$this->set('usertype',$this->Paginator->paginate());
}