DynamicAddTable.vue 4.68 KB
<template>
  <div class="dynamic-table-container">
    <span class="title">{{configObj.title}}调整:</span><el-button type="default" style="padding: 8px 20px;" v-if="isEdit" @click="addRow">
      添加事由
    </el-button>

    <el-form :model="dynamicForm" ref="dynamicForm">
      <el-table :data="tableData" border style="width: 100%; margin-top: 20px !important;">
        <el-table-column prop="index" label="序号" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column prop="adjustAmount" label="调整金额" width="180">
          <template slot-scope="scope">
            <el-form-item :prop="'tableData.' + scope.$index + '.adjustAmount'" :rules="adjustAmountRules">
              <el-input v-model="scope.row.adjustAmount" :disabled="!isEdit" placeholder="请输入金额" type="number" @input="handleInput(scope.$index, 'adjustAmount')" @blur="handleInputBlur"></el-input>
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column prop="adjustReason" label="调整事由">
          <template slot-scope="scope">
            <el-form-item :prop="'tableData.' + scope.$index + '.adjustReason'" :rules="adjustReasonRules">
              <el-input v-model="scope.row.adjustReason" placeholder="请输入调整事由" maxlength="50" :disabled="!isEdit"></el-input>
            </el-form-item>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120" align="center" v-if="isEdit">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="deleteRow(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>

    <!-- <div style="margin-top: 20px">
      <el-button type="info" @click="showTableData">显示表格数据</el-button>
      <el-button type="success" @click="validateForm">提交验证</el-button>
    </div> -->
  </div>
</template>

<script>
import { deepClone, Util } from '@/common/util'
import { limitPrice } from '@/common/format'
export default {
  name: 'DynamicAdjustmentTable',
  props: {
    adjustments: {
      type: Array,
      default: () => []
    },
    isEdit: {
      type: Boolean,
      default: true
    },
    configObj: {
      type: Object,
      default: () => {
        return {
          title: '应付'
        }
      }
    }
  },
  data() {
    return {
      dynamicForm: {
        tableData: [],
        tableOldData: []
      },
      adjustAmountRules: [
        { required: true, message: '请输入调整金额', trigger: 'blur' },
        {
          validator: (rule, value, callback) => {
            const decimalPart = String(value).split('.')[1]
            if (value && isNaN(Number(value))) {
              callback(new Error('请输入有效的数字'));
            } else {
              callback();
            }
          },
          trigger: 'blur'
        }
      ],
      adjustReasonRules: [
        { required: true, message: '请输入调整事由', trigger: 'blur' },
        { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
      ]
    }
  },
  computed: {
    tableData: {
      get() {
        return this.dynamicForm.tableData;
      },
      set(value) {
        this.dynamicForm.tableData = value;
      }
    }
  },
  created() {
    // 初始化一行数据
    this.addRow();
  },
  mounted() {
    this.dynamicForm.tableData = this.adjustments
    this.tableOldData = deepClone(this.adjustments)
  },
  methods: {
    addRow() {
      this.tableData.push({
        adjustAmount: '',
        adjustReason: ''
      });
      this.tableOldData = deepClone(this.tableData)
    },
    deleteRow(index) {
      this.tableData.splice(index, 1);
      this.tableOldData = deepClone(this.tableData)
      this.$emit('handleInputBlur')
    },
    handleInput(rowIdx, attr) {
      const val = this.tableData[rowIdx][attr]
      const oldVal = this.tableOldData[rowIdx][attr]
      this.tableData[rowIdx][attr] = limitPrice(val, oldVal, 7, 2, '', '', 'priceReg_negative')
      this.tableOldData[rowIdx][attr] = this.tableData[rowIdx][attr]
    },
    handleInputBlur() {
      this.$emit('handleInputBlur')
    },
    validateForm() {
      var flag = true
      this.$refs.dynamicForm.validate(valid => {
        if (!valid) {
          flag = false
        }
      });
      return flag
    }
  }
}
</script>

<style lang="scss">
.dynamic-table-container {
  box-sizing: border-box;
  padding: 20px 20px 0 20px;
  .title {
    font-size: 14px;
  }
  .el-table--medium td {
    padding: 4px 16px !important;
  }

  .el-form-item {
    margin-bottom: 0;
  }
}
</style>