layui在搜索的时候没有数据,在表格中显示暂无数据
网上有很多教程,都是说要改变table.js,但是我发现不需要,只需要在返回json数据的时候做一下判断即可。
php给layui返回json数据,并且按着layui的格式格式化数据。
//格式化数据,返回给layui的json数据
protected function formatData($data,$count){
if ($data){
$arr = [
'code'=>'0',
'data'=>$data,
'count'=>$count,
];
return json_encode($arr);
}else{
$arr = [
'code'=>'1',
'msg'=>'未查到任何信息',
'data'=>[]
];
return json_encode($arr);
}
}
}
PS:有数据返回值的解析,数组的”键“都是固定写法,layui的格式
code = 0 是有数据的状态码。
data = $data;查询出来的数据
count = $count 数据的总条数
PS:无数据返回值得解析,数组的“键”都是固定写法,layui的格式
code = 1 无数据返回的状态码
msg = ‘暂无数据’,//可以随意提示任何信息
data= [] //固定写法,如果不返回data,layui就会报数据接口异常。
调用formData方法,传入数据
public function userlist(Request $request){
$nick_name = $request->input('nick_name');
$mobile = $request->input('mobile');
$sex = $request->input('sex');
if (!empty($nick_name)){
$where = " where nick_name like '%$nick_name%'";
}else if (!empty($mobile)){
$where = " where mobile like '$mobile'";
}else if(!empty($sex)){
if ($sex=='1'){
$where = " where sex like '$sex'";
}else if($sex=='2'){
$where = " where sex like '$sex'";
}else{
$where = " where true";
}
}else{
$where = " where true";
}
//总记录数
if (empty($nick_name) && empty($mobile) && empty($sex)){
$count = \DB::select("select count(-1) as num from `user`");
}else{
$count = \DB::select("select count(-1) as num from `userinfo` where nick_name like '%安其拉%'");
}
//分页
$page = $request->input('page');
$limit = $request->input('limit');
$strno = ($page-1)*$limit;
$data = \DB::select("select `user`.id as user_id,`user`.create_time,`user`.email,`user`.tel,`user`.`status`,`user`.last_time,`user`.last_time,userinfo.* from `user` left join userinfo on `user`.id = userinfo.uid $where");
//var_dump($data);die();
foreach($data as $v){
//时间戳转换时间格式
$v->create_time = date('Y-m-d H:i:s',$v->create_time);
if (!empty($v->logo)){
$v->logo = 'http://www.redshop.com/headphoto/'.mb_substr($v->logo,0,8,'utf-8')."/".$v->logo;
}else{
$v->logo = 'http://www.redshop.com/headphoto/default.png';
}
if ($v->sex=='1'){
$v->sex = "男";
}else if($v->sex=="2"){
$v->sex="女";
}else if($v->sex=="0"){
$v->sex='保密';
}else{
$v->sex="未知";
}
}
//var_dump($data);die();
return $this->formatData($data,$count['0']->num);
}