Searching with Ajax pagination Cakephp
Step 1 : View File
<script>
$(document).ready(function(){
$(".pagination a, .header a").on('click',function(){
$('#content').load(unescape($(this).attr("href")),function(){
});
return false;
});
});
</script>
<div id="content">
<?php
echo $this->Form->create('User',array('type'=>'GET'));
echo $this->Form->input('User.name',array('id'=>'name','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['name']));
echo $this->Form->input('User.email',array('id'=>'email','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['email']));
echo $this->Form->input('User.message',array('id'=>'message','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['message']));
echo $this->Form->end('Search');
?>
<table>
<?php foreach($userDetail as $admins){?>
<tr>
<td><?php echo $admins['User']['name'];?></td>
<td><?php echo $admins['User']['email'];?></td>
<td><?php echo $admins['User']['message'];?></td>
</tr>
<?php } ?>
</table>
<div class="pagination">
<?php echo $this->Paginator->prev('Prev');?>
<?php echo $this->Paginator->numbers(array('separator' => false));?>
<?php echo $this->Paginator->next('Next');?>
</div>
</div>
Controller function
public function index()
{
$this->loadModel('User');
$conditions = array();
if($_GET)
{
$data = $_GET;
if(isset($data['name']) && !empty($data['name']))
{
$conditions = array_merge($conditions,array('User.name LIKE'=>'%'.trim($data['name']).'%'));
}
if(isset($data['email']) && !empty($data['email']))
{
$conditions = array_merge($conditions,array('User.email LIKE'=>'%'.trim($data['email']).'%'));
}
if(isset($data['message']) && !empty($data['message']))
{
$conditions = array_merge($conditions,array('User.message LIKE'=>'%'.trim($data['message']).'%'));
}
}
if(!empty($conditions)){
$this->paginate = array('limit'=>1,'order'=>'User.id desc');
$userDetail = $this->paginate('User',$conditions);
$this->set('userDetail',$userDetail);
if($this->RequestHandler->isAjax()){
$this->layout = '';
$this->autoRender = false;
$this->render('index');
}
}
}
}
Step 1 : View File
<script>
$(document).ready(function(){
$(".pagination a, .header a").on('click',function(){
$('#content').load(unescape($(this).attr("href")),function(){
});
return false;
});
});
</script>
<div id="content">
<?php
echo $this->Form->create('User',array('type'=>'GET'));
echo $this->Form->input('User.name',array('id'=>'name','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['name']));
echo $this->Form->input('User.email',array('id'=>'email','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['email']));
echo $this->Form->input('User.message',array('id'=>'message','error'=>array('class'=>'error'),
'required'=>false,'value'=>@$_GET['data']['User']['message']));
echo $this->Form->end('Search');
?>
<table>
<?php foreach($userDetail as $admins){?>
<tr>
<td><?php echo $admins['User']['name'];?></td>
<td><?php echo $admins['User']['email'];?></td>
<td><?php echo $admins['User']['message'];?></td>
</tr>
<?php } ?>
</table>
<div class="pagination">
<?php echo $this->Paginator->prev('Prev');?>
<?php echo $this->Paginator->numbers(array('separator' => false));?>
<?php echo $this->Paginator->next('Next');?>
</div>
</div>
Controller function
public function index()
{
$this->loadModel('User');
$conditions = array();
if($_GET)
{
$data = $_GET;
if(isset($data['name']) && !empty($data['name']))
{
$conditions = array_merge($conditions,array('User.name LIKE'=>'%'.trim($data['name']).'%'));
}
if(isset($data['email']) && !empty($data['email']))
{
$conditions = array_merge($conditions,array('User.email LIKE'=>'%'.trim($data['email']).'%'));
}
if(isset($data['message']) && !empty($data['message']))
{
$conditions = array_merge($conditions,array('User.message LIKE'=>'%'.trim($data['message']).'%'));
}
}
if(!empty($conditions)){
$this->paginate = array('limit'=>1,'order'=>'User.id desc');
$userDetail = $this->paginate('User',$conditions);
$this->set('userDetail',$userDetail);
if($this->RequestHandler->isAjax()){
$this->layout = '';
$this->autoRender = false;
$this->render('index');
}
}
}
}
No comments:
Post a Comment