ThinkPHP/---layui 分页

Model(base)

 

 

class Base extends Model
{
protected $autoWriteTimestamp = true;

public $page = '';

public $size = '';

public $from = '';


public function getTableList($data)
{
$query = http_build_query($data);
$where = [];
if(!empty($data['start']) && !empty($data['end']))
{
$where['create_time'] = [['>=',strtotime($data['start'])],['<=',strtotime($data['end'])]];
}
if(!empty($data['username'])){
$where['user_name'] = $data['username'];
}
$where['status'] = [
'neq',Config('code.all_user_delete')
];
$this->getSizeAndPage($data);
$result['tableList'] = $this->getTableListCondition($where);
$result['tableListCount'] = $this->getTableListByConditionCount($where);
$result['pagetotal'] = ceil($result['tableListCount']/$this->size);
$result['page'] = $this->page;
$result['query'] = $query;
return $result;
}


public function getSizeAndPage($data)
{
$this->page = !empty($data['page']) ? $data['page'] : 1;
$this->size = !empty($data['size']) ? $data['size'] : 1;
$this->from = ($this->page - 1) * $this->size;
}


public function getTableListCondition($data,$from=0,$size=5)
{
if(empty($data['status'])){
$data['status']=[
'neq',-1
];
}
$order=['id'=>'ASC'];
$result = $this->where($data)->order($order)->limit($from,$size)->select();
return $result;
}


public function getTableListByConditionCount($where)
{
if(empty($where['status'])){
$where['status']=[
'neq',-1
];
}
$result = $this->where($where)->count();
return $result;
}
}

Controller
public function index()
{
$data = input('param.');
$result = model('User')->getTableList($data);
$this->assign('curr',$result['page']);
$this->assign('userlist',$result['tableList']);
$this->assign('userlistcount',$result['tableListCount']);
$this->assign('query',$result['query']);
$this->assign('start',empty($data['start'])?'':$data['start']);
$this->assign('end',empty($data['end'])?'':$data['end']);
$this->assign('username',empty($data['username'])?'':$data['username']);
$this->assign('pagetotal',$result['pagetotal']);
return $this->fetch();
}

view(js)
<script type="text/javascript" src="__STATIC__/js/laypage/1.2/laypage.js"></script>
<script>
var url = '{:url("User/index")}'+'?{$query}';
//alert(url);
laypage({
cont: 'laypage', //容器。值支持id名、原生dom对象,jquery对象,
pages: '{$pagetotal}', //总页数
skin: '#5FB878', //皮肤
curr :'{$curr}',
jump: function(e, first){ //触发分页后的回调
if(!first){ //一定要加此判断,否则初始时会无限刷新
//alert(url+'?page='+e.curr);
location.href = url+'&page='+e.curr;
}
}
});

</script>


 

 

posted @ 2018-12-10 15:19  Gupq  阅读(892)  评论(0编辑  收藏  举报