当列出查找的内容有很多条的时候我们可以将数据进行分页显示。
user表的结构如图:
现在要将用户以列表的形式显示,显然不可能将查询结果显示在一页当中,此时要将结果分页显示,首先将分页控件page.php复制到项目的control文件中,然后我们可以在控制器中输入如下代码:
function actionCusList()
{
$cus_info = User::find();
//获取当前是第几页
$page = intval( $this->_context->page );
$page<1?1:$page;
//设置每页显示的数量
$page_size = 10;
//按条件查找用户
if($id = $this->_context->get('cus_id'))
$cus_info->where('id = ?', $id);
else
{
if($first_name = $this->_context->get('first_name'))
$cus_info->where('first_name = ?', $first_name);
if($last_name = $this->_context->get('last_name'))
$cus_info->where('last_name = ?', $last_name);
if($email = $this->_context->get('email'))
$cus_info->where('email = ?', $email);
if($code = $this->_context->get('code'))
$cus_info->where('pro_id = ?', Program::find('code = ?', $code)->getOne()->id);
}
$cus_info->limitPage($page, $page_size);
$cus = $cus_info->getAll();
//渲染视图
$this->_view['url_args'] = $this->_context->get();
$this->_view['pagination'] = $cus_info->getPagination();
$this->_view['cus'] = $cus;
}
此代码中有一处是按条件查找用户,我们可以进行前台设计一个表单让用户输入特定条件来查找用户,代码如下:
<div class="pt10">
<a href="javascript:;" onclick="javascript:$(this).parent().next('div').toggle();" title="search >">Search >
</a>
</div>
<!--search-->
<div id="searchbox" style="display: none;" class="search_box">
<a href="javascript:void(0);" onclick="javascript:$(this).parent().hide();" class="close2 f_r" title="close">close</a>
<form name="city_search_form" action="#" method="get">
<p>
<label for="">Customer ID</label>
<input class="text" name="cus_id" type="text">
</p>
<p>
<label for="">First Name</label>
<input name="first_name" class="text" type="text">
</p>
<p>
<label for="">Last Name</label>
<input name="last_name" class="text" type="text">
</p>
<p>
<label for="">Email Address</label>
<input name="email" class="text" type="text">
</p>
<p>
<label for="">Program Code</label>
<input name="code" class="text" type="text">
</p>
<p>
<label> </label>
<a onclick="city_search_form.submit();" title="" class="add_btn">Search</a>
</p>
</form>
</div>
<div class="paging pt10 txt_right">
<?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
</div>
<table class="pagetab mtb6" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th width="10%">Cust. ID</th>
<th width="15%">First Name</th>
<th width="15%">Last Name</th>
<th width="20%">Email Address</th>
<th width="10%">Program</th>
<th width="25%">Last Login</th>
<th width="5%">Action</th>
</tr>
</thead>
<?php foreach ($cus as $cus):; ?>
<tbody>
<tr>
<td><?php echo $cus['id']; ?></td>
<td><?php echo $cus['first_name']; ?></td>
<td><?php echo $cus['last_name']; ?></td>
<td><?php echo $cus['email']; ?></td>
<td><?php echo $cus['program']['name']; ?></td>
<td><?php echo $cus['last_login_date']; ?></td>
<td>
<a href="<?php echo url('cus/cusdetail', array('id' => $cus['id']))?>" title="View">View</a>
</td>
</tr>
</tbody>
<?php endforeach; ?>
</table>
<div class="paging pt10 txt_right">
<?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
</div>
分页关键代码为:
<?php echo $this->_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
显示结果如图: