| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- '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(`<tr>
- <td>合同</td>
- <td></td>
- </tr>`);
- }
- types.forEach(t => {
- const typeRow = `<tr>
- <td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value="${t}"></td>
- <td>
- <a href="javascript:void(0);" class="btn btn-sm text-danger remove-${fun}-btn"><i class="fa fa-remove"></i></a>
- </td>
- </tr>`;
- $('#'+ 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 = `<tr>
- <td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value=""></td>
- <td>
- <a href="javascript:void(0);" class="btn btn-sm text-danger remove-${fun}-btn"><i class="fa fa-remove"></i></a>
- </td>
- </tr>`;
- $('#'+ 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();
- }
- });
- });
|