1. 程式人生 > >【Layui】帶查詢條件的分頁

【Layui】帶查詢條件的分頁

author:咔咔

wechat:fangkangfk

 

這個前端UI框架是真的讓人又愛又恨吶!想了很久的方案才行的通

 

這是全部原始碼:

{include file="../../../application/admin/view/public/head" /}

<div class="page-container p10">

    <form class="layui-form " method="post" id="pageListForm">
        <div class="layui-input-inline w150">
            <div class="layui-btn-group">
                <a data-full="1" data-href="{:url('addBanner')}" class="layui-btn layui-btn-primary j-iframe"><i class="layui-icon">&#xe654;</i>新增廣告</a>
            </div>

        </div>

        <div class="layui-input-inline w150">
            <select name="vt_id" class="vt_id">
                <option value="">視訊型別</option>
                {foreach name='subject' item='v'}
                <option value="{$v['vt_id']}"  >{$v['vt_name']}</option>
                {/foreach}
            </select>
        </div>


        <div class="layui-input-inline w150">
            <select name="b_targetType" class="b_targetType">
                <option value="">是否跳出</option>
                <option value="1">是</option>
                <option value="2">否</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <input type="text" autocomplete="off" placeholder="請輸入搜尋條件" id="content" class="layui-input" name="b_title" value="">
        </div>

        <a class="layui-btn mgl-20 .j-kaka" id="query"> 查詢</a>


        <table class="layui-table" lay-size="sm">
        <thead>
            <tr >
                <th width="25">ID</th>
                <th width="50">分類</th>
                <th width="100">對應視訊</th>
                <th width="40">縮圖</th>
                <th width="80">點選url</th>
                <th width="30">排序</th>
                <th width="100">展示型別</th>
                <th width="100">是否跳出2是1否</th>
                <th width="100">標題</th>
                <th width="100">內容頁模版</th>
                <th width="100">連結列表</th>
                <th width="130">操作</th>
            </tr>
        </thead>
            <tbody id="tab_list">

            </tbody>
        </table>
    </form>
</div>

<div id="pages" class="center"></div>

{include file="../../../application/admin/view/public/foot" /}

