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

小云云
小云云Original
2018-01-13 10:33:246175browse

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>

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 项调用刷新方法
 });
})();

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; }
}

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