端端样式:
前端代码:
<form class="well form-search" method="post" action="{:U('Log/index')}">
<div class="search_type cc mb10">
<div class="mb10">
<span class="mr20">员工:
<select class="select_2" name="uid">
<option value='0'>全部</option>
<foreach name="user_list" item="uu">
<option value="{$uu['id']}" <if condition="$formget['uid'] eq $uu['id']">selected="selected"</if> >{$uu['user_nicename']}</option>
</foreach>
</select>
时间:
<input type="text" name="start_time" class="J_date btn" value="{$formget.start_time|default=''}" style="width: 120px;height: 36px;" autocomplete="off" /> -
<input type="text" name="end_time" class="J_date" value="{$formget.end_time}" style="width: 120px;height: 36px;" autocomplete="off" />
项目名称:
<input type="text" name="keyword" style="width: 200px;height: 36px;" value="{$formget.keyword}" placeholder="请输入项目名称...">
<span> </span>
<input type="submit" class="btn btn-primary" value="搜索" />
<button type="submit" class="btn btn-info" name="excel" value="1">EXCEL导出</button>
</span>
</div>
</div>
</form>
后端代码:
function index(){
$LogModel = D("Common/Log");
$condition = [];
$condition['is_del'] = 0;
if(IS_POST){
$formget = $_POST;
}else{
$formget = $_GET;
}
if(!empty($formget['keyword'])){
$condition['post_title'] = ['like','%'.$formget['keyword'].'%'];
}
if(!empty($formget['uid']) && is_numeric($formget['uid'])){
$condition['uid'] = $formget['uid'];
}
// 判断登录员工属性
if($_SESSION['user_role'] != 1){
$users = D('Users')->getList(['user_role'=>1]);
$user_ids = array_column($users,'id');
$condition['uid'] = ['not in',$user_ids];
}
if(!empty($formget['start_time']) && !empty($formget['end_time'])){
$condition['log_date'] = ['BETWEEN',[$formget['start_time'],$formget['end_time']]];
}else if(!empty($formget['start_time']) && empty($formget['end_time'])){
$condition['log_date'] = array('EGT',$formget['start_time']);
}else if(!empty($formget['end_time']) && empty($formget['start_time'])){
$condition['log_date'] = array('ELT',$formget['end_time']);
}
$p = I ( 'p', 1, 'intval' );
$list = $LogModel->getPageList($condition,$p);
// 此处判断是否为导出操作
if(isset($formget['excel']) && $formget['excel'] == 1){
$excel_arr = $LogModel->getList2($condition);
$pro_status = array(0=>'开发',1=>'BUG','2'=>'新需求');
$work_status = array(0=>'正常',1=>'加班');
$status = '';
foreach($excel_arr as $k=>$v){
$status = $excel_arr[$k]['pro_status'];
$work_sta = $excel_arr[$k]['work_status'];
$excel_arr[$k]['pro_status'] = $pro_status[$status];
$excel_arr[$k]['work_status'] = $work_status[$work_sta];
}
$header = [
'A' => ['post_title', '项目名称'],
'B' => ['pro_status', '项目属性'],
'C' => ['user_nicename', '员工姓名'],
'D' => ['work_pro', '项目进度(%)'],
'E' => ['log_date', '日报日期'],
'F' => ['work_time', '工作时长'],
'G' => ['work_status', '工作类型'],
'H' => ['create_time', '填写日期']
];
$fileName = '司瑞盛工作日报'.date('Y-m-d');
// Excel 导出,在公共方法中
$this->excel($header, $excel_arr, $fileName);
}
// 调用员工信息
$UserModel = D("Users");
$where = [];
if($_SESSION['user_role'] != 1){
$where['user_role'] = 0;
}
$user_list = $UserModel->getList($where);
// 获取文章总数
$count = $LogModel->getCount2($condition);
$Page = new \Think\Page($count,10); // 实例化分页类 传入总记录数和每页显示的记录数
//分页跳转的时候保证查询条件
foreach($formget as $key=>$val) {
$Page->parameter[$key] = urlencode($val);
}
$show = $Page->show();// 分页显示输出
$this->assign('page',$show);// 赋值分页输出
$this->assign('list',$list);
$this->assign('user_id',session('ADMIN_ID')); // 管理员ID
$this->assign('user_list',$user_list);
$this->assign('formget',$formget);
$this->display();
}
注释:
其中此处为重点1:
if(IS_POST){
$formget = $_POST;
}else{
$formget = $_GET;
}
此处为重点2:
//分页跳转的时候保证查询条件
foreach($formget as $key=>$val) {
$Page->parameter[$key] = urlencode($val);
}