主頁(yè) > 知識(shí)庫(kù) > php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼

php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼

熱門標(biāo)簽:Linux服務(wù)器 服務(wù)器配置 銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 阿里云 團(tuán)購(gòu)網(wǎng)站 Mysql連接數(shù)設(shè)置

一、HTML

table class="layui-hide layui-table" id="spu-data">/table>

二、JS

說(shuō)明:需要引入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 === '超級(jí)爆款') { }} 
  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', //后臺(tái)接口
  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: '貨號(hào)', sort: true},
    {field:'gcolor', title: '顏色組', sort: true},
    {field:'cate', title: '品類', sort: true},
    {field:'price', title: '業(yè)績(jī)', 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ǐng)選擇日期區(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è)績(jī)區(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類中主要的配合步驟

# 接收l(shuí)ayui發(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)用自定義分頁(yè)函數(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)前頁(yè)碼
  $page = $_GET['page'];
 }else{
  $page = 1;
 }

 // $count = $count; # 每頁(yè)顯示條數(shù)

 $countpage = ceil($tot/$count); # 計(jì)算總共頁(yè)數(shù)

 $start = ($page-1)*$count; # 計(jì)算每頁(yè)開(kāi)始位置

 $datas = array_slice($rows, $start, $count); # 計(jì)算當(dāng)前頁(yè)數(shù)據(jù)

 # 獲取上一頁(yè)和下一頁(yè)
 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)加入序號(hào) , 避免使用$i引起的序號(hào)跳位
 $n = 1;
 foreach ($datas as $data) {
  $data['n'] = $n;
  $n++;
 }
 
 $pages['rows'] = $datas;

 return $pages;
}

以上這篇php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • thinkphp5+layui實(shí)現(xiàn)的分頁(yè)樣式示例
  • layui+jquery支持IE8的表格分頁(yè)方法
  • layui 數(shù)據(jù)表格+分頁(yè)+搜索+checkbox+緩存選中項(xiàng)數(shù)據(jù)的方法
  • layui table 獲取分頁(yè) limit的方法
  • 淺談layui分頁(yè)控件field參數(shù)接收對(duì)象的問(wèn)題
  • 淺談layui框架自帶分頁(yè)和表格重載的接口解析問(wèn)題
  • Layui之table中的radio在切換分頁(yè)時(shí)無(wú)法記住選中狀態(tài)的解決方法
  • layui表格分頁(yè) 記錄勾選的實(shí)例
  • Layui實(shí)現(xiàn)數(shù)據(jù)表格默認(rèn)全部顯示(不要分頁(yè))

標(biāo)簽:江蘇 大理 衡水 衢州 棗莊 廣元 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php+layui數(shù)據(jù)表格實(shí)現(xiàn)數(shù)據(jù)分頁(yè)渲染代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266