【vue】编辑行

<template>
  <el-form
    ref="lineEditForm"
    :rules="tableFormRules"
    :model="tableFormData"
    v-bind="{
      'inline-message': true,
      'validate-on-rule-change': false,
      'label-width': '0px',
      ...(formOptions || {}),
    }"
  >
    <div class="lineEditForm">
      <el-table :data="tableData || []" v-bind="{ border: false, ...(tableOptions || {}) }">
        <el-table-column
          v-for="e in tableColumns || []"
          :key="e.props"
          :prop="e.props"
          v-bind="{ ...(e.tableColumnOptions || {}) }"
        >
          <template slot="header">
            <slot :name="`${e.prop}_header`" />
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="`${e.prop}-${scope.$index}`" v-bind="{ ...(e.formItemOptions || {}) }">
              <slot :name="e.prop" :rowData="scope.row" :rowIndex="scope.$index"/>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column
          v-if="!operationOptions.hiddenOptionColumn"
          v-bind="{
            prop: 'operation',
            align: 'center',
            width: '150px',
            label: $t('common_components_ui.components.lineEditTable.operation'),
            ...(operationOptions.operationColumnOptions || {}),
          }"
        >
          <template slot-scope="scope">
            <!--自定义分摊:操作(删除)-->
            <el-popover
              v-show="!(operationOptions.hiddenDelete || []).includes(scope.$index)"
              :ref="`lineEditFormPopover-${scope.$index}`"
              placement="top"
            >
              <p>{{ $t('common_components_ui.components.lineEditTable.confirmTips') }}</p>
              <div class="popover-btn">
                <el-button @click="$refs[`lineEditFormPopover-${scope.$index}`].doClose()">
                  {{ $t('common_components_ui.components.lineEditTable.cancel') }}
                </el-button>
                <el-button type="primary" @click="deleteLineEdit(scope.$index)">
                  {{ $t('common_components_ui.components.lineEditTable.confirm') }}
                </el-button>
              </div>
              <el-button slot="reference" type="text">
                {{ $t('common_components_ui.components.lineEditTable.delete') }}
              </el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-row v-show="!operationOptions.hiddenAdd" type="flex" justify="center" style="margin-top: 16px">
      <el-button type="text" class="ad-btn" icon="el-icon-plus" @click="addLineEdit">
        {{ $t('common_components_ui.components.lineEditTable.add') }}
      </el-button>
    </el-row>
  </el-form>
</template>

<script>
export default {
  name: 'MkLineEditTable',
  props: {
    // 行编辑列表数据 (必填)
    tableData: {
      type: Array,
      default() {
        return []
      },
    },
    /**
     * 行编辑列配置想,按从左到右顺序来 (必填)
     * [{
     *  prop: '', // 该列关键字和tableData中数据一一对应,必填
     *  initValue: '', // 该列初始值 新增一行时会用到初始值,必填
     *  rules: {
     *    trigger: '', // 校验触发类型 可选值 'change' || 'blur'
     *    validator: (rule, value, callback, rowData, rowIndex) => {
     *      // 校验逻辑 前三个入参值和el-form的validator函数一致,后两个参数为当前行数据 和 当前行index
     *    }
     *  },
     *  tableColumnOptions: {}, // 表格该列自定义属性,支持el-table-column的所有入参都支持
     *  formItemOptions: {} // 该列form项的自定义属性,支持el-form-item的所有入参都支持
     * }]
     */
    tableColumns: {
      type: Array,
      default() {
        return []
      },
    },
    // form表单自定义参数,支持el-form所有参数
    formOptions: {
      type: Object,
      default() {
        return {}
      },
    },
    // table表格自定义参数,支持el-table所有参数
    tableOptions: {
      type: Object,
      default() {
        return {}
      },
    },
    /**
     * 操作按钮属性
     * hiddenOptionColumn 是否隐藏操作列,默认false
     * hiddenDelete 数组 存 不展示删除按钮的行数下标
     * hiddenAdd 是否隐藏添加按钮,默认false
     * operationColumnOptions 操作列自定义属性,支持el-table-column的所有入参都支持
     */
    operationOptions: {
      type: Object,
      default() {
        return {
          hiddenOptionColumn: false,
          hiddenDelete: [],
          hiddenAdd: false,
          operationColumnOptions: {},
        }
      },
    },
  },
  data() {
    return {}
  },
  computed: {
    /**
     * 通过table数组数据形成Form表单所需对象
     */
    tableFormData() {
      const tableFormData = {}
      this.tableData.forEach((data, index) => {
        this.tableColumns.forEach((options) => {
          tableFormData[`${options.prop}-${index}`] = data[options.prop]
        })
      })
      return tableFormData
    },
    /**
     * 通过table数组数据形成Form表单所需校验规则
     */
    tableFormRules() {
      const tableFormRules = {}
      this.tableData.forEach((data, index) => {
        this.tableColumns.forEach((options) => {
          if (options.rules) {
            tableFormRules[`${options.prop}-${index}`] = {
              trigger: options.rules.trigger,
              validator: (rule, value, callback) => {
                options.rules.validator(rule, value, callback, this.tableData[index], index)
              },
            }
          }
        })
      })
      return tableFormRules
    },
  },
  methods: {
    /**
     * @method deleteLineEdit 删除一行
     */
    deleteLineEdit(index) {
      this.$refs[`lineEditFormPopover-${index}`].doClose()
      this.tableData.splice(index, 1)
      this.$emit('update:tableData', this.tableData)
    },
    /**
     * @method addLineEdit 添加一行
     */
    addLineEdit() {
      const tableDataItem = {}
      this.tableColumns.forEach((options) => {
        tableDataItem[options.prop] = options.initValue
      })
      this.tableData.push(tableDataItem)
      this.$emit('update:tableData', this.tableData)
    },
    /**
     * @method formValidate form表单校验
     * @return Promise
     */
    formValidate() {
      return new Promise((resolve, reject) => {
        this.$refs.lineEditForm.validate((valid) => {
          resolve(valid)
        })
      })
    },
    /**
     * @method formResetFields 对整个表单进行重置,将所有字段值重置为初始值并移除校验结果
     */
    formResetFields() {
      this.$refs.lineEditForm.validateField()
    },
    /**
     * @method formValidateField 对部分表单字段进行校验的方法
     * @param val 表单项的 prop 属性或者 prop 组成的数组
     * @param callback 校验完后的回调函数  Function(errorMessage: string)
     */
    formValidateField(val, callback) {
      this.$refs.lineEditForm.validateField(val, callback)
    },
    /**
     * @method formClearValidate 移除表单项的校验结果。传入待移除的表单项的 prop 属性或者 prop 组成的数组,如不传则移除整个表单的校验结果
     * @param val 表单项的 prop 属性或者 prop 组成的数组
     */
    formClearValidate(val) {
      this.$refs.lineEditForm.clearValidate(val)
    },
  },
}
</script>

