this.$set()给对象新增属性this.$delete()删除属性

项目场景:

给对象添加属性


问题描述

测试数据

 test: [
        { name: '张三' }, 
        { name: '李四' }, 
        { name: '王五' }
      ],

打印结果

在这里插入图片描述

方法一:用this.$set()添加age属性

	this.test.forEach((item) => {
	     this.$set(item, 'age', '18')
	   })
	console.log(this.test)

打印结果

在这里插入图片描述

方法二:直接赋值 item.age = “3” 不会及时改变

因为Vue 会在初始化实例时进行双向数据绑定,使用Object.defineProperty()对属性遍历添加 getter/setter 方法,所以属性必须在 data 对象上存在时才能进行上述过程 ,这样才能让它是响应的。
name有get和set方法,但是在新增的age里面并没有这两个方法,因此,设置了age值后vue并不会及时自动更新视图;

删除属性
this.$delete(item,'age')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<template> <div> <a-table :pagination="false" :columns="columns" :dataSource="dataSource"> //循环展示数据或input输入框 <template v-for="col in ['abbreviation', 'fullName', 'nodes']" :slot="col" slot-scope="text, record, index" > <div :key="col"> <a-input v-if="editableData[record.key]" v-model="editableData[record.key][col]" /> <template v-else>{{ text }}</template> </div> </template> //操作 <template slot="operation" slot-scope="text, record, index"> <span v-if="editableData[record.key]"> <a-icon type="check" @click="save(record.key)" /> </span> <span v-else> <a-icon type="delete" @click="deleteItem(record.key)" /> <a-icon type="edit" @click="edit(record.key)" /> <a-icon type="plus" v-if="index==dataSource.length-1" @click="addItem(record.key)" /> </span> </template> </a-table> </div> </template> <script> import { cloneDeep } from "lodash"; export default { data() { return { editableData: [], //正在编辑的数组 columns: [ { title: "简称", dataIndex: "abbreviation", scopedSlots: { customRender: "abbreviation" } }, { title: "全称", dataIndex: "fullName", scopedSlots: { customRender: "fullName" } }, { title: "来源", dataIndex: "nodes", scopedSlots: { customRender: "nodes" } }, { title: "操作", dataIndex: "operation", scopedSlots: { customRender: "operation" } } ], //表格数据 dataSource: [ { key: 0, abbreviation: "简称1", fullName: "全称1", nodes: "来源1" }, { key: 1, abbreviation: "简称2", fullName: "全称2", nodes: "来源2" }, { key: 2, abbreviation: "简称3", fullName: "全称3", nodes: "来源3" }, { key: 3, abbreviation: "简称14", fullName: "全称14", nodes: "来源14" } ] }; }, components: {}, props: ["tableDatas"], watch: {}, updated() {}, created() {}, methods: { addItem(key) { let item = { key: key + 1, abbreviation: "", fullName: "", nodes: "" }; this.dataSource.splice(key + 1, 0, item); this.$set(this.editableData, key + 1, item); }, deleteItem(key) { this.dataSource = this.dataSource.filter(item => item.key !== key); }, edit(key) { let editItem = cloneDeep( this.dataSource.filter(item => key === item.key)[0] ); this.$set(this.editableData, key, editItem); }, save(key) { Object.assign( this.dataSource.filter(item => key === item.key)[0], this.editableData[key] ); this.$set(this.editableData, key, null); } } }; </script>怎么实现编辑全部
06-06
要实现全部编辑,你可以考虑添加一个"编辑全部"按钮,当点击该按钮时,将所有行的数据都设置为可编辑状态。你可以按照以下步骤来实现: 1. 在 data 中新增一个名为`isEditingAll`的属性,用于记录是否处于编辑全部状态,默认值为`false`。 2. 在 template 中添加一个"编辑全部"按钮,并绑定点击事件`editAll`。 3. 在`editAll`方法中,遍历所有的行数据,将它们都设置为可编辑状态,同时将`isEditingAll`设为`true`。 4. 在模板中的每个输入框和操作按钮中都添加一个判断,如果`isEditingAll`为`true`,则显示输入框或操作按钮,否则显示文本。 5. 在保存和取消编辑的方法中,也要做出相应的修改:如果`isEditingAll`为`true`,则遍历所有的行数据进行保存或取消编辑,否则只保存或取消编辑当前行数据。 下面是修改后的代码示例: ``` <template> <div> <a-button type="primary" @click="editAll" v-if="!isEditingAll">编辑全部</a-button> <a-table :pagination="false" :columns="columns" :dataSource="dataSource"> <template v-for="col in ['abbreviation', 'fullName', 'nodes']" :slot="col" slot-scope="text, record, index" > <div :key="col"> <a-input v-if="editableData[record.key][col] || isEditingAll" v-model="editableData[record.key][col]" /> <template v-else>{{ text }}</template> </div> </template> <template slot="operation" slot-scope="text, record, index"> <span v-if="editableData[record.key] || isEditingAll"> <a-icon type="check" @click="save(record.key)" /> <a-icon type="close" @click="cancel(record.key)" /> </span> <span v-else> <a-icon type="delete" @click="deleteItem(record.key)" /> <a-icon type="edit" @click="edit(record.key)" /> <a-icon type="plus" v-if="index==dataSource.length-1" @click="addItem(record.key)" /> </span> </template> </a-table> </div> </template> <script> import { cloneDeep } from "lodash"; export default { data() { return { editableData: {}, isEditingAll: false, // 是否处于编辑全部状态 columns: [ { title: "简称", dataIndex: "abbreviation", scopedSlots: { customRender: "abbreviation" } }, { title: "全称", dataIndex: "fullName", scopedSlots: { customRender: "fullName" } }, { title: "来源", dataIndex: "nodes", scopedSlots: { customRender: "nodes" } }, { title: "操作", dataIndex: "operation", scopedSlots: { customRender: "operation" } } ], dataSource: [ { key: 0, abbreviation: "简称1", fullName: "全称1", nodes: "来源1" }, { key: 1, abbreviation: "简称2", fullName: "全称2", nodes: "来源2" }, { key: 2, abbreviation: "简称3", fullName: "全称3", nodes: "来源3" }, { key: 3, abbreviation: "简称14", fullName: "全称14", nodes: "来源14" } ] }; }, components: {}, props: ["tableDatas"], watch: {}, updated() {}, created() {}, methods: { addItem(key) { let item = { key: key + 1, abbreviation: "", fullName: "", nodes: "" }; this.dataSource.splice(key + 1, 0, item); this.$set(this.editableData, key + 1, item); }, deleteItem(key) { this.dataSource = this.dataSource.filter(item => item.key !== key); }, edit(key) { let editItem = cloneDeep(this.dataSource.filter(item => key === item.key)[0]); this.$set(this.editableData, key, editItem); }, save(key) { if (this.isEditingAll) { // 遍历所有行数据进行保存 this.dataSource.forEach(item => { Object.assign(item, this.editableData[item.key]); }); this.isEditingAll = false; } else { Object.assign(this.dataSource.filter(item => key === item.key)[0], this.editableData[key]); this.$set(this.editableData, key, null); } }, cancel(key) { if (this.isEditingAll) { // 遍历所有行数据进行取消编辑 this.dataSource.forEach(item => { this.$set(this.editableData, item.key, null); }); this.isEditingAll = false; } else { this.$set(this.editableData, key, null); } }, editAll() { this.dataSource.forEach(item => { this.$set(this.editableData, item.key, cloneDeep(item)); }); this.isEditingAll = true; } } }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值