search
HomeWeb Front-endLayui TutorialHow layui implements data paging function

How layui implements data paging function

Feb 07, 2021 am 11:03 AM
layuiPagination

How layui implements data paging function

Let’s first take a look at the demo screen of the official website.

Specific code:

The page introduces layui.css and layui.js

<div id="pTable" style="width: 1200px;">
  <table class="layui-table" id="layui_table_id" lay-filter="test">
  </table>
<div id="laypage"></div>
 </div>

Front-end js

var limitcount = 10;
 var curnum = 1;
 //列表查询方法
 function productsearch(productGroupId,start,limitsize) {
  layui.use([&#39;table&#39;,&#39;laypage&#39;,&#39;laydate&#39;], function(){
   var table = layui.table,
    laydate=layui.laydate,
    laypage = layui.laypage;
   table.render({
    elem: &#39;#layui_table_id&#39;
    , url: &#39;<%=path%>/xx/pListQuery.html?pId=&#39;+productGroupId+&#39;¤tPage=&#39;+ start+&#39;¤tNumber=&#39; + limitsize
    /*, where:{pagename:start,pagelimit:limitsize} //传参*/
    , cols: [[
     {field: &#39;productId&#39;, title: &#39;ID&#39;, width: &#39;170&#39;, sort: true}
     , {field: &#39;productName&#39;, title: &#39;名称&#39;, width: &#39;450&#39;}
     , {field: &#39;productState&#39;, title: &#39;状态&#39;, width: &#39;100&#39;}
     , {field: &#39;effectTime&#39;, title: &#39;生效时间&#39;, width: &#39;120&#39;, sort: true}
     , {field: &#39;invalidTime&#39;, title: &#39;失效时间&#39;, width: &#39;120&#39;, sort: true}
     , {field: &#39;productCost&#39;, title: &#39;成本&#39;, width: &#39;100&#39;, sort: true}
     , {field: &#39;poperation&#39;, title: &#39;操作&#39;, width: &#39;100&#39;,fixed: &#39;right&#39;, toolbar: &#39;#barDemo&#39;}
    ]]
    , page: false
    , height: 430
    ,done: function(res, curr, count){
     //如果是异步请求数据方式,res即为你接口返回的信息。
     //如果是直接赋值的方式,res即为:{data: [], count: 99} data为当前页数据、count为数据总长度
     laypage.render({
      elem:&#39;laypage&#39;
      ,count:count
      ,curr:curnum
      ,limit:limitcount
      ,layout: [&#39;prev&#39;, &#39;page&#39;, &#39;next&#39;, &#39;skip&#39;,&#39;count&#39;,&#39;limit&#39;]
      ,jump:function (obj,first) {
       if(!first){
        curnum = obj.curr;
        limitcount = obj.limit;
        //console.log("curnum"+curnum);
        //console.log("limitcount"+limitcount);
        //layer.msg(curnum+"-"+limitcount);
        productsearch(productGroupId,curnum,limitcount);
       }
      }
     })
    }
   })
 
   //监听工具条
   table.on(&#39;tool(test)&#39;, function(obj){ //注:tool是工具条事件名,test是table原始容器的属性 lay-filter="对应的值"
    var data = obj.data //获得当前行数据
     ,layEvent = obj.event; //获得 lay-event 对应的值
    if(layEvent === &#39;detail&#39;){
     viewLableInfo(data.attrId);
     layer.msg(data.attrId);
    } else if(layEvent === &#39;del&#39;){
     layer.msg(&#39;删除&#39;);
    } else if(layEvent === &#39;edit&#39;){
     layer.msg(&#39;编辑操作&#39;);
    }
   });
   //常规用法
   laydate.render({
    elem: &#39;#createDate&#39;
   });
   //常规用法
   laydate.render({
    elem: &#39;#processingTime&#39;
   });
 
  });
 }
  var pId = &#39;${pGBean.pgId }&#39;;
productsearch(pId, curnum, limitcount);

Business logic layer

@Override
  public String queryList (HttpServletRequest request) {
   String total = "";
   String pId = request.getParameter("pId");
   int currentNumber = Integer.parseInt(request.getParameter("currentNumber"));
  String currentPage = request.getParameter("currentPage") == null ? "1" : request.getParameter("currentPage");
  //分页处理,显示第一页的30条数据(默认值)
  PageHelper.startPage(Integer.parseInt(currentPage), currentNumber);
  List<PExl> list = exportDao.queryList (pId);
  if(list.size() > 0){
   total = list.get(0).getTotal();
  }
  
  Page page = PageHelper.localPage.get();
  if(page!=null){
   page.setCurrentPage(Integer.parseInt(currentPage));
  }
  PageHelper.endPage();
 
  JSONObject jsonObject = new JSONObject();
  jsonObject.put("code", 0);
  jsonObject.put("msg", "");
  jsonObject.put("count", total);
  jsonObject.put("data", list);
  //System.out.println("json:----" + jsonObject.toString());
  return jsonObject.toString();
  }

sql

The sql can be written like this when calculating the total totle

COUNT(*) OVER(PARTITION BY 1) AS TOTAL

Related recommendations:layui tutorial

The above is the detailed content of How layui implements data paging function. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:码农教程. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor