Home Web Front-end JS Tutorial How to realize automatic merging of layui data tables (code)

How to realize automatic merging of layui data tables (code)

Sep 07, 2018 pm 04:43 PM
layui data table

The content of this article is about how to realize automatic merging (code) of layui data tables. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Requirement description:

In the data table, for displaying data according to a certain category, there are multiple subdivided nested subdivisions under the category, such as province and city nicknames, province There are multiple cities below, multiple districts below the city, and nicknames below the districts. Cross-row merging is required, and the operation buttons are in categories (that is, the operation buttons need to be merged according to categories).

Requirement analysis:

Except for the operation column, the left column is used as the basic unit for cross-row merging. The current number of merged cells

The operation column can Select any column as the base unit (this needs to be improved).

Original page:

How to realize automatic merging of layui data tables (code)

Processed page:

How to realize automatic merging of layui data tables (code)

##Method:

var execRowspan = function(fieldName,index,flag){
  // 1为不冻结的情况,左侧列为冻结的情况
  let fixedNode = index=="1"?$(".layui-table-body")[index - 1]:(index=="3"?$(".layui-table-fixed-r"):$(".layui-table-fixed-l"));
  // 左侧导航栏不冻结的情况
  let child = $(fixedNode).find("td");
  let childFilterArr = [];
  // 获取data-field属性为fieldName的td
  for(let i = 0; i < child.length; i++){
    if(child[i].getAttribute("data-field") == fieldName){
      childFilterArr.push(child[i]);
    }
  }
  // 获取td的个数和种类
  let childFilterTextObj = {};
  for(let i = 0; i < childFilterArr.length; i++){
    let childText = flag?childFilterArr[i].innerHTML:childFilterArr[i].textContent;
    if(childFilterTextObj[childText] == undefined){
      childFilterTextObj[childText] = 1;
    }else{
      let num = childFilterTextObj[childText];
      childFilterTextObj[childText] = num*1 + 1;
    }
  }
  let canRowspan = true;
  let maxNum = 9999;
  for(let i = 0; i < childFilterArr.length; i++){
    maxNum = $(childFilterArr[i]).prev().attr("rowspan")&&fieldName!="8"?$(childFilterArr[i]).prev().attr("rowspan"):maxNum;
    let key = flag?childFilterArr[i].innerHTML:childFilterArr[i].textContent;
    let tdNum = childFilterTextObj[key];
    let curNum = maxNum<tdNum?maxNum:tdNum;
    let nextKey;

    for(let j =1;j<curNum&&(i+j<childFilterArr.length);j++){
      nextKey = flag?childFilterArr[i+j].innerHTML:childFilterArr[i+j].textContent;
      if(key!=nextKey&&curNum>1){
        canRowspan = true;
        curNum = j;
      }
    }
    if(canRowspan){
      childFilterArr[i].setAttribute("rowspan",curNum);
      if($(childFilterArr[i]).find("p.rowspan").length>0){//设置td内的p.rowspan高度适应合并后的高度
        $(childFilterArr[i]).find("p.rowspan").parent("p.layui-table-cell").addClass("rowspanParent");
        $(childFilterArr[i]).find("p.layui-table-cell")[0].style.height= curNum*38-10 +"px";
      }
      canRowspan = false;
    }else{
      childFilterArr[i].style.display = "none";
    }
    if(maxNum){
      maxNum--;
    }
    if(--childFilterTextObj[key]==0||maxNum==0||(nextKey!=undefined&&key!=nextKey)){
      canRowspan = true;
    }
  }
}
//合并数据表格行
var layuiRowspan = function(fieldNameTmp,index,flag){
  let fieldName = [];
  if(typeof fieldNameTmp == "string"){
    fieldName.push(fieldNameTmp);
  }else{
    fieldName = fieldName.concat(fieldNameTmp);
  }
  for(let i = 0;i<fieldName.length;i++){
    execRowspan(fieldName[i],index,flag);
  }
}
Copy after login

Use:

HTML:

 <p class="box">
