antdv表格的点击按钮添加一行输入框

antdv表格的点击按钮添加一行输入框

需求
在这里插入图片描述

 <a-table :dataSource="dataSource" :columns="columns" :pagination="false">
                  <template #bodyCell="{ column, record, index }">
                    <template v-if="column.key === 'label'">
                    <-- -->
                      <a-input
                        v-if="isEditing[index][0]"
                        v-model:value="record.label"
                        @blur="handleBlur(index, 0)"
                      />
                      <span v-else>{{ record.label }}</span>
                    </template>
                    <template v-if="column.key === 'desc'">
                      <a-input
                        v-if="isEditing[index][1]"
                        v-model:value="record.desc"
                        @blur="handleBlur(index, 1)"
                      />
                      <span v-else>{{ record.desc }}</span>
                    </template>
                    <template v-if="column.key === 'action'">
                      <a-button>
                        <template #icon><delete-outlined @click="deleteCircle(index)" /></template>
                      </a-button>
                    </template>
                  </template>
                  <template #summary>
                    <a-table-summary fixed="bottom">
                      <a-table-summary-row>
                        <a-table-summary-cell :index="0" :col-span="2">
                          <a-button @click="addCircle" :disabled="kbFormData.domain_name_id == ''"
                            ><template #icon><plus-outlined /></template>添加</a-button
                          >
                        </a-table-summary-cell>
                      </a-table-summary-row>
                    </a-table-summary>
                  </template>
                </a-table>

isEditing 用来储存table中key是否是编辑状态
需求中数据只有类型和描述 所以需要生成一个二维数组来存储编辑状态
同时表格的数据是根据左边的单元框来生成默认值的

//点击单元获取表格默认值
const changeModdel = () => {
    dataSource.value = []
    let metaConfig = modalList.value.filter((item) => {
      return item.id == kbFormData.value.domain_name_id
    })
    console.log(metaConfig, 'xxx')
    dataSource.value = JSON.parse(metaConfig[0].schemaJson)
    //生成储存的二维数组
    //ataSource.value.length 对已有的table数据设置编辑状态都为false
    //生成的数组应该是[[false,false],[false,false]]格式
    isEditing.value = Array.from({ length: dataSource.value.length }, () => [false, false])
  }
//添加
const addCircle = () => {
	
    const newRow = { label: '', desc: '' }
    //对表格绑定数据 dataSource添加空数据
    dataSource.value.push(newRow)
    //对isEditing数组添加表格新增数据的编辑状态
    //这个时候表格中会出现对应的input输入框
    isEditing.value.push([true, true])
  }
//输入框值变化
function handleBlur(index, num) {
    //失去焦点修改对应isEditing对应的编辑状态
    isEditing.value[index][num] = false
  }

核心是isEditing这个变量 记录表格中每个数据的编辑状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值