php+layui數(shù)據(jù)表格實現(xiàn)數(shù)據(jù)分頁渲染代碼
一、HTML
<table class="layui-hide layui-table" id="spu-data"></table>
二、JS
說明:需要引入layui中的table和laytpl模板引擎,laytpl可以自定義事件及自定義數(shù)據(jù)字段等
<!-- 拼接圖片 --> <script type="text/html" id="pimg"> <img class="img" onmouseover="divIn(event)" onmouseout="divOut(event)" onmousemove="divIn(event)" src="__PUBLIC__/{{d.pimgurl}}t_{{d.pimgname}}" alt=""> </script> <!-- 查看詳情按鈕 --> <script type="text/html" id="spu_detail"> <button class="layui-btn layui-btn-xs layui-btn-primary spu_detail" artnum="{{d.artnum}}" value="{{d.basic_id}}" onclick="spuDetail(event)">查看詳情</button> </script> <script type="text/html" id="hotcake_color"> {{# if (d.hotcake === '超級爆款') { }} <span style="display: block;background-color: #CCFFCC;">{{ d.hotcake }}</span> {{# } else if(d.hotcake === '大爆款') { }} <span style="display: block;background-color: #99CCCC;">{{ d.hotcake }}</span> {{# } else if(d.hotcake === '小爆款') { }} <span style="display: block;background-color: #FFCCCC;">{{ d.hotcake }}</span> {{# } else if(d.hotcake === '熱銷款') { }} <span style="display: block;background-color: #FFFFCC;">{{ d.hotcake }}</span> {{# } else { }} <span style="display: block;background-color: #CCFFFF;">{{ d.hotcake }}</span> {{# } }} </script>
<script type="text/javascript"> layui.use(['form','laydate','layer','table','laytpl'],function(){ var laydate = layui.laydate; var layer = layui.layer; var table = layui.table; var laytpl = layui.laytpl; //---SPU數(shù)據(jù)--------------------------------------------- var spu_table = table.render({ elem: '#spu-data', //html中table窗口的id height: 800, url: '__URL__/spu_data', //后臺接口 toolbar: true, loading: true, text: { none: '空空如也' }, title: 'spu數(shù)據(jù)', size: 'sm', page: { layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], limit: 20, limits: [20,30,50,100,200,5000] }, cols: [[ {field:'n', title: 'i', width: 55}, {field:'', title: '圖', width: 31, templet: '#pimg'}, // templet 引用laytpl中的自定義模板 {field:'', title: '查看詳情', width: 120, templet: '#spu_detail'}, // 引用laytpl中的自定義模板 {field:'artnum', title: '貨號', sort: true}, {field:'gcolor', title: '顏色組', sort: true}, {field:'cate', title: '品類', sort: true}, {field:'price', title: '業(yè)績', sort: true}, {field:'sales', title: '銷量', sort: true}, {field:'hotcake', title: '熱銷程度', templet: '#hotcake_color', sort: true}, {field:'sumcost', title: '商品成本', sort: true} ]] }); // 搜索重載數(shù)據(jù) $('#spudata_search').click(function(){ // 獲取日期的值 var date = $('#spusearch_date').val(); if (!date) { layer.msg('請選擇日期區(qū)間搜索', { time: 2000 }); return false; } var perfor_val = $('#perfor_val').val();; var hot_type = $('#hot_type').val(); var artnum = $('#artnum').val(); var cate_id = $('#cate_id').val(); // 只選其一條件 if (perfor_val && hot_type) { layer.msg('業(yè)績區(qū)間和爆款類型只選其一', { time: 2000 }); return false; } // 數(shù)據(jù)重載 spu_table.reload({ // 發(fā)送條件 where: { artnum: artnum, perfor_val: perfor_val, hot_type: hot_type, cate_id: cate_id, date: date, act: 'reload' }, page: { layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], curr: 1 } }); }) }) </script>
三、PHP
#這里是PHP類中主要的配合步驟 # 接收layui發(fā)送的limit if (trim($_GET['limit'])) { $limit = trim($_GET['limit']); }else{ $limit = 15; } # 按某字段排序,$rows為數(shù)據(jù)數(shù)組 $sort_num = array_column($rows,'num'); array_multisort($sort_num,SORT_DESC,$rows, SORT_DESC); # 調(diào)用自定義分頁函數(shù) $datas = array(); $datas = showpage($rows,$limit); $items = array(); # 返回layui數(shù)據(jù)格式 $items['data'] = $datas['rows']; $items['code'] = 0; $items['msg'] = 'ok'; $items['count'] = $datas['tot']; exit(json_encode($items));
# showpage函數(shù) function showpage($rows,$count){ $tot = count($rows); // 總數(shù)據(jù)條數(shù) if ($_GET['page']) { //獲取當(dāng)前頁碼 $page = $_GET['page']; }else{ $page = 1; } // $count = $count; # 每頁顯示條數(shù) $countpage = ceil($tot/$count); # 計算總共頁數(shù) $start = ($page-1)*$count; # 計算每頁開始位置 $datas = array_slice($rows, $start, $count); # 計算當(dāng)前頁數(shù)據(jù) # 獲取上一頁和下一頁 if ($page > 1) { $uppage = $page-1; }else{ $uppage = 1; } if ($page < $countpage) { $nextpage = $page+1; }else{ $nextpage = $countpage; } $pages['countpage'] = $countpage; $pages['page'] = $page; $pages['uppage'] = $uppage; $pages['nextpage'] = $nextpage; $pages['tot'] = $tot; //循環(huán)加入序號 , 避免使用$i引起的序號跳位 $n = 1; foreach ($datas as &$data) { $data['n'] = $n; $n++; } $pages['rows'] = $datas; return $pages; }
以上這篇php+layui數(shù)據(jù)表格實現(xiàn)數(shù)據(jù)分頁渲染代碼就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
php結(jié)合js實現(xiàn)點擊超鏈接執(zhí)行刪除確認(rèn)操作
本文是一篇超級簡單的php結(jié)合js實現(xiàn)點擊超鏈接執(zhí)行js代碼,并確認(rèn)是否刪除數(shù)據(jù)庫數(shù)據(jù),附上全部源代碼,給需要的朋友參考下吧2014-10-10YII2框架自定義全局函數(shù)的實現(xiàn)方法小結(jié)
這篇文章主要介紹了YII2框架自定義全局函數(shù)的實現(xiàn)方法,總結(jié)分析了YII2框架自定義全局函數(shù)相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2020-03-03php使用strtotime和date函數(shù)判斷日期是否有效代碼分享
php使用strtotime和date函數(shù)進(jìn)行檢驗判斷日期是否有效代碼分享,大家參考使用吧2013-12-12Thinkphp將二維數(shù)組變?yōu)闃?biāo)簽適用的一維數(shù)組方法總結(jié)
這篇文章主要介紹了Thinkphp將二維數(shù)組變?yōu)闃?biāo)簽適用的一維數(shù)組方法,總結(jié)了常見的轉(zhuǎn)化數(shù)組方法,非常實用,需要的朋友可以參考下2014-10-10如何用PHP來實現(xiàn)一個動態(tài)Web服務(wù)器
這篇文章介紹了如何用PHP來實現(xiàn)一個動態(tài)Web服務(wù)器,文章思路清晰,并附有演示代碼地址,需要的朋友可以參考下2015-07-07