Ant Design Vue中动态增减表单项

Ant Design Vue中动态增减表单项

去官网

按钮【增加】弹出的界面中动态的添加删除输入框,输入内容后保存为数组值(可以拼接为字符串保存),【编辑】显示时传入数组,若是字符串,可以通过转化为数组
效果图(增加页面、编辑页面)
在这里插入图片描述

代码:

<template>
  <div>

  <div class="student-modal-container" >
    <a-form :form="form" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
      <a-row>
        <a-col :span="12" >
          <a-form-item label="消息1:">
            <a-select :disabled="type === 'view'" v-decorator="['category', {rules: validateRules.category, initialValue: formData.category}]" placeholder="请选择">
              <a-select-option
                v-for="item in options"
                :key="item.value"
                :value="item.value"
              >
                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>

          <a-col :span="12">
          <a-form-item label="消息2:">
            <a-select :disabled="type === 'view'" v-decorator="['messCategory', {rules: validateRules.messCategory, initialValue: formData.messCategory}]" placeholder="请选择">
              <a-select-option
                v-for="item in messOptions"
                :key="item.value"
                :value="item.value"
              >

                {{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row v-for="item in keysList" :key="item">
        <a-col :span="12">
            <a-form-item label="内容" >
              <a-input
                placeholder="请输入内容"
                v-decorator="[
                  `${title}arr[${item}]`,
                  {
                    initialValue: arr[item] ? arr[item] : undefined,
                    rules: [{ required: true, message: '请输入内容' }]
                  }
                ]"
              style="width: 60%; margin-right: 8px"/>
              <template v-if="keysList.length > 1">

                  <a-icon
                          type="minus-circle"
                          class="dynamic-delete-button"
                          @click="removeRow(item)" />
<!--                </a-button>-->
              </template>
            </a-form-item>
          </a-col>
      </a-row>

       <a-row>
          <a-col :span="12">
              <a-form-item v-bind="formItemLayoutWithOutLabel">
                <a-button type="dashed" style="width: 60%" @click="addRow">
                  <a-icon type="plus-circle" /> 
                </a-button>
              </a-form-item>
          </a-col >
      </a-row>

    </a-form>
  </div>
  </div>
</template>

script中的methods

<script>
  let id = 0;
  import { Select, Form, Input, Row, Col ,Button ,Icon } from 'ant-design-vue';
  
  const ASelectOption = Select.Option;
  const AFormItem = Form.Item;
  const PARTONE = 'partOne';

  export default {
    name: "EditForm",
    components: {
      ASelect: Select, ASelectOption, AForm: Form, AFormItem,
      AInput: Input, ARow: Row, ACol: Col,ATextarea: Input.TextArea,
      AButton: Button,
      AIcon: Icon,

    },
    data: function() {
      return {
        id: 0,
        keysList: [],
        validateRules: {
          category: [{required: this.type !== 'view', message: '消息不能为空'}],
          names: [{required: this.type !== 'view', message: '内容不能为空'}],
          messCategory: [{required: this.type !== 'view', message: '消息不能为空'}],

        },
        options: [
          { value: '33', label: '33' }
        ],
        messOptions: [
          { value: '22', label: '22' }
         
        ],

// 数据
        data: Object.assign({}, this.formData),
        formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 4 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
      },
      formItemLayoutWithOutLabel: {
        wrapperCol: {
          xs: { span: 24, offset: 0 },
          sm: { span: 20, offset: 4 },
        },
      },

        form: this.$form.createForm(this),
        arr: [],
      }

    },

    created () {
    // this.form = this.$form.createForm(this)
      this.form = this.$form.createForm(this, { name: 'StudentEditForm' });//StudentEditForm
      this.init();
  },


    props: {
      formData: {
        type: Object,
        default: () => {return {}}
      },
      type: {
        type: String,
        default: 'create'
      },
      title: {
        type: String,
        default: ''
      },
      wrapHeight: { // 表单容器的高度
      type: Number,
      default: 120
    },
    // arr: {
    //   type: Array,
    //   default: function () {
    //     return []
    //   }
    // }
    },

    methods: {
      handleCancel() {
        this.$emit('on-cancel')
      },
      handleConfirm(type) {
        this.$emit('on-confirm', type, this.data);
      },
      init () {
        const arr = []
        if (this.formData.arr !== undefined){
          this.arr = this.formData.arr
        }
        if (this.arr.length !== 0) {
          for (let i = 0; i < (this.formData.arr).length-1; i++) {
            arr.push(i)
            this.id = this.id + 1
          }
        }
        else{
          arr.push(0)
          this.id = this.id + 1
        }
      this.keysList = arr
    },
    // 移除某行
    removeRow (k) {
      if (this.keysList.length === 1) { // 如果存在可以移除所有行的情况,把条件改为this.keysList.length === 0即可
        return
      }
      this.keysList = this.keysList.filter(item => item !== k)
    },
    // 新增一行
    addRow () {
      this.id = this.id + 1
      this.keysList = this.keysList.concat(this.id)
    },

    }
  }
</script>

样式


