优化前代码
<script>
// 行序1:field119982
// 行序2:field129442
jQuery(document).ready(function() {
// 获取两个明细表的所有行索引
var detailArr01 = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
var detailArr02 = WfForm.getDetailAllRowIndexStr("detail_2").split(",");
// 定义更新字段样式的函数
function updateFieldStyle(rowIndex, shouldHighlight) {
var $row = jQuery("#field119982_" + rowIndex);
$row.prop("disabled", true); // 禁用字段
var $tr = $row.closest("tr");
if ($tr.length) {
if (shouldHighlight) {
// 高亮样式
$tr.css({
"color": "red",
"background-color": "yellow",
"font-weight": "bold",
"font-style": "italic"
});
} else {
// 恢复默认样式
$tr.css({
"color": "",
"background-color": "",
"font-weight": "",
"font-style": ""
});
}
} else {
console.error("无法找到对应的 tr 元素:", "#field119982_" + rowIndex);
}
}
// 遍历第二个明细表的所有行
for (var i = 0; i < detailArr02.length; i++) {
var rowIndex02 = detailArr02[i];
var replyseq02 = WfForm.getFieldValue("field129442_" + rowIndex02);
// 遍历第一个明细表的所有行
for (var j = 0; j < detailArr01.length; j++) {
var rowIndex01 = detailArr01[j];
var replyseq01 = WfForm.getFieldValue("field119982_" + rowIndex01);
// 如果行序相同,则高亮显示
if (replyseq01 === replyseq02) {
updateFieldStyle(rowIndex01, true);
}
}
}
});
</script>
优化后代码
<script>
jQuery(document).ready(function() {
// 获取两个明细表的所有行索引
var detailArr01 = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
var detailArr02 = WfForm.getDetailAllRowIndexStr("detail_2").split(",");
// 创建一个哈希表来存储第一个明细表的值
var detailMap01 = {};
for (var i = 0; i < detailArr01.length; i++) {
var rowIndex01 = detailArr01[i];
var replyseq01 = WfForm.getFieldValue("field119982_" + rowIndex01);
detailMap01[replyseq01] = rowIndex01;
}
// 定义更新字段样式的函数
function updateFieldStyle(rowIndex, shouldHighlight) {
var $row = jQuery("#field119982_" + rowIndex);
$row.prop("disabled", true); // 禁用字段
var $tr = $row.closest("tr");
if ($tr.length) {
if (shouldHighlight) {
// 高亮样式
$tr.css({
"color": "red",
"background-color": "yellow",
"font-weight": "bold",
"font-style": "italic"
});
} else {
// 恢复默认样式
$tr.css({
"color": "",
"background-color": "",
"font-weight": "",
"font-style": ""
});
}
} else {
console.error("无法找到对应的 tr 元素:", "#field119982_" + rowIndex);
}
}
// 遍历第二个明细表的所有行
for (var i = 0; i < detailArr02.length; i++) {
var rowIndex02 = detailArr02[i];
var replyseq02 = WfForm.getFieldValue("field129442_" + rowIndex02);
// 如果在哈希表中找到了匹配项,则高亮显示
if (detailMap01.hasOwnProperty(replyseq02)) {
updateFieldStyle(detailMap01[replyseq02], true);
}
}
});
</script>