search
HomeWeb Front-endLayui TutorialBasic parameter application in the table module in layui

Basic parameter application in the table module in layui

layui's table module is a major focus and is as friendly as possible in terms of basic parameters, that is, ensuring the prerequisites of functionality while avoiding overly complicated configurations.

Basic parameters generally appear in the following scenarios:

Scenario 1: The content in the following lay-data is the basic parameters items, remember: the value must be in single quotes

<table lay-data="{height:300, url:&#39;/api/data&#39;}" lay-filter="demo"> …… </table>

Scenario 2: The key value in the following method is the basic parameter item

table.render({
  height: 300
  ,url: &#39;/api/data&#39;
});

More scenarios: The following options are the basic parameters. Item object

> table.init(&#39;filter&#39;, options); //转化静态表格
> var tableObj = table.render({});
  tableObj.reload(options); //重载表格

Let’s take a look at the basic elements?

1. elem - The binding element specifies the original table container, which is only applicable to the rendering method of table.render()

HTML:
<table id="test"></table>     
 
JS:
table.render({ //其它参数在此省略
  elem: &#39;#test&#39; //或 elem: document.getElementById(&#39;test&#39;) 等
});

2. Set the table header, here Contains many values ​​and is a two-dimensional array. If you use "method-level rendering" of tables, then you need to use this parameter to set the table. For example:

JS:
table.render({
  cols:  [[ //标题栏
    {checkbox: true}
    ,{field: &#39;id&#39;, title: &#39;ID&#39;, width: 80}
    ,{field: &#39;username&#39;, title: &#39;用户名&#39;, width: 120}
  ]]
});
 
它等价于:
<table class="layui-table" lay-data="{基础参数}" lay-filter="test">
  <thead>
    <tr>
      <th lay-data="{checkbox:true}"></th>
      <th lay-data="{field:&#39;id&#39;, width:80}">ID</th>
      <th lay-data="{field:&#39;username&#39;, width:180}">用户名</th>
    </tr>
  </thead>
</table>

The following is an example of a secondary header:

JS:
table.render({
  cols:  [[ //标题栏
    {field: &#39;username&#39;, title: &#39;联系人&#39;, width: 80, rowspan: 2} //rowspan即纵向跨越的单元格数
    ,{field: &#39;amount&#39;, title: &#39;金额&#39;, width: 80, rowspan: 2}
    ,{align: &#39;center&#39;, title: &#39;地址&#39;, colspan: 3} //colspan即横跨的单元格数,这种情况下不用设置field和width
  ], [
    {field: &#39;province&#39;, title: &#39;省&#39;, width: 80}
    ,{field: &#39;city&#39;, title: &#39;市&#39;, width: 120}
    ,{field: &#39;county&#39;, title: &#39;详细&#39;, width: 300}
  ]]
});
 
它等价于:
<table class="layui-table" lay-data="{基础参数}">
  <thead>
    <tr>
      <th lay-data="{field:&#39;username&#39;, width:80}" rowspan="2">联系人</th>
      <th lay-data="{field:&#39;amount&#39;, width:120}" rowspan="2">金额</th>
      <th lay-data="{align:&#39;center&#39;}" colspan="3">地址</th>
    </tr>
    <tr>
      <th lay-data="{field:&#39;province&#39;, width:80}">省</th>
      <th lay-data="{field:&#39;city&#39;, width:120}">市</th>
      <th lay-data="{field:&#39;county&#39;, width:300}">详细</th>
    </tr>
  </thead>
</table>

It should be noted that the table module supports Infinitus headers, and you can continue to expand in the above way. The core point lies in the two parameters rowspan and colspan

The next step is some parameter settings in the header

table.render({
  cols: [[
    {field: &#39;id&#39;} //其它参数在此省略
    ,{field: &#39;username&#39;}
  ]]
});
 
等价于:
<th lay-data="{field:&#39;id&#39;}"></th>
<th lay-data="{field:&#39;username&#39;}"></th>

title: set the title name

table.render({
  cols: [[
    {title: &#39;邮箱&#39;} //其它参数在此省略
    ,{title: &#39;签名&#39;}
  ]]
});
 
等价于:
<th lay-data="{}">邮箱</th> (PS:也可以把标题写在lay-data里面,即 title:&#39;邮箱&#39;)
<th lay-data="{}">签名</th>

width: set Fixed column width . The setting of column width is usually necessary (except for "special columns", such as checkbox columns, toolbars, etc.), which is related to the overall beauty of the table.

table.render({
  cols: [[
    {width: 80} //其它参数在此省略
    ,{width: 120}
  ]]
});
 
等价于:
<th lay-data="{width:80}"></th>
<th lay-data="{width:120}"></th>

checkbox: Set the checkbox. If set to true, it means that the content of this column is a check box, usually it is placed in the first column.

table.render({
  cols: [[
    {checkbox: true} //其它参数在此省略
    ,{field: &#39;id&#39;, title:&#39;ID&#39;, width: 100}
  ]]
});
 
等价于:
<th lay-data="{checkbox:true}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

It should also be noted that LAY_CHECKED here is used in conjunction with checkbox. If set to true, it means that all checkboxes are selected by default.

table.render({
  cols: [[
    {checkbox: true, LAY_CHECKED: true} //其它参数在此省略
    ,{field: &#39;id&#39;, title:&#39;ID&#39;, width: 100}
  ]]
});
 
等价于:
<th lay-data="{checkbox:true, LAY_CHECKED: true}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

space: Set the gap column. If set to true, define a 15px width column without any content.

table.render({
  cols: [[ //其它参数在此省略
    {space: true}
    ,{field: &#39;id&#39;, title:&#39;ID&#39;, width: 100}
  ]]
});
 
等价于:
<th lay-data="{space:true}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

sort: Whether sorting is required. If true is set, the sorting icon will be displayed in the corresponding table header, thereby enabling the sorting function for the column.

Note: It is not recommended to enable sorting for columns whose values ​​exist: numbers and ordinary characters, because it will enter lexicographic comparison. For example: 'Xianxin' > '2' > '100', this may not be the result you want, but the dictionary sorting algorithm (ASCII code comparison) is like this. You can also learn about the dictionary for details. sequence knowledge.

table.render({
  cols: [[
    {sort:true} //其它参数在此省略
    ,{field:&#39;id&#39;, title:&#39;ID&#39;, width:100}
  ]]
});
 
等价于:
<th lay-data="{sort:true}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

fixed: Whether fixed columns are required. If true or 'right' is set, the corresponding column will be fixed to the left or right and will not scroll with the scroll bar.

table.render({
  cols: [[
    {fixed:true} //其它参数在此省略
    ,{field:&#39;id&#39;, title:&#39;ID&#39;, width:100}
    ,{field:&#39;username&#39;, title:&#39;姓名&#39;, width:120, fixed:&#39;right&#39;} //固定列在右
  ]]
});
 
等价于:
<th lay-data="{sort:true}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>
<th lay-data="{field:&#39;username&#39;, width:120, fixed:&#39;right&#39;}">姓名</th>

edit: Whether to allow editing. If set to true, the cells in the corresponding column will be allowed to be edited. Currently, only input editing of type="text" is supported.

table.render({
  cols: [[
    {edit:&#39;text&#39;} //其它参数在此省略
    ,{field:&#39;id&#39;, title:&#39;ID&#39;, width:100}
  ]]
});
 
等价于:
<th lay-data="{edit:&#39;text&#39;}"></th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

templet: Custom template. By default, the content of the cell is output exactly according to the content returned by the data interface. If you want to add links and other elements to the cells of a certain column, you can easily achieve this with the help of this parameter. This is a very practical function, and the content of your table will be rich and diverse.

table.render({
  cols: [[
    {field:&#39;title&#39;, title: &#39;文章标题&#39;, width: 200, templet: &#39;#titleTpl&#39;} //这里的templet值是模板元素的选择器
    ,{field:&#39;id&#39;, title:&#39;ID&#39;, width:100}
  ]]
});
 
等价于:
<th lay-data="{field:&#39;title&#39;, width: 200, templet: &#39;#titleTpl&#39;}">文章标题</th>
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>

In fact, templet can also be directly a piece of html content, such as:

templet: &#39;<div><a href="/detail/{{d.id}}" class="layui-table-link">{{d.title}}</a></div>&#39; 
注意:这里一定要被一层 <div></div> 包裹,否则无法读取到模板

toolbar: bind toolbar. Usually you need to add similar operation buttons such as view, edit, and delete in each row of the table, and the tool parameter is born for this, so you can implement various operation functions very conveniently.

The tool parameter is used in exactly the same way as the templet parameter. It usually accepts a selector or a segment of HTML characters.

table.render({
  cols: [[
    {field:&#39;id&#39;, title:&#39;ID&#39;, width:100}
    ,{fixed: &#39;right&#39;, width:150, align:&#39;center&#39;, toolbar: &#39;#barDemo&#39;} //这里的toolbar值是模板元素的选择器
  ]]
});
 
等价于:
<th lay-data="{field:&#39;id&#39;, width:100}">ID</th>
<th lay-data="{fixed: &#39;right&#39;, width:150, align:&#39;center&#39;, toolbar: &#39;#barDemo&#39;}"></th>

The following is the template corresponding to the toolbar, which can be stored anywhere on the page:

<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-mini" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
  
  <!-- 这里同样支持 laytpl 语法,如: -->
  {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-mini" lay-event="check">审核</a>
  {{#  } }}
</script>
 
注意:属性 lay-event="" 是模板的关键所在,值可随意定义。

Next, we use the toolbar events of the table module to complete different operating functions:

//监听工具条
table.on(&#39;tool(test)&#39;, function(obj){ //注:tool是工具条事件名,test是table原始容器的属性 lay-filter="对应的值"
  var data = obj.data; //获得当前行数据
  var layEvent = obj.event; //获得 lay-event 对应的值
  var tr = obj.tr; //获得当前行 tr 的DOM对象
 
  if(layEvent === &#39;detail&#39;){ //查看
    //do somehing
  } else if(layEvent === &#39;del&#39;){ //删除
    layer.confirm(&#39;真的删除行么&#39;, function(index){
      obj.del(); //删除对应行(tr)的DOM结构,并更新缓存
      layer.close(index);
      //向服务端发送删除指令
    });
  } else if(layEvent === &#39;edit&#39;){ //编辑
    //do something
    
    //同步更新缓存对应的值
    obj.update({
      username: &#39;123&#39;
      ,title: &#39;xxx&#39;
    });
  }
});

For more layui related knowledge, please pay attention to layui framework.

The above is the detailed content of Basic parameter application in the table module in layui. 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
How do I use Layui's flow module for infinite scrolling?How do I use Layui's flow module for infinite scrolling?Mar 18, 2025 pm 01:01 PM

The article discusses using Layui's flow module for infinite scrolling, covering setup, best practices, performance optimization, and customization for enhanced user experience.

How do I use Layui's element module to create tabs, accordions, and progress bars?How do I use Layui's element module to create tabs, accordions, and progress bars?Mar 18, 2025 pm 01:00 PM

The article details how to use Layui's element module to create and customize UI elements like tabs, accordions, and progress bars, highlighting HTML structures, initialization, and common pitfalls to avoid.Character count: 159

How do I customize the appearance and behavior of Layui's carousel module?How do I customize the appearance and behavior of Layui's carousel module?Mar 18, 2025 pm 12:59 PM

The article discusses customizing Layui's carousel module, focusing on CSS and JavaScript modifications for appearance and behavior, including transition effects, autoplay settings, and adding custom navigation controls.

How do I use Layui's carousel module to create image sliders?How do I use Layui's carousel module to create image sliders?Mar 18, 2025 pm 12:58 PM

The article guides on using Layui's carousel module for image sliders, detailing steps for setup, customization options, implementing autoplay and navigation, and performance optimization strategies.

How do I configure Layui's upload module to restrict file types and sizes?How do I configure Layui's upload module to restrict file types and sizes?Mar 18, 2025 pm 12:57 PM

The article discusses configuring Layui's upload module to restrict file types and sizes using accept, exts, and size properties, and customizing error messages for violations.

How do I use Layui's layer module to create modal windows and dialog boxes?How do I use Layui's layer module to create modal windows and dialog boxes?Mar 18, 2025 pm 12:46 PM

The article explains how to use Layui's layer module to create modal windows and dialog boxes, detailing setup, types, customization, and common pitfalls to avoid.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft