欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年10月26日 10:13:25   作者:yach_yu  
今天小編就為大家分享一篇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)文章

最新評論