Home Web Front-end JS Tutorial Detailed explanation of Layui table component initialization loading data and other examples

Detailed explanation of Layui table component initialization loading data and other examples

Jan 13, 2018 am 10:33 AM
layui table initialization

This article mainly introduces the implementation code of using Layui table component to initialize data loading, data refresh table and parameter transmission. Friends in need can refer to it. I hope it can help everyone.

Background

The author has been using bootstrap table before, because the Layui framework is mainly used in the current project, so I followed Layui table, but there were some problems when using it, and of course it was strange. It’s a pot you’re not familiar with!

Problems:

1. When using the [Convert Static Table] method officially provided by Layui to initialize the load, the id cannot be found error (my own fault)

2 , The problem of passing parameters (it can be regarded as Layui's official fault)

The table loading and refreshing scheme used by the author

There is a page with a tree on the left and a table on the right. The default table loading Full data, when you click on the tree node, the table will filter, it is a very simple requirement!

We will not talk about the use of tree here, we will only post the relevant methods of table!

First post the source form code:

<table class="layui-table" lay-filter="EditListTable">
 <thead>
 <tr>
  <th lay-data="{field:&#39;Index&#39;, width:60}">序号</th>
  <th lay-data="{field:&#39;UserId&#39;, width:80}">销售ID</th>
  <th lay-data="{field:&#39;UserName&#39;, width:80}">姓名</th>
  <th lay-data="{field:&#39;Year&#39;, width:70}">年份</th>
  <th lay-data="{field:&#39;M01&#39;, width:80}">一月</th>
  <th lay-data="{field:&#39;M02&#39;, width:80}">二月</th>       
  <th lay-data="{field:&#39;YearValue&#39;, width:80, fixed: &#39;right&#39;}">年度</th>
  <th lay-data="{width:100, align:&#39;center&#39;, toolbar: &#39;#barDemo1&#39;, fixed: &#39;right&#39;}">操作</th>
 </tr>
 </thead>
</table>
<script type="text/html" id="barDemo1">
 <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
</script>
Copy after login

Explain directly in the code through comments:

(function () {
 //加载列表的后端 url
 var getListUrl = '';
 //对于任意一个 table,按照官方的说法,有三种不同的初始化渲染方式,不多介绍,而这里使用的方式姑且看做第三种:转换静态表格 方式
 //转换静态表格方式,自然首先需要有一个已经存在的表格,然后再通过 js 方式转化为 Layui 表格
 //无论哪种方式的 Layui table 初始化自然需要配置项
 //通过转化的方式初始化 Layui table,配置项部分可以在 源table中,部分在js中,源 table 的源代码上文已经给出,下面给出一个示例的 js 中的配置项
 var tableOptions = {
  url: getListUrl, //请求地址
  method: 'POST', //方式
  id: 'listReload', //生成 Layui table 的标识 id,必须提供,用于后文刷新操作,笔者该处出过问题
  page: false, //是否分页
  where: { type: "all" }, //请求后端接口的条件,该处就是条件错误点,按照官方给出的代码示例,原先写成了 where: { key : { type: "all" } },结果并不是我想的那样,如此写,key 将是后端的一个类作为参数,里面有 type 属性,如果误以为 key 是 Layui 提供的格式,那就大错特错了
  response: { //定义后端 json 格式,详细参见官方文档
   statusName: 'Code', //状态字段名称
   statusCode: '200', //状态字段成功值
   msgName: 'Message', //消息字段
   countName: 'Total', //总数字段
   dataName: 'Result' //数据字段
  }
 };
 //
 layui.use(['table', 'layer'], function () {//layui 模块引用,根据需要自行修改
  var layer = layui.layer, table = layui.table;
  //表初始化
  var createTable = function () {
   table.init('EditListTable', tableOptions);// table lay-filter
  };
  //表刷新方法
  var reloadTable = function (item) {
   table.reload("listReload", { //此处是上文提到的 初始化标识id
    where: {
     //key: { //该写法上文已经提到
      type: item.type, id: item.id
     //}
    }
   });
  };
  //表初始化
  createTable();
  //其他和 tree 相关的方法,其中包括 点击 tree 项调用刷新方法
 });
})();
Copy after login

Backend method:

//本示例中,后台代码写法
public ActionResult GetGoalList(string type, string id)
{
  //
}

//如果按照官方文档条件项,应该是下面的写法

public ActionResult GetGoalList(keyItem key)
{
  //
}
public class keyItem
{
 public string id { get; set; }
 public string type { get; set; }
}
Copy after login

Related recommendations:

Use VUE element-ui to write a reusable Table component

jquery dataTable method to get a certain row of data

jquery dataTable loads data in the background and paging

The above is the detailed content of Detailed explanation of Layui table component initialization loading data and other examples. 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)

What to do if the dynamic link library initialization routine fails What to do if the dynamic link library initialization routine fails Dec 29, 2023 am 10:30 AM

Solution: 1. Reinstall the application; 2. Repair or reinstall the DLL; 3. System restore or checkpoint recovery; 4. Scan using System File Checker (SFC); 5. Check startup items and services; 6. Use Tools; 7. Check official documentation or forums; 8. Consider security software; 9. Check the event viewer; 10. Seek expert help, etc.

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 initialize the computer in win7 How to initialize the computer in win7 Jan 07, 2024 am 11:53 AM

The win7 system is a very excellent high-performance system. During the continuous use of win7, many friends are asking how to initialize the computer in win7! Today, the editor will bring you how to restore the factory settings of a win7 computer. Related information on how to initialize the computer in win7: Detailed instructions with pictures and text. Steps: 1. Open the "Start Menu" and enter. 2. Click to enter the settings at the bottom of the left side. 3. In the Win10 update and recovery settings interface, select. 4. Click below "Remove all content and reinstall Windows". 5. You can see the following "Initialization" settings, and then click. 6. Enter the "Your computer has multiple drives" setting option. There are two options here, you can choose according to the situation.

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.

Fix Unable to initialize graphics system error on PC Fix Unable to initialize graphics system error on PC Mar 08, 2024 am 09:55 AM

Many gamers have encountered the frustrating issue of the game failing to initialize the graphics system. This article will delve into the common reasons behind this problem and find simple yet effective solutions that will get you back on the board and beating the level in no time. So, if you are getting Unable to initialize graphics system error message in Rollercoaster Tycoon, Assassin’s Creed, Tony Hawk’s Pro Skater, etc., then follow the solutions mentioned in this article. Initialization error Unable to initialize the graphics system. Graphics cards are not supported. Fix the Unable to initialize the graphics system error message To resolve the Unable to initialize the graphics system error in games like Rollercoaster Tycoon, Assassin's Creed, Tony Hawk's Pro Skater, etc., you can try the following workarounds: Update your graphics card driver in Compatibility Mode

How to reset win7 network settings How to reset win7 network settings Dec 26, 2023 pm 06:51 PM

The win7 system is a very excellent high-performance system. Recently, many friends of the win7 system are looking for how to initialize the network settings in win7. Today, the editor will bring you the details of win7 computer network initialization. Let’s take a look at the tutorial. Detailed tutorial on how to initialize network settings in win7: Graphical steps: 1. Click the "Start" menu, find and open the "Control Panel", and then click "Network and Sharing Center". 2. Then find and click "Change Adapter Device". 3. Next, in the window that opens, right-click "Local Area Connection" and then click "Properties". 4. After opening it, find "Internet Protocol Version (TCP/IPv4)" and double

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.

See all articles