220 lines
8.6 KiB
JavaScript
220 lines
8.6 KiB
JavaScript
/* * 版 本 PIT-ADMS V7.0.3 敏捷开发框架
|
||
* Copyright (c) 2013-2018 Hexagon PPM
|
||
* 创建人:超级管理员
|
||
* 日 期:2022-02-24 11:10
|
||
* 描 述:LOOKUP
|
||
*/
|
||
|
||
var ProjectId = request('ProjectId');
|
||
var SP_ID = request('SP_ID');
|
||
var table;
|
||
var bootstrap = function ($, learun) {
|
||
"use strict";
|
||
var page = {
|
||
init: function () {
|
||
page.bind();
|
||
page.initData();
|
||
},
|
||
bind: function () {
|
||
// 新增
|
||
$('#lr_add').on('click', function () {
|
||
let newRowData = {};
|
||
|
||
// **获取表格的所有列名**
|
||
let columnNames = table.columns().header().toArray().map(th => $(th).text().trim());
|
||
let firstColumn = columnNames[0]; // 第一列 (recordNumber)
|
||
columnNames = columnNames.slice(1); // 移除第一列,用户不输入它
|
||
|
||
// **获取当前最大 recordNumber 并 +1**
|
||
let maxRecordNum = 0;
|
||
table.rows().data().each(row => {
|
||
let recordNum = parseInt(row[firstColumn]) || 0;
|
||
if (recordNum > maxRecordNum) maxRecordNum = recordNum;
|
||
});
|
||
newRowData[firstColumn] = maxRecordNum + 1; // 生成新的 recordNumber
|
||
|
||
// **让用户输入每列的数据**
|
||
for (let col of columnNames) {
|
||
if (col != firstColumn) {
|
||
let value = prompt(`请输入 ${col} 的值:`);
|
||
if (value === null) return; // 取消输入则终止
|
||
newRowData[col] = value;
|
||
}
|
||
|
||
}
|
||
|
||
// **1. 添加到 DataTable**
|
||
table.row.add(newRowData).draw(false);
|
||
|
||
//// **2. 发送数据到后端**
|
||
//$.ajax({
|
||
// type: "POST",
|
||
// url: "/ZZDT_EC/ec_LookupTable/AddData",
|
||
// contentType: "application/json",
|
||
// data: JSON.stringify(newRowData),
|
||
// success: function (response) {
|
||
// alert("数据添加成功!");
|
||
// },
|
||
// error: function (xhr, status, error) {
|
||
// console.error("添加失败:", error);
|
||
// alert("数据添加失败!");
|
||
// }
|
||
//});
|
||
});
|
||
|
||
// 删除
|
||
|
||
$('#lr_delete').on('click', function () {
|
||
|
||
let selectedRow = table.row(".selected");
|
||
if (!selectedRow.data()) {
|
||
alert("请先选择一行数据!");
|
||
return;
|
||
}
|
||
|
||
let rowData = selectedRow.data(); // 获取选中行数据
|
||
|
||
console.log("最终要删除的 JSON 数据:", rowData);
|
||
// 发送 AJAX 请求给后端
|
||
$.ajax({
|
||
type: "POST",
|
||
url: top.$.rootUrl + '/ZZDT_EC/ec_LookupTable/DeleteData',
|
||
contentType: "application/json",
|
||
data: JSON.stringify({ ProjectId: ProjectId, lookupTableId: SP_ID, strEntity: JSON.stringify(rowData) }),
|
||
success: function (response) {
|
||
learun.alert.success("删除成功!");
|
||
selectedRow.remove().draw(false); // **从表格中移除行**
|
||
},
|
||
error: function (xhr, status, error) {
|
||
console.error("删除失败:", error);
|
||
alert("数据保存失败!");
|
||
}
|
||
});
|
||
});
|
||
// 保存
|
||
|
||
|
||
$('#lr_save').on('click', function () {
|
||
let tableData = [];
|
||
$('#dynamicTable tbody tr').each(function () {
|
||
let rowData = {};
|
||
$(this).find('td').each(function (index) {
|
||
let colName = table.column(index).header().textContent.trim(); // 获取列名
|
||
let cellValue = $(this).find('input').length ? $(this).find('input').val() : $(this).text().trim(); // 获取值
|
||
rowData[colName] = cellValue;
|
||
});
|
||
tableData.push(rowData);
|
||
});
|
||
|
||
console.log("最终要提交的 JSON 数据:", tableData);
|
||
// 发送 AJAX 请求给后端
|
||
$.ajax({
|
||
type: "POST",
|
||
url: top.$.rootUrl + '/ZZDT_EC/ec_LookupTable/SaveData',
|
||
contentType: "application/json",
|
||
data: JSON.stringify({ ProjectId: ProjectId, lookupTableId: SP_ID, strEntity: JSON.stringify(tableData) }),
|
||
success: function (response) {
|
||
learun.alert.success("数据保存成功!");
|
||
},
|
||
error: function (xhr, status, error) {
|
||
console.error("保存失败:", error);
|
||
alert("数据保存失败!");
|
||
}
|
||
});
|
||
});
|
||
},
|
||
initData: function () {
|
||
// 初始化下拉列表并配置数据
|
||
|
||
$.ajax({
|
||
type: 'GET',
|
||
url: top.$.rootUrl + '/ZZDT_EC/ec_LookupTable/GetDataRecord?ProjectId=' + ProjectId + '&lookupTableId=' + SP_ID,
|
||
|
||
traditional: true,
|
||
success: function (response) {
|
||
if (typeof response === "string") {
|
||
console.log("response 是字符串,尝试解析 JSON");
|
||
response = JSON.parse(response);
|
||
}
|
||
|
||
|
||
if (!response || !response.data) {
|
||
console.error("response.data 为空!完整 response:", response);
|
||
alert("数据为空!");
|
||
return;
|
||
}
|
||
|
||
var data = response.data;
|
||
// 获取列名(从返回的第一条数据提取字段)
|
||
const columns = Object.keys(data[0]).map(col => ({
|
||
title: col, data: col
|
||
}));
|
||
|
||
// 销毁已有的 DataTable(避免重复初始化)
|
||
if ($.fn.DataTable.isDataTable("#dynamicTable")) {
|
||
$("#dynamicTable").DataTable().destroy();
|
||
}
|
||
|
||
// 渲染 DataTable
|
||
table = $("#dynamicTable").DataTable({
|
||
data: data,
|
||
columns: columns,
|
||
destroy: true, // 允许销毁旧表格重新渲染
|
||
searching: true, // 开启搜索
|
||
sort: false,
|
||
columnDefs: [
|
||
{ width: "80px", targets: 0 }, // 第一列宽度 80px
|
||
]
|
||
});
|
||
|
||
|
||
|
||
// 双击编辑(除了第一列)
|
||
$('#dynamicTable tbody').on('dblclick', 'td:not(.no-edit)', function () {
|
||
let colIndex = table.cell(this).index().column;
|
||
|
||
if (colIndex === 0) {
|
||
return; // **第一列不允许编辑,直接返回**
|
||
}
|
||
|
||
let cell = table.cell(this);
|
||
let oldValue = cell.data();
|
||
let input = `<input type="text" value="${oldValue}" class="editing-input">`;
|
||
|
||
// 替换单元格为输入框
|
||
cell.data(input).draw(false);
|
||
|
||
// 自动聚焦并处理失去焦点或回车保存
|
||
let inputField = $(this).find('input');
|
||
inputField.focus().select();
|
||
|
||
inputField.on('blur keypress', function (e) {
|
||
if (e.type === 'blur' || (e.type === 'keypress' && e.which === 13)) {
|
||
let newValue = $(this).val();
|
||
cell.data(newValue).draw(false); // 更新 DataTable 数据
|
||
}
|
||
});
|
||
});
|
||
},
|
||
error: function (msg) {
|
||
console.log('2');
|
||
learun.loading(false);
|
||
},
|
||
complete: function (data) {
|
||
console.log('3');
|
||
|
||
}
|
||
});
|
||
|
||
|
||
|
||
}
|
||
|
||
};
|
||
|
||
page.init();
|
||
}
|
||
//文字特殊字符及最大长度检测
|
||
function verify() {
|
||
return true;
|
||
} |