'use strict'; /** * 项目信息js * * @author EllisRan. * @date 2019/3/19 * @version */ function setTypeTable(types, type, fun = 'type') { $('#'+ type + '-'+ fun +'-table').empty(); if (fun === 'used') { $('#' + type + '-' + fun + '-table').append(` 合同 `); } types.forEach(t => { const typeRow = ` `; $('#'+ type + '-'+ fun +'-table').append(typeRow); }); } $(document).ready(() => { autoFlashHeight(); const funs = ['type', 'used']; for (const fun of funs) { $('#add-' + fun + '-btn').click(function(){ const type = $('#' + fun + '-tabs a.active').attr('type'); const newType = ` `; $('#'+ type + '-' + fun + '-table').append(newType); }); $('body').on('click', '.remove-' + fun + '-btn', function() { const input = $(this).parents('td').siblings('td').eq(0).children('input'); input.attr('disabled', true); // 文字加删除线并移除foucs if (input.val() === '') { input.removeAttr('placeholder'); } input.css('text-decoration', 'line-through'); input.blur(); $(this).remove(); checkAndShowTypesBtn(fun); }); $('body').on('blur', '#contract-' + fun + '-set input[name="value"]', function() { checkAndShowTypesBtn(fun); }); $('#set-' + fun + '-btn').click(function() { const { new_subProject_types, new_tender_types } = checkAndShowTypesBtn(fun, true); const data = { type: 'set-contract-' + fun, } if (fun === 'type') { data.contract_type = { contract_type: new_subProject_types, tender_contract_type: new_tender_types }; } else if (fun === 'used') { data.contract_used = { contract_used: new_subProject_types, tender_contract_used: new_tender_types } } postData(`/sp/${spid}/contract/audit/save`, data, function (res) { toastr.success('设置成功'); if (fun === 'type') { tender_type = new_tender_types; subProject_type = new_subProject_types; } else if (fun === 'used') { tender_used = new_tender_types; subProject_used = new_subProject_types; } setTypeTable(new_tender_types, 'tender', fun); setTypeTable(new_subProject_types, 'subProject', fun); checkAndShowTypesBtn(fun); }); }); $('#cancel-' + fun + '-btn').click(function() { toastr.warning('已取消改动'); if (fun === 'type') { setTypeTable(tender_type, 'tender', fun); setTypeTable(subProject_type, 'subProject', fun); } else if (fun === 'used') { setTypeTable(tender_used, 'tender', fun); setTypeTable(subProject_used, 'subProject', fun); } checkAndShowTypesBtn(fun); }); } function checkAndShowTypesBtn(fun, return_type = false) { const new_subProject_types = []; const new_tender_types = []; const type = ['subProject', 'tender']; for (const t of type) { $('#'+ t + '-' + fun + '-table tr').each(function () { const input = $(this).find('input[name="value"]'); console.log(input.val()); if (input && input.val() !== undefined && !input.prop('disabled')) { const value = input.val().trim(); if (value) { if (t === 'subProject') { new_subProject_types.push(value); } else { new_tender_types.push(value); } } } }); } if (fun === 'type') { // 判断数组是否相同,但别改变原数组顺序 if (_.isEqual(_.sortBy(new_tender_types), _.sortBy(tender_type)) && _.isEqual(_.sortBy(new_subProject_types), _.sortBy(subProject_type))) { $('#show-'+ fun +'-btn').hide(); } else { $('#show-' + fun + '-btn').show(); } } else if (fun === 'used') { if (_.isEqual(_.sortBy(new_tender_types), _.sortBy(tender_used)) && _.isEqual(_.sortBy(new_subProject_types), _.sortBy(subProject_used))) { $('#show-'+ fun +'-btn').hide(); } else { $('#show-' + fun + '-btn').show(); } } if (return_type) { return { new_subProject_types, new_tender_types }; } } // 审批设置 $('#contract-shenpi-set input[type="checkbox"]').change(function() { postData(`/sp/${spid}/contract/setting/update`, { type: 'page_show', openContractSubProjectShenpi: $('#openContractSubProjectShenpi')[0].checked, openContractPaySubProjectShenpi: $('#openContractPaySubProjectShenpi')[0].checked, openContractTenderShenpi: $('#openContractTenderShenpi')[0].checked, openContractPayTenderShenpi: $('#openContractPayTenderShenpi')[0].checked, }, function (result) { }); }); // 附加属性设置 // 立即获取表格 tbody 并绑定事件(确保脚本加载时即可初始化) const $tbody = $('#attribute-set-table'); // 保存初始 HTML,在关闭时恢复(保证再次打开时为初始状态) let _initialColSetTbodyHtml = $tbody.html(); // 更新上下移链接显示状态 function updateMoveButtons() { const $currentRows = $tbody.find('tr'); const totalRows = $currentRows.length; $currentRows.each(function(index) { const $row = $(this); const $upLink = $row.find('.move-up'); const $downLink = $row.find('.move-down'); if (totalRows === 1) { $upLink.hide(); $downLink.hide(); } else if (index === 0) { $upLink.hide(); $downLink.show(); } else if (index === totalRows - 1) { $upLink.show(); $downLink.hide(); } else { $upLink.show(); $downLink.show(); } }); checkAttribute(); } function checkAttribute() { const $currentRows = $('#attribute-set-table').find('tr'); const attribute_set = []; $currentRows.each(function() { const $row = $(this); let alias = ''; const $aliasInput = $row.find('input[type="text"]'); if ($aliasInput.length) { alias = $aliasInput.val().trim(); } // 仅组装数据库需要的3个字段 const rowData = { field: $row.attr('code'), // 字段标识(必填) show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填) alias: alias // 别名(必填,固定列保存"-") }; attribute_set.push(rowData); // 所有行按顺序存入数组,顺序=表格排序 }); // 和 attributeSet对比部分值和顺序,如果不同则返回true,否则返回false const defaultAttr = []; for (const attr of attributeSet) { defaultAttr.push({ field: attr.field, show: attr.show, alias: attr.alias || '' // 确保默认值也有 alias 字段,且空值为 '-' }); } console.log(attribute_set, defaultAttr); if (_.isEqual(attribute_set, defaultAttr)) { $('#show-attribute-btn').hide(); $('#cancel-attribute-btn').hide(); } else { $('#show-attribute-btn').show(); $('#cancel-attribute-btn').show(); } } // 立即初始化链接状态(页面渲染完成后就计算显示) updateMoveButtons(); $tbody.on('change', 'input', function () { checkAttribute(); }); $('#set-attribute-btn').click(function() { const $currentRows = $('#attribute-set-table').find('tr'); const attribute_set = []; $currentRows.each(function() { const $row = $(this); let alias = ''; const $aliasInput = $row.find('input[type="text"]'); if ($aliasInput.length) { alias = $aliasInput.val().trim(); } // 仅组装数据库需要的3个字段 const rowData = { field: $row.attr('code'), // 字段标识(必填) show: Number($row.find('.form-check-input').is(':checked')), // 是否显示(必填) alias: alias // 别名(必填,固定列保存"-") }; attribute_set.push(rowData); // 所有行按顺序存入数组,顺序=表格排序 }); console.log(attribute_set); postData(`/sp/${spid}/contract/setting/update`, { type: 'attribute_set', attribute_set, }, function (result) { toastr.success('设置成功'); attributeSet = result; _initialColSetTbodyHtml = $tbody.html(); // 更新初始 HTML 为当前状态(保存后以当前状态为初始状态) checkAttribute(); }); }); $('#cancel-attribute-btn').click(function() { toastr.warning('已取消改动'); $tbody.html(_initialColSetTbodyHtml); // 恢复初始 HTML updateMoveButtons(); // 恢复后重新计算链接显示状态 }); // 事件委托:从一开始就绑定上/下移事件 $tbody.on('click', '.move-up', function(e) { e.preventDefault(); const $currentRow = $(this).closest('tr'); const $prevRow = $currentRow.prev('tr'); if ($prevRow.length) { $currentRow.insertBefore($prevRow); updateMoveButtons(); } }); $tbody.on('click', '.move-down', function(e) { e.preventDefault(); const $currentRow = $(this).closest('tr'); const $nextRow = $currentRow.next('tr'); if ($nextRow.length) { $currentRow.insertAfter($nextRow); updateMoveButtons(); } }); });