let form, layer, table, tableIns;
let pageNum = 1, limitSize = 10; // 默认第一页,分页数量为20
layui.use(['form', 'layer', 'table'], function () {
form = layui.form;
layer = layui.layer;
table = layui.table;
pages(1, 10, 1);
// 导入试题库数据
$('.import-btn').on('click', function () {
$("#import-excel").trigger("click");
});
})
function pages(pageNum, pageSize, typeNum) {
let params = getReqParams(pageNum, pageSize, typeNum);
$.ajax({
url: dataUrl + "proteam/pot/itemBank/getItemBanks?token=" + token,
data: params,
type: 'POST',
async: false,
success: function (result) {
if (result.code === 200) {
if (result.data) {
initTable(result.data, result.limit, result.curr)
laypages(result.count, result.curr, result.limit)
}
} else if (result.code === 500) {
layer.alert(result.msg, { icon: 2 })
} else if (result.code === 401) {
logout(1);
}
}, error: function () {
}
});
}
function laypages(total, page, limit) {
layui.use(['laypage'], function () {
let laypage = layui.laypage;
laypage.render({
elem: 'voi-page',
count: total,
curr: page,
limit: limit,
limits: [10, 20, 50, 100],
layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
groups: 5,
jump: function (obj, first) {
if (!first) {
pageNum = obj.curr, limitSize = obj.limit;
pages(obj.curr, obj.limit, null);
}
}
});
})
}
/*初始化表格*/
function initTable(dataList, limit, page) {
let loadingMsg = layer.msg("数据加载中,请稍候...", { icon: 16, scrollbar: false, time: 0, });
tableIns = table.render({
id: 'todayTaskTable',
elem: "#todayTaskTable",
height: "full-150",
data: dataList,
cols: [
[
//表头
{ type: 'checkbox', width: '5%'},
{
title: "序号",
width: '5%',
unresize: true,
align: "center",
templet: function (d) {
return (page - 1) * limit + d.LAY_NUM;
}
},
{
field: "topic",
title: "题干",
width: '30%',
unresize: false,
align: "center",
},
{
field: "topicTypeName",
title: "题干类型",
width: '8%',
unresize: false,
align: "center",
},
{
field: "topicOption",
title: "选项",
width: '36%',
unresize: false,
align: "center",
templet: function (d) {
let html = '';
if (d.topicOption) {
let optionArr = d.topicOption.split('|');
$.each(optionArr, function (index, item) {
html += '
' + item + '
'
});
}
return html;
}
},
{
field: "topicAnswer",
title: "答案",
width: '8%',
unresize: false,
align: "center",
},
{
title: "操作",
width: '8%',
unresize: false,
align: "center",
templet: function (d) {
return "修改|
" +
"删除";
}
},
],
],
limit: limit,
done: function (res, curr, count) {
layer.close(loadingMsg);
},
});
}
// 获取参数
function getReqParams(page, limit, type) {
let obj = {};
if (!type) {
obj = {
page: page + "",
limit: limit + "",
topic: $('#topic').val(),
topicType: $('#topicType').val(),
};
} else {
obj = {
page: 1,
limit: limit,
topic: $('#topic').val(),
topicType: $('#topicType').val(),
};
}
return obj;
}
// 查询
function query(type) {
if (type === 2) {
$('#topic').val('');
$('#topicType').val('');
layui.form.render();
}
pages(1, limitSize);
}
// 新增/修改 试题库
function addData(params, type) {
let title = '';
if (type === 1) {
title = '新增试题'
params = {};
} else {
title = '修改试题';
}
openIframeByParamObj("operItemBank", title, "itemBankForm.html", "52%", "70%", params);
}
// 导入试题库
$("#import-excel").change(function () {
if ($(this).val()) {
let files = $(this)[0].files[0];
let fileType = files.name.substring(
files.name.lastIndexOf(".") + 1,
files.name.length
);
if (fileType === "xlsx" || fileType === "xls") {
excelUpload11(this, dataUrl + "proteam/pot/itemBank/importExcelData?token=" + token)
} else {
layer.msg("仅支持上传文件格式为:xlx、xlsx", { icon: 5, });
$("#import-excel").val("");
}
}
});
// 导出试题库
function exportExcelData() {
let topic = $('#topic').val(),
topicType = $('#topicType').val();
let loadingMsg = layer.msg("数据导出中,请稍候...", { icon: 16, scrollbar: false, time: 0, });
let url = dataUrl + "proteam/pot/itemBank/exportExcelData?topic=" + topic + "&topicType=" + topicType + "&token=" + token;
let xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "blob"; // 转换流
xhr.setRequestHeader("encryption", "encryption");
xhr.onload = function () {
layer.close(loadingMsg);
if (this.status === 200) {
let blob = this.response;
var a = document.createElement("a");
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "试题库" + ".xlsx"; // 文件名
} else {
layer.msg("数据发生异常,请稍后重试", { icon: 16, scrollbar: false, time: 2000 });
}
a.click();
window.URL.revokeObjectURL(url);
};
xhr.send();
}
// 试题库模板下载
function downloadTemplate() {
let loadingMsg = layer.msg('试题库模板下载中,请稍候...', { icon: 16, scrollbar: false, time: 0 });
let url = dataUrl + 'proteam/pot/itemBank/downloadTemplate?token=' + token;
let xhr = new XMLHttpRequest();
xhr.open("get", url, true);
xhr.responseType = "blob"; // 转换流
xhr.setRequestHeader("encryption", "encryption");
xhr.setRequestHeader("encrypt",
sm3(JSON.stringify({})));
xhr.onload = function () {
layer.close(loadingMsg); // 关闭提示层
if (this.status === 200) {
let blob = this.response;
var a = document.createElement("a");
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = "试题库-导入模板.xlsx"; // 文件名
} else {
layer.msg('服务异常,请稍后重试', { icon: 16, scrollbar: false, time: 2000 });
}
a.click()
window.URL.revokeObjectURL(url)
};
xhr.send();
}
// 删除数据
function delData(type,id) {
let selectedIds = [];
if(type === 1){
$('.layui-table-body .layui-form-checked').each(function () {
var tr = $(this).closest('tr');
var id = tr.attr('data-index'); // 获取行索引
// 或者从数据属性获取
var data = table.cache['todayTaskTable'][id];
selectedIds.push(data.id);
});
if(selectedIds.length === 0){
return layer.msg('未选择删除数据',{icon:7});
}
}else{
selectedIds.push(id);
}
layer.confirm('确定删除吗?', function () {
let loadingMsg = layer.msg('正在删除,请稍等...', { icon: 16, shade: 0.01, time: '0' });
let url = dataUrl + "proteam/pot/itemBank/delData";
let params = { delIds: selectedIds };
ajaxRequest2(url, "POST", params, true, function (result) {
layer.close(loadingMsg); // 关闭提示层
if (result.code === 200) {
parent.layer.msg(result.msg, { icon: 1 })
query(1);
} else {
layer.msg(result.msg, { icon: 2 })
}
}, function (xhr) {
layer.close(loadingMsg); // 关闭提示层
error(xhr)
}, null, token);
})
}