layui+php实现后台页面展示搜索分页排序删除

2021/6/20 20:26:54

本文主要是介绍layui+php实现后台页面展示搜索分页排序删除,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

把需要的CSS+JS放入根目录public中

<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
    </script>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body>

<div class="demoTable">
    搜索ID:
    <div class="layui-inline">
        <input class="layui-input" name="name" id="demoReload" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>

<table class="layui-hide" id="LAY_table_user" lay-filter="user">
<script type="text/html" id="img">
    <div><img src="/storage/{{d.img}}" width="30" height="30" alt="加载失败.."></div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</table>
<!-- 注意:如果你直接复制所有代码到本地,上述 JS 路径需要改成你本地的 -->
<script>
    layui.use('table', function(){
        var table = layui.table;

        //方法级渲染
        table.render({
            elem: '#LAY_table_user'//表格ID
            ,url: '/admin/index/orders'//后台数据路径
            ,cols: [[
                {field:'id', width:80, title: 'ID', sort: true,fixed: true}
                ,{field:'name', width:80, title: '用户名'},
                {field:'img', width:80,title: '品牌LOGO',templet:'#img'},
                {field:'price', width:80, title: '性别', sort: true},
                {fixed: 'right', width:200, align:'center',title: '操作' ,toolbar: '#barDemo'}
            ]]
            ,id: 'testReload'
            ,page: true//开启分页
            ,limit:10
            ,height: 500
        });
        //进行查看编辑删除
        table.on('tool(user)', function(obj){
            var data = obj.data;
            console.log(data)
            if(obj.event === 'detail'){
                layer.msg('ID:'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.alert('编辑行:<br>'+ JSON.stringify(data))
            }
        });
        //搜索
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            id: demoReload.val(),
                            name:demoReload.val(),
                        }
                    }
                });
            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>

controller里面的方法获取

url: '/admin/index/orders'   //后台数据路径
//页面数据
public function orders(){
        $par = $this->request->param();
        $page = $this->request->param('page');
        $limit = $this->request->param('limit');
        if (isset($par['key'])){
            $data = lists::where('id','like',$par['key']['id'])->whereOr('name','like',$par['key']['name'])->paginate(3)->toArray();
            return json(['data'=>$data['data'],'code'=>0,'msg'=>'']);
        }else{
            $data = lists::paginate($limit)->toArray();
            $count = lists::select()->count();
            return json(['data'=>$data['data'],'code'=>0,'msg'=>'','count'=>$count]);
        }
    }
//页面
public function order_list(){
        return View::fetch('index/order_list');
    }


这篇关于layui+php实现后台页面展示搜索分页排序删除的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程