<style lang="scss" scoped>
.popover-btn {
  text-align: right;
  margin-top: 16px;
}

.header-label:before {
  content: '*';
  color: #ea3447;
  margin-right: 4px;
}

.ad-btn {
  width: 100%;
  border: 1px dashed rgba(0, 0, 0, 0.1) !important;
  color: rgba(0, 0, 0, 0.45) !important;
  font-size: 15px;
}

.lineEditForm ::v-deep .el-table__empty-block {
  height: 80px !important;
}
</style>

使用场景:动态编辑行

对于Ant Design Vue中的表格编辑,你可以使用Table组件的editable属性来实现。首先,设置editable为true,然后在columns中定义需要编辑的列,并指定相应的编辑器组件。 以下是一个示例代码,展示如何在Ant Design Vue中实现表格编辑: ```vue <template> <a-table :columns="columns" :data-source="data" :row-key="record => record.id"> <template slot="name" slot-scope="{ record, index }"> <a-input v-model="record.name" :disabled="!record.editable" /> </template> <template slot="email" slot-scope="{ record, index }"> <a-input v-model="record.email" :disabled="!record.editable" /> </template> <template slot="operation" slot-scope="{ record, index }"> <a-button @click="handleEdit(record)">编辑</a-button> <a-button @click="handleSave(record)">保存</a-button> <a-button @click="handleCancel(record)">取消</a-button> </template> </a-table> </template> <script> export default { data() { return { data: [ { id: 1, name: 'John', email: 'john@example.com', editable: false }, { id: 2, name: 'Jane', email: 'jane@example.com', editable: false }, ], columns: [ { title: '姓名', dataIndex: 'name' }, { title: '邮箱', dataIndex: 'email' }, { title: '操作', dataIndex: 'operation' }, ], }; }, methods: { handleEdit(record) { record.editable = true; }, handleSave(record) { record.editable = false; // 在这里可以进保存操作,例如向后端提交数据 }, handleCancel(record) { record.editable = false; // 在这里可以进取消操作,例如恢复原始数据 }, }, }; </script> ``` 上述代码中,我们使用了Ant Design Vue的Table组件,通过设置editable属性为true来启用编辑功能。每个需要编辑的列都使用了具名插槽,并使用a-input组件作为编辑器。 在表格中,我们还添加了三个操作按钮:编辑、保存和取消。点击编辑按钮会将对应的editable属性设置为true,使该变为可编辑状态。点击保存按钮会将对应的editable属性设置为false,并可以在handleSave方法中进保存数据的操作。点击取消按钮会将对应的editable属性设置为false,并可以在handleCancel方法中进取消编辑的操作。 希望以上示例能帮助到你实现Ant Design Vue表格编辑功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值