需求:要求表头最后一列为新增,点击新增按钮,可以插入数据,并可以对当前插入的数据进行删除操作。
当table表格中的数据全部删除完时,删除按钮消失,只能点击新增按钮来新增数据。
页面展示:
点击新增按钮:出来一条数据并填写
点击删除按钮:
代码实现:
table的表头:主要是{customRender: ‘operation’, title: ‘Title’}
{
//title: <a-button onClick={() => this.addForm()}>添加</a-button>,
dataIndex: 'operation',
scopedSlots: {customRender: 'operation', title: 'Title'},
width: 20,
}
template 中:slot中的值为scopedSlots传递过来的
<template slot="Title" slot-scope="text, record, index">
<a-button @click="addForm(index1)">新增</a-button>
</template>
<template slot="operation" slot-scope="text, record, index">
<a-button v-if="index === model.highRuleInfo[index1].highList.length - 1"
@click="delForm(text, record, index)">删除
</a-button>
</template>
然后就是addForm,delForm方法。点击新增时就是对数组进行push操作,点击删除时就是对数组进行splice操作。
当table中的数据全部删除时,只能点击新增操作的实现是通过 v-if 去判断 数组的长度,也就是当数组的长度为0时 隐藏。 v-if=“index === model.highRuleInfo[index1].highList.length - 1”
描述有误的地方欢迎大家指正,大家有问题可加qq 876942434。一起进步~