script
的barDemo
,通过lay-event="edit"
来控制点击var lists = [];
function renderTable(){
table.render({
elem: '#list',
data:lists,
limit: 10,
cols: [[
{type:'checkbox'}
,{field:'arttitle', minWidth:200, title: '标题'}
,{field:'itemname', minWidth:100, title: '类型', sort: true}
,{field:'addusername', width:80, title: '编辑人'}
,{field:'applydate', title: '发布时间', minWidth: 100}
,{fixed: 'right',title: '操作',toolbar: '#barDemo',minWidth: 250}
]]
,page: false,
loading: true
});
}
filters,lists,total
要先定义。filters里面是查询需要的内容,total = res.total;
是Ajax获取的总页数,lists = res.data;
是Ajax获取的数据内容,renderLaypage(page);
渲染分页,renderTable();
渲染表格var filters = {}; // 搜索条件
var lists = [];
let total;
function listArticles(page) {
myloading(true)//加载效果
let para = {
page: page,
filters: {
arttitle:$("#arttitle").val(),
starttime: $("#starttime").val(),
endtime: $("#endtime").val(),
itemcode:$("#itemname").val(),
diccode:$('[data-role="option"]:selected').attr('data-diccode')
},
sorts: {},
}
listArticle(para).then(res => {
if(res.code == 1) {
myloading(false);
total = res.total;
lists = res.data;//Ajax数据
renderLaypage(page); //渲染分页
renderTable(); //渲染表格
}
})
}
let total;//定义总页数
function renderLaypage(page) {
laypage.render({
elem: 'pagesize',
count: total,
curr: page, //获取起始页
hash: 'fenye', //自定义hash值
layout: ['prev', 'page', 'next'],
jump: function(obj, first) {
if(!first) {
listArticles(obj.curr);
}
}
});
}
starttime
和endtime
时间
laydate.render({
elem: '#starttime', //开始日期
trigger: 'click'
});
laydate.render({
elem: '#endtime', //结束日期
trigger: 'click'
});
$("#search").click(function() {
listArticles(1);
})
lay-event="edit"
来定位获取的。tool(list)
中list是table
的id名,编辑就跳转,获取artid删除
table.on('tool(list)', function(obj) {
if(obj.event == 'edit') {//编辑
window.open('/system/article/edit.html?artid=' + obj.data.artid, '_self')
}
if(obj.event == 'del') {//删除
layer.confirm('确认删除该文章吗?', function(index) {
var para = {
artid: obj.data.artid
}
removeArticle(para).then(function(res) {
if(res.code == 1) {
listArticles(1);
layer.msg(res.msg, {
time: 2000,
icon: 6
});
} else {
layer.msg(res.msg, {
time: 2000,
icon: 5
});
}
})
})
}
})
table.checkStatus('list')
list是表格id,获取artid名来删除,lay-filter="batchDel"
定位到表格,batchremoveArticle调取批量删除的接口,通过getCheckData
来获取选中的数据function getCheckData() {
var checkStatus = table.checkStatus('list')
var data = checkStatus.data;
var artids = [];
data.forEach(function(v, i) {
artids.push(v.artid)
})
return artids
}
//批量删除
form.on('submit(batchDel)', function(data) {
if(getCheckData().length == 0) {
layer.msg("请先选择批量删除的内容", {time: 2000,icon: 5});
return;
}
layer.confirm('确认删除所有选中的内容吗?',{
cancel: function(index, layero){
}
}, function(index) {
var artids = getCheckData()
let para = {
"artid": artids,
}
batchremoveArticle(para).then(function(res) {
if(res.code == 1) {
listArticles(1);
layer.close(index);
layer.msg(res.msg, {time: 2000,icon: 6}); //成功信息提示
} else {
layer.msg(res.msg, {time: 2000,icon: 5});
}
})
return false
}, function(index, layero){})
})
最后贴一份完整的代码
后台-文章管理-文章列表