1,目标代码
cols: [
[
{type: 'checkbox'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'operate', title: '操作', align: 'center', width: 100, templet: function(d){
return '<a style="color: #409EFF;" lay-event="edit">修改</a>' +
'<a style="color: #409EFF;margin-left: 10px" lay-event="del">删除</a>'
}}
]
],
2,现有的动态数据
从后端传来的数据 ColsList 只有中间这段
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
{field: 'name', title: '标题', width:110, align: 'center', halign: 'center'},
3,前端组合
首部添加
{type: 'checkbox'}
尾部添加
{field: 'operate', title: '操作', align: 'center', width: 100, templet: function(d){
return '<a style="color: #409EFF;" lay-event="edit">修改</a>' +
'<a style="color: #409EFF;margin-left: 10px" lay-event="del">删除</a>'
}}
最终代码:
//初始化
var ColsList= ${ColsList};
//首部添加
ColsList.splice(0,0,{type: 'checkbox'})
//尾部添加
ColsList.push({field: 'operate', title: '操作', align: 'center', width: 100, templet: function(d){
return '<a style="color: #409EFF;" lay-event="edit">修改</a>' +
'<a style="color: #409EFF;margin-left: 10px" lay-event="del">删除</a>'
}});
//最后放入layui的表格属性中
cols: [
ColsList
],
4,知识笔记
4.1,splice
语法:
// 索引从0开始计数
arrayObject.splice(索引,删除项目数量,添加新项目1,…,添加新项目n)
4.2,push
语法:
arrayObject.push(添加新项目1,…,添加新项目n)
push() 方法可把它的参数顺序添加到 arrayObject 的尾部。它直接修改 arrayObject,而不是创建一个新的数组。