layui.use(['table','layer','form'],function(){var data=document.getElementById("dz").value;var table =layui.table;var layer=layui.layer;var form =layui.form;
table.render({
elem:'#test',
url:data+'/bookAction.action?methodName=query',
method:'post',
cols:[[
{field:'book_id',title:'书本ID',width:100,height:80},
{field:'book_name',title:'书本名称',width:100,height:80},
{field:'book_name_pinyin',title:'书本拼音',width:100,height:80},
{field:'book_category_name',title:'书本类型',width:100,height:80},
{field:'book_author',title:'书本作者',width:100,height:80},
{field:'book_price',title:'书本价格',width:100,height:80},
{field:'book_image',title:'书本图片',width:100,height:80},
{field:'publishing',title:'书本出版社',width:100,height:80},
{field:'book_desc',title:'书本简介',width:100,height:80},
{field:'zt_name',title:'书本状态',width:100,height:80},
{field:'deploy_datetime',height:80, width:300, title: '上架时间',templet:"
{field:'sales_volume',title:'书本销量',width:100,height:80},
{fixed: 'right', title:'操作', toolbar: '#lineBtns', width:150}
]],
page:'true',
id:'testReload'});//上方菜单操作栏(查询、以及 增加 按钮 )
var $ = layui.$, active ={//查询
reload: function () {var name = $('#book_name');//书籍名称 根据 id来取值
console.log(name.val());//执行重载
table.reload('testReload', {
page: {
curr:1
//重新从第 1 页开始
},where: {
key:'book_name',
book_name: name.val(),
}
});
}, add: function () {//添加
layer.open({//弹出框
type: 1,
title:'添加书本',
maxmin:true,
shadeClose:true, //点击遮罩关闭层
area: ['80%', '80%'],
content: $('#box1'),
btn: ['确定', '取消'],
yes: function (index, layero) {//确定执行函数
console.log(layero);//执行添加方法
$.getJSON(data+"/bookAction.action?methodName=addBook", {
book_name: $("#book_name2").val(),
book_name_pinyin: $("#book_name_pinyin").val(),
book_category_id: $("#book_category_id").val(),
book_author: $("#book_author").val(),
book_price: $("#book_price").val(),
publishing: $("#publishing").val(),
book_desc: $("#book_desc").val()
}, function (data) {/*根据后台返回的参数来进行判断*/
if (data > 0) {//弹出提示层
layer.alert('添加成功', {icon: 1, title: '提示'}, function (i) {
layer.close(i);
layer.close(index);//关闭弹出层
$("#book")[0].reset()//重置form
})//重载表格
table.reload('testReload', {
page: {
curr:1//重新从第 1 页开始
}
})
}else{//提示添加失败
layer.msg('添加失败')
}
})
}, cancel: function (index, layero) {//取消//当点击取消按钮
$("#book")[0].reset()//重置form 根据id
layer.close(index)
}
});
}
}
$('.layui-form .layui-btn').on('click', function () {var type = $(this).data('type');
active[type]? active[type].call(this) : '';
});/*表格 行内操作(编辑 以及 删除 按钮操作)*/table.on('tool(test)', function(obj){var data = obj.data; //获得当前行数据
var urlex=document.getElementById("dz").value;var tr=obj.tr//活动当前行tr 的 DOM对象
console.log(data);var layEvent = obj.event; //获得 lay-event 对应的值(也可以是表头的 event 参数对应的值)
if(layEvent === 'del'){ //删除
layer.confirm('确定删除吗?',{title:'删除'}, function(index){//向服务端发送删除指令og
$.getJSON(urlex+'/bookAction.action?methodName=remove',{book_id: data.book_id}, function(ret){
layer.close(index);//关闭弹窗
table.reload('testReload', {//重载表格})
});
layer.close(index);
});
}else if(layEvent === 'edit'){
layer.open({
type:1
,skin: 'layui-layer-molv',area: ['380px', '270px']
,title: ['编辑书本信息','font-size:18px']
,btn: ['确定', '取消']
,shadeClose:true,shade:0
,maxmin: true
,content:$('#box1')
,success:function(layero,index){
$('#book_name2').val(data.book_name);
$('#book_name_pinyin').val(data.book_name_pinyin);
$('#book_category_id').val(data.book_category_id);
$('#book_author').val(data.book_author);
$('#book_price').val(data.book_price);
$('#publishing').val(data.publishing);
$('#book_desc').val(data.book_desc);
},yes:function(index,layero){
$.getJSON(urlex+'/bookAction.action?methodName=edit',{
book_name: $('#book_name2').val(),
book_name_pinyin: $('#book_name_pinyin').val(),
book_category_id: $('#book_category_id').val(),
book_author: $('#book_author').val(),
book_price: $('#book_price').val(),
publishing: $('#publishing').val(),
book_desc: $('#book_desc').val(),
book_id: data.book_id,
},function(data){
if(data>0){
layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
layer.close(i);
layer.close(index);
$("#book")[0].reset()
})
table.reload('testReload',{
page:{
curr:1}
})
}
});
}
});
}
});
})