一、显示开关按钮
layui分页获取状态的信息(0或1)来判断是或者否(0代表是)
html中
<table class="layui-hide" id="demo" lay-filter="test"></table>
<script type="text/html" id="statusTpl">
<!--这里的 d 和上面的是一样的,表示当前行数据-->
<input type="checkbox" lay-skin="switch" value={{d.articleId}} lay-filter="switchTest" lay-text="是|否" {{ d.articleState== "0" ? "checked" : "" }}>
</script>
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
<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>
js、layui分页中
templet: statusTpl是应用上面id为statusTpl的模板
, cols: [[ //表头
{type: 'checkbox', fixed: 'left'}
, {field: 'articleState', title: '是否展示', width: '10%', align: 'center', templet: statusTpl}
]],
二、开关按钮事件
<script type="text/javascript">
layui.use(['table','form'], function () {
layui.form.on('switch(switchTest)', function (obj) {
var articleState = this.checked ? '0' : '1';
console.log("onoff:" + articleState, "id:" + this.value);
$.post('/pgArticles/updateArticleState', {articleId: this.value, articleState: articleState}, function (data) {
if (data) {
layer.msg("状态修改成功");
} else {
layer.msg("状态修改失败");
}
});
});
});
后台(Controller层)
@PostMapping("/updateArticleState")
public Boolean updateArticleState(String articleId,String articleState){
PgArticles pgArticles = new PgArticles();
pgArticles.setArticleState(Long.parseLong(articleState));
// 根据 UpdateWrapper 条件,更新记录 需要设置sqlset
UpdateWrapper<PgArticles> updateWrapper = new UpdateWrapper<PgArticles>();
updateWrapper.eq("article_id",articleId);
return pgArticlesService.update(pgArticles, updateWrapper);
}