<script type="text/javascript">

    window.onload= function () {
        loadData()  //請求資料
        getPage()     //分頁操作
    }
    var page=1; //設定首頁頁碼
    var limit=3;  //設定一頁顯示的條數
    var total;    //總條數
    function loadData(){
        $.ajax({
            type:"post",
            url:"{url(Banner/index)}",//對應controller的URL
            async:false,
            dataType: 'json',
            data:{
                "page_index":page,
                "page_size":limit,
            },
            success:function(ret){
                total=ret.total_count;

                var data1=ret.data;
                var html= '';
                for(var i=0;i<data1.length;i++){
                    html+='<tr>';
                    html+='<td>'+ data1[i].b_id +'</td>';
                    html+='<td>'+ data1[i].b_id +'</td>';
                    html+='<td>'+ data1[i]['banner_vs_video_name']['vi_title'] +'</td>';
                    html+='<td>'+ data1[i]['b_thumbnailUrl'] +'</td>';
                    html+='<td>'+ data1[i]['b_linkUrl'] +'</td>';
                    html+='<td>'+ data1[i]['b_sort'] +'</td>';
                    html+='<td>'+ data1[i]['b_showType'] +'</td>';
                    html+='<td>'+ data1[i]['b_targetType'] +'</td>';
                    html+='<td>'+ data1[i]['b_title'] +'</td>';
                    html+='<td>'+ data1[i]['b_slaveTitle'] +'</td>';
                    html+='<td>'+ data1[i]['b_linkUrlList']+'</td>';
                    html+='<td>';
                    html+='<a class="layui-badge-rim j-iframe" data-full="1" data-href='+ROOT_PATH+'/index.php/admin/banner/editBanner?b_id='+data1[i]['b_id']+' href="javascript:;" title="編輯">編輯</a>';
                    html+='<a class="layui-badge-rim j-tr-del del" data-href='+ROOT_PATH+'/index.php/admin/banner/delBanner?b_id='+data1[i]['b_id']+' href="javascript:;" title="刪除">刪除</a>';
                    html+='</td>';

                    html+='</tr>';
                }
                $("#tab_list").html(html);
            }
        });
    }


    // 查詢
    $('#query').click(function(){
        var content = $('#content').val();
        var vt_id = $('.vt_id').val();
        var b_targetType = $('.b_targetType').val();

        if(!content && !vt_id && !b_targetType){
            layer.msg('查詢條件不能為空');
            return false
        }

        $.ajax({
            type:"post",
            url:"{url(Banner/index)}",//對應controller的URL
            async:false,
            dataType: 'json',
            data:{
                "page_index":page,
                "page_size":limit,
                "b_title":content,
                "vt_id":vt_id,
                "b_targetType":b_targetType
            },
            success:function(ret){
                total=ret.total_count;
                getPage();

                var data1=ret.data;
                var html= '';
                for(var i=0;i<data1.length;i++){
                    html+='<tr>';
                    html+='<td>'+ data1[i].b_id +'</td>';
                    html+='<td>'+ data1[i].b_id +'</td>';
                    html+='<td>'+ data1[i]['banner_vs_video_name']['vi_title'] +'</td>';
                    html+='<td>'+ data1[i]['b_thumbnailUrl'] +'</td>';
                    html+='<td>'+ data1[i]['b_linkUrl'] +'</td>';
                    html+='<td>'+ data1[i]['b_sort'] +'</td>';
                    html+='<td>'+ data1[i]['b_showType'] +'</td>';
                    html+='<td>'+ data1[i]['b_targetType'] +'</td>';
                    html+='<td>'+ data1[i]['b_title'] +'</td>';
                    html+='<td>'+ data1[i]['b_slaveTitle'] +'</td>';
                    html+='<td>'+ data1[i]['b_linkUrlList']+'</td>';
                    html+='<td>';
                    html+='<a class="layui-badge-rim j-iframe" data-full="1" data-href='+ROOT_PATH+'/index.php/admin/banner/editBanner?b_id='+data1[i]['b_id']+' href="javascript:;" title="編輯">編輯</a>';
                    html+='<a class="layui-badge-rim j-tr-del del" data-href='+ROOT_PATH+'/index.php/admin/banner/delBanner?b_id='+data1[i]['b_id']+' href="javascript:;" title="刪除">刪除</a>';
                    html+='</td>';

                    html+='</tr>';
                }
                $("#tab_list").html(html);
            }
        });
    });


    function getPage(){
        layui.use('laypage', function(){
            var laypage = layui.laypage
                , layer = layui.layer;
            laypage.render({
                elem: 'pages'
                ,count: total //資料總數,從服務端得到
                ,limit:limit
                ,jump: function(obj, first){
                    page=obj.curr;  //改變當前頁碼
                    limit=obj.limit;
                    if(!first){
                        loadData()
                    }
                }
            });
        });
    }




    // 點選刪除
    $(document).on('click','.del',function(){
        var that = $(this),
            href = !that.attr('data-href') ? that.attr('href') : that.attr('data-href');
        layer.confirm('刪除之後無法恢復,您確定要刪除嗎?', {title:false, closeBtn:0}, function(index){
            if (!href) {
                layer.msg('請設定data-href引數');
                return false;
            }
            $.get(href, function(res){
                layer.msg(res.msg);
                if (res.code == 1) {
                    that.parents('tr').remove();
                }
            });
            layer.close(index);
        });
        return false;
    })


    /**
     * 更改資料順序
     */
    layui.use('laypage', function(){
        var laypage = layui.laypage
            , layer = layui.layer,
            $ = layui.$;

        $(document).on('blur','.sort',function(){
            var that = $(this),
                b_sort = that.val();
            var b_id = that.attr('b_id');

            $.post("{:url('banner/editSort')}",{b_sort:b_sort,b_id:b_id},function(res){
                if(res == 1){
                    loadData()
                }
            });
        })
    });




</script>
</body>
</html>

 

這個業務只有一個問題那就是button,我們需要把button的標籤換了,換成a標籤

 

還有一個點就是在點選查詢後,獲取的資料是根據條件查的,在把分頁初始化一次即可

 

其餘的操作跟做跟做分頁是一樣的