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']) { //獲取當前頁碼
$page = $_GET['page'];
}else{
$page = 1;
}
// $count = $count; # 每頁顯示條數(shù)
$countpage = ceil($tot/$count); # 計算總共頁數(shù)
$start = ($page-1)*$count; # 計算每頁開始位置
$datas = array_slice($rows, $start, $count); # 計算當前頁數(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字符串替換str_replace()函數(shù)四種用法
str_replace() 函數(shù)以其他字符替換字符串中的一些字符(區(qū)分大小寫)。本文重點給大家介紹PHP字符串替換str_replace()函數(shù)四種用法,感興趣的朋友一起看看吧2017-10-10
淺析Yii2 GridView 日期格式化并實現(xiàn)日期可搜索教程
這篇文章主要介紹了Yii2 GridView 日期格式化并實現(xiàn)日期可搜索教程的相關(guān)資料,需要的朋友可以參考下2016-04-04
PHP如何實現(xiàn)阿里云短信sdk靈活應用在項目中的方法
這篇文章主要介紹了PHP如何實現(xiàn)阿里云短信sdk靈活應用在項目中的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
TP5(thinkPHP5)框架mongodb擴展安裝及特殊操作示例
這篇文章主要介紹了TP5(thinkPHP5)框架mongodb擴展安裝及特殊操作,結(jié)合實例形式分析了MongoDB擴展的基本安裝、配置、模型操作以及使用Push操作實現(xiàn)的數(shù)據(jù)添加、更新等方法,需要的朋友可以參考下2018-09-09
函數(shù)中使用require_once問題深入探討 優(yōu)雅的配置文件定義方法推薦
這篇文章主要介紹了函數(shù)中使用require_once問題深入探討,優(yōu)雅的配置文件定義方法推薦,最終用一個靜態(tài)類來定義配置文件獲勝,完美解決函數(shù)中使用require_once的問題,需要的朋友可以參考下2014-07-07
PHP實現(xiàn)QQ登錄的開原理和實現(xiàn)過程
本文主要給大家講述了PHP實現(xiàn)QQ登錄的開原理以及相關(guān)的代碼實現(xiàn)過程,對此有興趣和需要的朋友參考下吧。2018-02-02

