layui表格创建一个新增按钮,并插入到表格的最后一行(完整代码展示)
时间: 2023-06-10 17:04:58 浏览: 598
好的,我会尽力回答你的问题,以下是代码展示:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新增按钮</title>
<link rel="stylesheet" href="//res.layui.com/layui/dist/css/layui.css">
<script src="//res.layui.com/layui/dist/layui.js"></script>
</head>
<body>
<table id="demo" lay-filter="test"></table>
<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>
layui.use('table', function(){
var table = layui.table;
//渲染table
table.render({
elem: '#demo',
url: '/demo/table/user/',
toolbar: 'default',
title: '用户数据表',
cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'},
{field: 'username', title: '用户名', width: 120},
{field: 'email', title: '邮箱', minWidth: 150},
{field: 'sign', title: '签名', minWidth: 160},
{field: 'sex', title: '性别', width: 80, templet: '#switchTpl', unresize: true},
{field: 'city', title: '城市', width: 100},
{field: 'experience', title: '积分', width: 80, sort: true},
{field: 'score', title: '评分', width: 80, sort: true},
{field: 'classify', title: '职业', width: 80},
{field: 'wealth', title: '财富', width: 135, sort: true},
{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]],
page: true,
limit: 30,
height: 'full-220'
});
//新增按钮方法
table.on('toolbar(test)', function(obj){
var layEvent = obj.event;
if(layEvent === 'add'){
var data = {
id: '',
username: '',
email: '',
sign: '',
sex: '',
city: '',
experience: '',
score: '',
classify: '',
wealth: ''
};
data.id = Date.now();
data.username = '新用户';
data.email = 'newuser@layui.com';
data.sign = '这个人很懒,什么也没留下';
data.sex = 1;
data.city = '南京';
data.experience = Math.floor(Math.random()*1001);
data.score = Math.floor(Math.random()*101);
data.classify = '程序员';
data.wealth = Math.floor(Math.random()*1000001);
table.reload('demo', {
url: '/demo/table/user/',
where: {data: data},
page: {curr: 1}
});
//插入到表格的最后一行
var tr = $("layui-table-fixed .layui-table tbody tr:last");
var index = tr.attr("data-index");
var colspan = tr.find("td").length;
var tdHtml = `<td colspan=${colspan}>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-0 laytable-cell-checkbox">`;
tdHtml += `<input type="checkbox" name="layTableCheckbox" lay-skin="primary">`;
tdHtml += `</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-1">${data.id}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-2">${data.username}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-3">${data.email}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-4">${data.sign}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-5">`;
tdHtml += `<input type="checkbox" name="sex" value="1" lay-skin="switch" lay-text="男|女" lay-filter="sexDemo" ${(data.sex == 1 ? "checked" : "")}>`;
tdHtml += `</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-6">${data.city}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-7">${data.experience}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-8">${data.score}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-9">${data.classify}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-10">${data.wealth}</div>`;
tdHtml += `<div class="layui-table-cell laytable-cell-1-11">`;
tdHtml += `<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>`;
tdHtml += `<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>`;
tdHtml += `</div>`;
tdHtml += `</td>`;
tr.after(tdHtml);
}
});
});
</script>
</body>
</html>
```
这段代码实现了在`layui`表格中创建一个新增按钮,并将新数据插入到表格的最后一行。在新增事件触发时,我们添加一个新的数据集合,并通过`table.reload()`方法重新渲染表格,然后通过`jQuery`的方法获取表格中的最后一行并在它的后面插入一行新数据。
阅读全文