<table class="layui-table" id="test" lay-data="{page:true,limit:50}"  lay-filter="test">
  <thead>
  <tr>
    <th lay-data="{field:&#39;province&#39;, width:200}">省</th>
    <th lay-data="{field:&#39;city&#39;, width:200}">市</th>
    <th lay-data="{field:&#39;zone&#39;, width:200}">区</th>
    <th lay-data="{field:&#39;username&#39;, width:200}">昵称</th>
    <th lay-data="{field:&#39;joinTime&#39;, width:150}">加入时间</th>
    <th lay-data="{field:&#39;sign&#39;, minWidth: 180}">签名</th>
    <th lay-data="{field:&#39;8&#39;,align:&#39;right&#39;}">基本操作</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>浙江</td>
    <td>杭州</td>
    <td>西湖区</td>
    <td>贤心1</td>
    <td>2016-11-28</td>
    <td>人生就像是一场修行 A</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>浙江</td>
    <td>这个</td>
    <td>西湖区</td>
    <td>贤心2</td>
    <td>2016-11-29</td>
    <td>人生就像是一场修行 B</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>浙江</td>
    <td>丽水</td>
    <td>莲都区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>浙江</td>
    <td>丽水</td>
    <td>莲都区</td>
    <td>贤心3</td>
    <td>2016-19-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>浙江</td>
    <td>位置</td>
    <td>莲都区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>位置</td>
    <td>莲都区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心3</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 B</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 B</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 C</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  <tr>
    <td>湖北</td>
    <td>这个</td>
    <td>1区</td>
    <td>贤心</td>
    <td>2016-11-30</td>
    <td>人生就像是一场修行 D</td>
    <td>
      <p class="layui-btn-group" style="overflow:visible;">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">按钮1</a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">按钮2</a>
      </p>
    </td>
  </tr>
  </tbody>
</table>
  </p>
Copy after login
layui.use(&#39;table&#39;, function(){
        var table = layui.table;
        table.init(&#39;test&#39;,{done:function(res,curr,count){
          layuiRowspan(&#39;province&#39;,1);
          layuiRowspan([&#39;city&#39;,&#39;zone&#39;,&#39;username&#39;,&#39;joinTime&#39;,&#39;sign&#39;],1);//支持数组
          layuiRowspan("8",1,true);
        }
      });})
Copy after login

Related recommendations:

layui implements dynamic and static data table paging

The above is the detailed content of How to realize automatic merging of layui data tables (code). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up jump on layui login page How to set up jump on layui login page Apr 04, 2024 am 03:12 AM

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

How to get form data in layui How to get form data in layui Apr 04, 2024 am 03:39 AM

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

How layui implements self-adaptation How layui implements self-adaptation Apr 26, 2024 am 03:00 AM

Adaptive layout can be achieved by using the responsive layout function of the layui framework. The steps include: referencing the layui framework. Define an adaptive layout container and set the layui-container class. Use responsive breakpoints (xs/sm/md/lg) to hide elements under specific breakpoints. Specify element width using the grid system (layui-col-). Create spacing via offset (layui-offset-). Use responsive utilities (layui-invisible/show/block/inline) to control the visibility of elements and how they appear.

What is the difference between layui and vue? What is the difference between layui and vue? Apr 04, 2024 am 03:54 AM

The difference between layui and Vue is mainly reflected in functions and concerns. Layui focuses on rapid development of UI elements and provides prefabricated components to simplify page construction; Vue is a full-stack framework that focuses on data binding, component development and state management, and is more suitable for building complex applications. Layui is easy to learn and suitable for quickly building pages; Vue has a steep learning curve but helps build scalable and easy-to-maintain applications. Depending on the project needs and developer skill level, the appropriate framework can be selected.

How to transfer data in layui How to transfer data in layui Apr 26, 2024 am 03:39 AM

The method of using layui to transmit data is as follows: Use Ajax: Create the request object, set the request parameters (URL, method, data), and process the response. Use built-in methods: Simplify data transfer using built-in methods such as $.post, $.get, $.postJSON, or $.getJSON.

What does layui mean? What does layui mean? Apr 04, 2024 am 04:33 AM

layui is a front-end UI framework that provides a wealth of UI components, tools and functions to help developers quickly build modern, responsive and interactive web applications. Its features include: flexible and lightweight, modular design, rich components, Powerful tools and easy customization. It is widely used in the development of various web applications, including management systems, e-commerce platforms, content management systems, social networks and mobile applications.

The difference between layui framework and vue framework The difference between layui framework and vue framework Apr 26, 2024 am 01:27 AM

layui and vue are front-end frameworks. layui is a lightweight library that provides UI components and tools; vue is a comprehensive framework that provides UI components, state management, data binding, routing and other functions. layui is based on a modular architecture, and vue is based on a componentized architecture. layui has a smaller ecosystem, vue has a large and active ecosystem. The learning curve of layui is low, and the learning curve of vue is steep. Layui is suitable for small projects and rapid development of UI components, while vue is suitable for large projects and scenarios that require rich functions.

What language is layui framework? What language is layui framework? Apr 04, 2024 am 04:39 AM

The layui framework is a JavaScript-based front-end framework that provides a set of easy-to-use UI components and tools to help developers quickly build responsive web applications. Its features include: modular, lightweight, responsive, and has complete documentation and community support. layui is widely used in the development of management backend systems, e-commerce websites, and mobile applications. The advantages are quick start-up, improved efficiency, and easy maintenance. The disadvantages are poor customization and slow technology updates.

See all articles