Summary of common usage of layui table component
table is one of the core components of layui. It is used to perform a series of functions and dynamic data operations on tables, covering almost all needs involved in daily business. Supports fixed headers, fixed rows, fixed column left/column right, supports dragging to change column widths, supports sorting, supports multi-level headers, supports custom templates for cells, and supports table overloading (such as search and conditional filtering) etc.), supports check boxes, supports paging, supports cell editing and many other functions.
An example is compiled below:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>layer学习</title> <link href="/Content/mycss.css" rel="stylesheet" /> <link href="/Content/layui/css/layui.css" rel="stylesheet" /> <script src="/Content/layui/layui.js"></script> </head> <body> <!--表格--> <div id="myTable" lay-filter="test"></div> <!--工具栏--> <script type="text/html" id="barDemo"> <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a> <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> </script> <script type="text/html" id="allow"> <!-- 这里的 checked 的状态只是演示 --> <input type="checkbox" name="{{d.Id}}" value="{{d.Id}}" title="允许" lay-filter="allowSetFilter" {{ d.IsAllow == true ? 'checked' : '' }}> </script> <script> layui.use(['table','form'], function () { var table = layui.table; form = layui.form; //*******************************渲染表格********************************** table.render({ //------------------------核心参数 elem: '#myTable' //渲染的dom元素 , url: '/Home/GetUserList' //异步请求接口 , page: true //开启分页 , id: 'elementID' //容器唯一ID , cols: [[ //列设置 { field: 'Id', title: '编号', sort: true, fixed: 'left' } , { field: 'Name', title: '姓名' } , { field: 'Age', title: '年龄' } , { field: 'Role', title: '角色' } , { field: 'CreateTime', title: '创建时间' } , { title: '自定义模板', width: 200 , templet: function (d) { return '姓名:<span style="color: #c00;">' + d.Name + '</span>' } } , { field: 'IsAllow', title: '是否使用', templet: '#allow', unresize: true, align: 'center' } , { fixed: 'right', width: 150, align: 'center', toolbar: '#barDemo' } ]] //-----------------------------异步请求设置 , method: 'post' //异步请求方式 , headers: { hello: 'hengheng' } //在request的header中添加数据 , request: { //request设置,默认值如下 pageName: 'page', limitName: 'limit' } , response: { //response设置,默认值如下 statusName: 'code' , countName: 'count' , dataName: 'data' , msgName: 'msg' } , where: { //向后台添加的额外参数 nameParm: 'u', roleParm: 'o' } //-----------------------加载的其他选项 , done: function (res, curr, count) { //res为接口返回的数据、count为数据总长度 console.log(res); console.log(curr); console.log(count); } , text: { none: '暂无相关数据' //默认:无数据。 } , initSort: { field: 'Id' //排序字段为Id , type: 'desc' //排序方式 asc: 升序、desc: 降序、null: 默认排序 } }) //*******************************监听表格********************************** table.on('tool(test)', function (obj) { //test为lay-filter值 var data = obj.data; //获得当前行数据 var layEvent = obj.event; //获得 lay-event var tr = obj.tr; //获得当前行 tr 的DOM对象 if (layEvent === 'edit') { var id = data.Id; layer.open({ type: 2 , title: '修改' //标题栏 , scrollbar: false , area: ['400px', '300px'] , shade: 0.5 , id: 'layerId' //设定一个id,防止重复弹出 , moveType: 1 //拖拽模式,0或者1 , content: '/Home/EditUserInfo?id=' + id }); } else if (layEvent === 'del') { layer.confirm('真的删除吗?', function (index) { obj.del(); //删除对应行(tr)的DOM结构 layer.close(index); var id = data.Id; //向服务端发送删除指令 $.post("/Home/DeleteUserInfo", { "id": id }, function (result) { if (result.IsSuccess === 1) { layer.msg(result.Msg); table.reload('elementID'); } else { layer.msg(result.Msg); table.reload('elementID'); } }) }); } }); //*******************************监听checkbox******************************** //监听操作----置顶 form.on('checkbox(allowSetFilter)', function (obj) { var pre = { "Id": this.name, "IsAllow": obj.elem.checked }; //alert(this.name+'----'+obj.elem.checked); $.post('/Home/SetAllow', pre, function (result) { if (result.IsSuccess === 1) { layer.msg(result.msg) } else { layer.msg(result.msg) } }) }); }); </script> </body> </html>
For more layui knowledge, please pay attention to the layui usage tutorial column.
The above is the detailed content of Summary of common usage of layui table component. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

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 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.

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.