```html
<style lang="less" scoped>
  .student-modal-container{

    .ant-form{
      .ant-input-disabled, .ant-select-disabled {
        background: none;
        border: none;
        color: rgba(0,0,0,.65);
        font-weight: 700;
        cursor: auto;
        /deep/ .ant-select-selection{
          background: none;
          border: none;
          cursor: auto;
          .ant-select-arrow{
            display: none;
          }
        }
      }
    }

  .dynamic-delete-button {
  cursor: pointer;
  position: relative;
  top: 4px;
  font-size: 24px;
  color: #999;
  transition: all 0.3s;
}
.dynamic-delete-button:hover {
  color: #777;
}
.dynamic-delete-button[disabled] {
  cursor: not-allowed;
  opacity: 0.5;
}

  }
  .dynamic-wrap {
    padding-top: 10px;
    background-color: white;
    overflow-y: scroll;
    overflow-x: hidden;
    &::-webkit-scrollbar {
      width: 7px;
    }
    &::-webkit-scrollbar-thumb {
      background: #d8d8d8;
      border-radius: 10px;
    }
    &::-webkit-scrollbar-track-piece {
      background: transparent;
    }
  }
  .minusRowBtn {
    color: #f5222d;
    background: #fff1f0;
    border-color: #ffa39e;
    padding-right: 7px;
    padding-left: 7px;
    height: 29px;
    margin-left: 10px;
  }
  .addRowBtn {
    width: 70%;
    color: #1890ff;
    border-color: #91d5ff;
    margin: 0px 0px 20px 70px;
  }
</style>

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Ant Design Vue 表格的动态表头可以通过以下步骤实现: 1. 定义表格列的数组,其每个列对象包含 `title` 属性和 `key` 属性,如下所示: ``` columns: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, // 其他列 ] ``` 2. 定义动态表头的数组,其每个表头对象包含 `label` 属性和 `children` 属性,如下所示: ``` headerList: [ { label: '基本信息', children: [ { label: '姓名', key: 'name' }, { label: '年龄', key: 'age' }, // 其他列 ] }, { label: '扩展信息', children: [ { label: '职业', key: 'job' }, { label: '工作经验', key: 'experience' }, // 其他列 ] }, // 其他表头 ] ``` 3. 在表格组件使用 `v-for` 循环渲染表头,如下所示: ``` <template> <a-table :columns="tableColumns" :data-source="tableData"> <template #title> <span>{{ tableTitle }}</span> <a-divider /> <a-table-header :columns="headerColumns" /> </template> <!-- 其他表格内容 --> </a-table> </template> <script> export default { data() { return { columns: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, // 其他列 ], headerList: [ { label: '基本信息', children: [ { label: '姓名', key: 'name' }, { label: '年龄', key: 'age' }, // 其他列 ] }, { label: '扩展信息', children: [ { label: '职业', key: 'job' }, { label: '工作经验', key: 'experience' }, // 其他列 ] }, // 其他表头 ] } }, computed: { tableColumns() { return this.columns.filter(item => { return this.headerList.some(header => { return header.children.some(child => { return child.key === item.key }) }) }) }, headerColumns() { return this.headerList.map(header => { return { title: header.label, children: this.columns.filter(item => { return header.children.some(child => { return child.key === item.key }) }) } }) } }, // 其他代码 } </script> ``` 在上面的代码,我们使用 `computed` 属性的 `tableColumns` 计算属性过滤出需要显示的表格列,使用 `headerColumns` 计算属性生成动态表头。最后在表格组件使用 `a-table-header` 标签渲染动态表头。 注意,上面的代码只是一个示例,实际使用时需要根据具体需求进行修改。 ### 回答2: 实现 ant design vue 表格的动态表头,需要进行以下步骤: 1. 先定义一个表头数组,其每个表头对象包括标题(title)和键(key)两个属性。例如: ``` tableHeaders: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, { title: '性别', key: 'gender' } ] ``` 2. 在 ant design vue 的表格组件,使用 v-for 指令遍历表头数组,动态生成表头: ``` <template> <a-table :columns="tableHeaders" :data-source="tableData"></a-table> </template> ``` 3. 然后定义一个表格数据数组,其每个对象包括与表头键对应的值。例如: ``` tableData: [ { name: '张三', age: 20, gender: '男' }, { name: '李四', age: 25, gender: '女' }, { name: '王五', age: 30, gender: '男' } ] ``` 4. 确保表头对象的键与表格数据对象的属性相匹配,以保证表格能正确渲染数据。 5. 如需动态修改表头,可以通过改变表头数组的内容来实现,然后更新视图即可。 通过上述步骤,就可以实现 ant design vue 表格的动态表头功能。可以根据实际需求,灵活定义表头数组和表格数据数组,从而满足不同的动态表头需求。 ### 回答3: 在 ant design vue ,可以通过使用表格的 header 属性实现动态表头。header 属性接受一个函数,该函数返回一个数组,数组的每个元素对应表格的表头列。在这个函数,我们可以根据需要动态生成表头列。 例如,我们可以在 data 定义一个变量 columns,用于存储表格的表头列。然后,在 header 函数使用该变量返回表头列数组。我们可以根据需求修改 columns 变量,从而实现动态表头。 具体步骤如下: 1. 在 data 定义 columns 变量,并初始化为一个空数组。 2. 在 template 使用 Table 组件,并通过 :columns="columns" 将 columns 变量绑定到表格的表头列属性。 3. 在 header 函数根据需要动态生成表头列,并将其添加到 columns 数组。 4. 在触发动态修改表头的操作,修改 columns 变量以更新表格的表头。 例如,我们可以在一个按钮的点击事件动态添加一个新的表头列: ``` <template> <div> <Button @click="addNewColumn">添加新的表头列</Button> <Table :columns="columns" :dataSource="dataSource" /> </div> </template> <script> export default { data() { return { columns: [], dataSource: [...], }; }, methods: { addNewColumn() { const newColumn = { title: '新的表头列', key: 'columnKey' }; this.columns.push(newColumn); }, }, computed: { header() { // TODO: 根据需要动态生成表头列 return this.columns; }, }, }; </script> ``` 在这个例子,点击按钮后会在表格上方添加一个新的表头列。你可以根据实际需求自定义表头列的属性和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值