var layer, tbl;
layui.use(['table', 'layer', 'form'], function () {
var table = layui.table;
layer = layui.layer;
var form = layui.form;
//第一个实例
tbl = table.render({
elem: '#tbl',
url: '/mes/api/staff', //数据接口
page: true, //开启分页
toolbar: '#toolbar',
cols: [[ //表头
{ checkbox: true },
{ field: 'fCode', title: '工号', width: 160, sort: true },
{ field: 'fName', title: '姓名', width: 120, sort: true },
{
field: 'fStatus', title: '状态', width: 100, sort: true, align: 'center', templet: function (d) {
var div = $('
');
var span = $('');
span.addClass('layui-badge');
span.text(d.fStatusName);
if (d.fStatusName == '离职') {
span.addClass('layui-bg-red');
} else if (d.fStatusName == '正常') {
span.attr('style', 'background-color:#5FB878');
}
div.append(span);
return div.html();
}
},
{
field: 'rectime', title: '录入日期', width: 240, sort: true, templet: function (d) {
return (d.rectime == null || d.rectime == undefined) ? '' : timeFormat(d.rectime);
}
},
{ field: 'remark', title: '备注' }
]],
done: () => {
$.ajaxSettings.async = false;
form.render();
}
});
table.on('toolbar(staff)', function (obj) {
if (obj.event == 'add') {
layer.open({
type: 2,
title: '新增员工信息',
content: 'staffAdd.html',
area: ['800px', '400px']
});
} else if (obj.event == 'delete') {
var checkStatus = table.checkStatus('tbl');
console.log(checkStatus);
if (checkStatus.data.length === 0) {
layer.msg('请先选中需要操作的员工!', {
icon: 2,
anim: 6
});
return;
}
var ids = '';
for (var i = 0; i < checkStatus.data.length; i++) {
ids += checkStatus.data[i].id + ',';
}
ids = ids.substr(0, ids.length - 1);
$.ajax({
url: '/mes/api/staff/retired',
type: 'post',
contentType: 'application/json',
dataType: 'text',
data: JSON.stringify(ids),
success: function (res) {
if (res) {
layer.msg('操作成功', {
icon: 1,
anim: 0
});
tbl.reload(true);
} else {
layer.msg('操作失败', {
icon: 2,
anim: 6
});
}
}
});
} else if (obj.event == 'search') {
showToast();
var fcode = $('input[name=fCode]').val();
var name = $('input[name=name]').val();
var status = $('select[name=status]').val();
tbl.reload({
where: {
fcode: fcode,
name: name,
status: status
},
done: () => {
form.render();
form.val('searchForm', {
fCode: fcode,
name: name,
status: status
})
layer.closeAll('loading');
}
});
}
else if (obj.event == 'all') {
showToast();
$('input[name=fCode]').val('');
$('input[name=name]').val('');
$('select[name=status]').val('');
tbl.reload({
where: {
fcode: fcode,
name: name,
status: status
},
done: () => {
layer.closeAll('loading');
}
});
}
});
table.on('rowDouble(staff)', function (obj) {
layer.open({
type: 2,
content: 'staffAdd.html?id=' + obj.data.id,
area: ['800px', '400px']
});
});
table.on('sort(staff)', function (obj) {
tbl.reload({
initSort: obj,
where: {
sort: obj.field,
order: obj.type
}
})
})
});
function reloadTable() {
tbl.reload();
}