323 lines
11 KiB
JavaScript
323 lines
11 KiB
JavaScript
|
|
var example = null;
|
|||
|
|
var pers = null;
|
|||
|
|
var form = null;
|
|||
|
|
var lumpSumContractId = localStorage.getItem("lumpSumContractId");
|
|||
|
|
var lumpSumType = localStorage.getItem("lumpSumType");
|
|||
|
|
var gxListArr=[];
|
|||
|
|
|
|||
|
|
var selectedGxListInParent = [];// 父页面存储已选数据
|
|||
|
|
|
|||
|
|
var deletedGxList = []; // 用于存储被删除的数据
|
|||
|
|
layui.use(['layer', 'laydate', 'form'], function () {
|
|||
|
|
form = layui.form;
|
|||
|
|
form.render();
|
|||
|
|
form.verify();
|
|||
|
|
pers = checkPermission();
|
|||
|
|
|
|||
|
|
$(document).ready(function() {
|
|||
|
|
init(); // 初始化表格
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
$("#searchBt").click(function () {
|
|||
|
|
example.ajax.reload();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
//重置
|
|||
|
|
$("#resetBt").click(function () {
|
|||
|
|
$("#gxName").val("");
|
|||
|
|
$("#pilePosition").val("");
|
|||
|
|
|
|||
|
|
gxListArr=[];
|
|||
|
|
selectedGxListInParent = [];// 父页面存储已选数据
|
|||
|
|
example.ajax.reload();
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
form.on('submit(formDemo)', function (data) {
|
|||
|
|
const table = $('#dt-table').DataTable();
|
|||
|
|
const rowsData = table.rows().data().toArray(); // 获取所有行数据
|
|||
|
|
|
|||
|
|
if (rowsData.length === 0) {
|
|||
|
|
layer.msg("没有可保存的数据", { icon: 2, time: 2000 });
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const gxList = rowsData.map(row => ({
|
|||
|
|
gxId: row.gxId, // 表格行中的 id 字段作为 gxId
|
|||
|
|
gxName: row.gx, // 包干工序字段
|
|||
|
|
gxZw: row.pilePosition // 桩位字段
|
|||
|
|
}));
|
|||
|
|
|
|||
|
|
// 构造最终对象
|
|||
|
|
const submitData = {
|
|||
|
|
id: lumpSumContractId, // 转换为整数
|
|||
|
|
type: "1",
|
|||
|
|
gxList: gxList
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 提交到后端
|
|||
|
|
$.ajax({
|
|||
|
|
url: ctxPath + "/teamSmallBagDryTreaty/operContract", // 替换为你的实际接口
|
|||
|
|
type: 'POST',
|
|||
|
|
contentType: 'application/json',
|
|||
|
|
data: JSON.stringify(submitData),
|
|||
|
|
success: function (res) {
|
|||
|
|
if (res.res === 1) {
|
|||
|
|
layer.msg("保存成功", { icon: 1, time: 2000 });
|
|||
|
|
setTimeout("reloading()", 2001);
|
|||
|
|
} else {
|
|||
|
|
layer.msg(res.resMsg || "保存失败", { icon: 2, time: 2000 });
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
error: function (xhr, status, error) {
|
|||
|
|
console.error("保存失败:", error);
|
|||
|
|
layer.msg("网络错误,请重试", { icon: 5, time: 2000 });
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
return false; // 阻止表单刷新页面
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
function init() {
|
|||
|
|
example = $('#dt-table').DataTable({
|
|||
|
|
"searching": false, // 禁用搜索
|
|||
|
|
"processing": true, // 加载数据时显示进度状态
|
|||
|
|
"serverSide": false,
|
|||
|
|
'scrollY': "640px",
|
|||
|
|
'scroller': true,
|
|||
|
|
'scrollCollapse': true,
|
|||
|
|
'deferRender': true,
|
|||
|
|
"language": {
|
|||
|
|
"url": ctxPath + "/js/plugin/datatables/Chinese.lang"
|
|||
|
|
},
|
|||
|
|
"ajax": {
|
|||
|
|
"url": ctxPath + "/commonSelect/getGxPilePositions",
|
|||
|
|
"type": "get",
|
|||
|
|
"dataSrc": function (res) {
|
|||
|
|
if (res.res === 1) {
|
|||
|
|
return res.obj; // 使用 obj 作为数据源
|
|||
|
|
} else {
|
|||
|
|
layer.msg(res.resMsg || '数据加载失败');
|
|||
|
|
return [];
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"data": function (d) {
|
|||
|
|
d.contractId = lumpSumContractId;
|
|||
|
|
d.type = lumpSumType;
|
|||
|
|
d.gxName = $("#gxName").val();
|
|||
|
|
d.pilePosition = $("#pilePosition").val();
|
|||
|
|
},
|
|||
|
|
"error": function (xhr, textStatus, errorThrown) {
|
|||
|
|
var msg = xhr.responseText;
|
|||
|
|
console.log(msg);
|
|||
|
|
var response = JSON.parse(msg);
|
|||
|
|
var code = response.code;
|
|||
|
|
var message = response.message;
|
|||
|
|
if (code == 400) {
|
|||
|
|
layer.msg(message);
|
|||
|
|
} else if (code == 401) {
|
|||
|
|
localStorage.removeItem("token");
|
|||
|
|
layer.msg("token过期,请先登录", {shift: -1, time: 1000}, function () {
|
|||
|
|
location.href = ctxPath + '/login.html';
|
|||
|
|
});
|
|||
|
|
} else if (code == 403) {
|
|||
|
|
console.log("未授权:" + message);
|
|||
|
|
layer.msg('未授权');
|
|||
|
|
} else if (code == 500) {
|
|||
|
|
console.log('系统错误:' + message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
"lengthMenu": [ [10, 20, 50], [10, 20, 50] ], // 设置每页显示数据量选项
|
|||
|
|
"paging": false, // 禁用分页
|
|||
|
|
"ordering": false, // 禁用排序
|
|||
|
|
"drawCallback": function() {
|
|||
|
|
// 这里可以根据需要进行额外的处理
|
|||
|
|
},
|
|||
|
|
"dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-4 col-xs-4 hidden-xs'i><'col-xs-8 col-sm-8' p v>><'dt-table-length'l>",
|
|||
|
|
"columns": [
|
|||
|
|
{
|
|||
|
|
"width": "5%",
|
|||
|
|
"orderable": false,
|
|||
|
|
data: function (row, type, set, meta) {
|
|||
|
|
var c = meta.settings._iDisplayStart + meta.row + 1;
|
|||
|
|
return c;
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"data": "gx",
|
|||
|
|
"width": "5%",
|
|||
|
|
"defaultContent": "" // 如果字段缺失,默认显示空字符串
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"data": "pilePosition",
|
|||
|
|
"width": "6%",
|
|||
|
|
"defaultContent": "" // 如果字段缺失,默认显示空字符串
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
"data": "",
|
|||
|
|
"defaultContent": "",
|
|||
|
|
"orderable": false,
|
|||
|
|
"width": "10%",
|
|||
|
|
"render": function (data, type, row) {
|
|||
|
|
var id = row['id'];
|
|||
|
|
var gx = row['gx'];
|
|||
|
|
var gxId = row['gxId'];
|
|||
|
|
var pilePosition = row['pilePosition'];
|
|||
|
|
var clockStatus = row['clockStatus'];
|
|||
|
|
var html = '';
|
|||
|
|
if (clockStatus == 1){
|
|||
|
|
html += '<span class="label label-success">不可操作</span>';
|
|||
|
|
}else{
|
|||
|
|
html += buttonDelView(id, "", pers);
|
|||
|
|
}
|
|||
|
|
return html;
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
],
|
|||
|
|
"order": [] // 在栏目列上显示排序功能
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function buttonDelView(id, permission, pers) {
|
|||
|
|
|
|||
|
|
if (permission != "") {
|
|||
|
|
if ($.inArray(permission, pers) < 0) {
|
|||
|
|
return "";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
var btn = $("<button class='layui-btn layui-btn-xs' style='margin: 5px 5px;' title='删除' onclick='delView(\"" + id + "\")'>删除</button>");
|
|||
|
|
return btn.prop("outerHTML");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 删除工程
|
|||
|
|
* */
|
|||
|
|
function delView(id) {
|
|||
|
|
layer.confirm("确定删除吗?", function (index) {
|
|||
|
|
const table = $('#dt-table').DataTable();
|
|||
|
|
// 遍历所有行,找到匹配 id 的行并删除
|
|||
|
|
table.rows().every(function () {
|
|||
|
|
const data = this.data();
|
|||
|
|
if(data){
|
|||
|
|
if (data.id == id) {
|
|||
|
|
// 从 selectedGxListInParent 中移除对应的项
|
|||
|
|
selectedGxListInParent = selectedGxListInParent.filter(item => item.gxId !== data.id);
|
|||
|
|
table.row(this.index()).remove().draw(); // 删除该行并重绘表格
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
layer.close(index); // 关闭确认框
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 新增选择包干工序及桩位
|
|||
|
|
*/
|
|||
|
|
function lumpSumAdd(){
|
|||
|
|
localStorage.setItem("lumpSumType", "1")
|
|||
|
|
// 获取当前表格中所有数据
|
|||
|
|
const table = $('#dt-table').DataTable();
|
|||
|
|
const allData = table.rows().data().toArray();
|
|||
|
|
let index = layer.open({
|
|||
|
|
title: ['选择包干工序及桩位', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
|
|||
|
|
type: 2,
|
|||
|
|
content: 'lumpSumAddForm.html',
|
|||
|
|
area: ['90%', '90%'],
|
|||
|
|
maxmin: false,
|
|||
|
|
btn: ['确定','关闭'],
|
|||
|
|
|
|||
|
|
success: function(layero, index){
|
|||
|
|
var myIframe = window[layero.find('iframe')[0]['name']];
|
|||
|
|
var fnc = myIframe.receiveDataFromParent(JSON.stringify(selectedGxListInParent)); //aaa()为子页面的方法
|
|||
|
|
},
|
|||
|
|
yes: function (index, layero) {
|
|||
|
|
// 获取弹出层中的form表单元素
|
|||
|
|
var formSubmit = layer.getChildFrame('form', index);
|
|||
|
|
var submited = formSubmit.find('button')[1];
|
|||
|
|
submited.click();
|
|||
|
|
},
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function receiveDataFromChild(gxList,resultStrings) {
|
|||
|
|
selectedGxListInParent = JSON.parse(gxList); // 更新父页面缓存
|
|||
|
|
console.log('收到子页面数据:', gxList);
|
|||
|
|
console.log('收到子页面数据groupedData:', resultStrings);
|
|||
|
|
const cleaned = JSON.parse(resultStrings).map(item => item.replace(/\s+/g, ''));
|
|||
|
|
// 第二步:处理每一项,生成标准化格式
|
|||
|
|
const formatted = cleaned.map(item => {
|
|||
|
|
// 使用正则提取工序名 和 桩位列表
|
|||
|
|
const nameMatch = item.match(/^([\u4e00-\u9fa5]+)(?:工序)/);
|
|||
|
|
const pileMatch = item.match(/桩位(.*)/);
|
|||
|
|
if (!nameMatch || !pileMatch) return item;
|
|||
|
|
const gxName = nameMatch[1]; // 工序名
|
|||
|
|
let piles = pileMatch[1]; // 桩位字符串
|
|||
|
|
// 去重 + 排序(可选)
|
|||
|
|
const uniquePiles = [...new Set(piles.split(','))].filter(Boolean).join(',');
|
|||
|
|
// 返回标准格式
|
|||
|
|
return `${gxName}工序${uniquePiles}桩位`;
|
|||
|
|
});
|
|||
|
|
if(formatted.length > 0){
|
|||
|
|
$("#gxList").val("1");
|
|||
|
|
}
|
|||
|
|
gxListArr = formatGxList(gxList);
|
|||
|
|
selectedGxList(gxListArr)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function selectedGxList(gxListArr) {
|
|||
|
|
|
|||
|
|
const table = $('#dt-table').DataTable();
|
|||
|
|
// 清除之前追加的数据
|
|||
|
|
table.rows().every(function () {
|
|||
|
|
const data = this.data();
|
|||
|
|
if (data.added) {
|
|||
|
|
table.row(this.index()).remove();
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 遍历数据并追加到表格
|
|||
|
|
gxListArr.forEach(item => {
|
|||
|
|
// 构造一行数据对象,对应 columns 定义的字段
|
|||
|
|
const rowData = {
|
|||
|
|
id: item.gxId,
|
|||
|
|
gx: item.gxName,
|
|||
|
|
pilePosition: item.gxZw,
|
|||
|
|
gxId: item.gxId,
|
|||
|
|
clockStatus: 0 // 新增项默认可操作
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 检查是否已存在相同 id 的记录,避免重复添加
|
|||
|
|
const existingRows = table.rows().data();
|
|||
|
|
let exists = false;
|
|||
|
|
for (let i = 0; i < existingRows.length; i++) {
|
|||
|
|
if (existingRows[i].id === rowData.id) {
|
|||
|
|
exists = true;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (!exists) {
|
|||
|
|
table.row.add(rowData).draw(); // 追加新行
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function formatGxList(gxListStr) {
|
|||
|
|
try {
|
|||
|
|
const parsed = JSON.parse(gxListStr);
|
|||
|
|
console.log('格式化后的数据:', parsed)
|
|||
|
|
return parsed.map(item => ({
|
|||
|
|
gxId: item.gxId.trim(), // 直接取并去空格
|
|||
|
|
gxName: item.gxName.trim(),
|
|||
|
|
gxZw: item.gxZw.trim()
|
|||
|
|
}));
|
|||
|
|
} catch (e) {
|
|||
|
|
console.error("JSON 解析失败", e);
|
|||
|
|
return [];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function reloading() {
|
|||
|
|
var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
|
|||
|
|
parent.layer.close(index); //再执行关闭
|
|||
|
|
window.parent.location.reload();
|
|||
|
|
}
|