1、layui的table.render表格渲染,常用属性

表格组件 table:它以表格的承载方式对数据进行渲染、重载、排序、统计、分页等等一系列交互操作。

方法配置渲染:table.render(options);

正如标题,只写渲染表单的一些常用的属性,并解释其作用和注意事项。更详细的看API:https://layui.dev/docs

table.render({
/**-------基础属性*/
elem: '#test', // 绑定元素选择器,就是你这个表格绑定的是html里的那个
   cols: [[]], //表头属性集
data: JSON.stringify(db), //设置 data 模式时,这个属于你选择的得到列表值的方式,前台赋虚拟值才这么给值,后台给值要删掉
id: "swtable", //这个就是给你渲染好的table直接起个名字,你其他地方就可以直接用这个名字用table的方法,等于你直接获得了整个表单,不写这个属性默认是:html:table的id属性值
toolbar: true, //这其实就是上方的工具栏,toolbar: true:开启右侧小工具,toolbar: 'default',开启左侧小工具。toolbar: '<div>xxx</div>'自定义。toolbar: '#template-id' 把自定义的写在html
defaultToolbar: ['filter', 'exports', ,'print' ],//'filter':列筛选;'exports':导出;'print':打印,这是配置右上角工具栏的,图标将根据数组值的顺序排列,还能扩展工具图标,2.9.12+版本:重写内置工具、具体的看layuiAPI
totalRow: "",//是否开启合计行区域
page: {curr: 1 },//用于开启分页,{curr: 1 }:重置到第一页,防止当前在第3页但筛选后数据不足导致空白。支持传入 laypage 组件的基础属性(jump,elem 除外),false:不开启分页。默认不开启
limit: 10, //每页显示的条数
limits: [10,20,50,100], //每页条数的选择项,值需对应 limits 参数的选项。优先级低于 page 属性中的 limit 属性。
loading: false, //设置数据请求时的加载动画,需开启 url 选项才生效。 默认是开启的。也能自定义"<i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i>"
title: "", //定义 table 的大标题(在文件导出等地方会用到)
text: "无数据,请点击上面[添加数据]添加", //自定义文本,如空数据时的异常提示等。
autoSort: false,//是否开启排序,默认是开启的
initSort: {
field: 'id', // 按 id 字段排序
type: 'desc' // 降序排序
}, //按字段排序
size: "100sm",设置表格其他尺寸。可选值:sm|md|lg
before: function(options){
console.log(options); // 当前实例属性选项
options.where.abc = 123; // 修改或额外追加 where 属性
}, //2.7+,数据渲染之前的回调函数。 option是你的table.render本身
done: function(res, curr, count, origin){
console.log(res); // 得到当前渲染的数据
console.log(curr); // 得到当前页码
console.log(count); // 得到数据总量
console.log(origin); // 回调函数所执行的来源 --- 2.8.7+
}, //数据渲染完毕的回调函数。
error: function(e, msg) {
console.log(e, msg)
}, //2.6+
complete: function(xhr, ts) {
console.log(xhr, ts)
}, //2.8.18+,数据接口请求完成后执行,无论成功还是失败均会触发

/**-------异步属性:异步属性本质也是基础属性,当开启 url 属性时才有效*/
url: "", //发送异步请求的 URL。默认会自动传递两个参数:?page=1&limit=30(该参数可通过 request 属性自定义)page 代表当前页码、limit 代表每页数据条数。
method: "post", //请求的方式,默认:get
where: {}, //请求的其他参数。如:where: {token: 'sasasas', id: 123}, 这就是模糊查询之类的
headers: , //请求的数据头参数。如:headers: {token: 'sasasas'}
contentType: "application/json", //请求的内容编码类型。若要发送 json 内容,可设置: contentType: 'application/json'
dataType: "json", //2.7+,请求的数据类型,默认 json。
request: {
pageName: 'curr', // 页码的参数名称,默认:page
limitName: 'nums' // 每页数据条数的参数名,默认:limit
}, //用于对默认的分页相关的请求参数 page,limit 重新设定名称。 比如你实体类的分页参数不叫他这个名字,就这里改他前端的传参。
parseData: function(res){ // res 即为原始返回的数据
return {
"code": res.status, // 解析接口状态
"msg": res.message, // 解析提示文本
"count": res.total, // 解析数据长度
"data": res.data.item // 解析数据列表
};
}, //很多时候,您接口返回的数据格式并不一定都符合 table 默认规定的格式, 此时我们可以借助 parseData 回调函数将数据解析并转换为默认规定的格式。
ajax: function (origOptions, type) { //自定义 ajax 请求,origOptions - 包含了原始的请求参数,type - 执行 ajax 请求的来源,'table' 或 'treeNodes'
$.ajax({
url: origOptions.url,
data: origOptions.data,
dataType: origOptions.dataType,
})
.done(function (data) {
// 调用原始的 success 回调
origOptions.success(data);
})
.fail(function (xhr, status, error) {
// 调用原始的 error 回调
origOptions.error(xhr, status, error);
})
.always(function () {
// 调用原始的 complete 回调
if (typeof origOptions.complete === "function") {
origOptions.complete();
}
});
}
});
posted @ 2026-10-22 11:53  椿树之久  阅读(10)  评论(0)    收藏  举报