vue基于element-ui table做行内编辑

在做行内编辑的时候,直接对对象数组的属性赋值发现没有做响应式的
现象: 点击编辑的时候,没有切换输入框。f12后,输入框才切换出来

vue文档说明: https://cn.vuejs.org/v2/guide/reactivity.html#%E5%A6%82%E4%BD%95%E8%BF%BD%E8%B8%AA%E5%8F%98%E5%8C%96

var vm = new Vue({
  data: {
    items: ['a', 'b', 'c']
  }
})
vm.items[1] = 'x' // 不是响应性的
vm.items.length = 2 // 不是响应性的

代码如下

 <template>
    <el-table :data="labelList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" align="center">
            <template slot-scope="scope">
                <el-input placeholder="名称" size="small" v-show="scope.row.show" v-model="scope.row.name"></el-input>
                <span v-show="!scope.row.show">{{scope.row.name}}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
            <template slot-scope="scope">
                <el-button v-if="!scope.row.show" size="mini" @click="handleEdit(scope.$index)">编辑</el-button>
                <el-button v-if="scope.row.show" size="mini" @click="handleEdit(scope.$index,'cancel')">取消</el-button>
                <el-button v-if="scope.row.show" size="mini" @click="saveLabel(scope.row)">保存</el-button>
                <el-button v-if="!scope.row.show" type="danger" size="mini" @click="deleteLabel(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

注意一下,需要给数组里的对象增加一个显示隐藏属性

    // 给每个对象加个属性
    list.forEach(item => {
        item["show"] = false
    });
     // 深拷贝 这样labelList和tempLabelList不会指向同一list,修改其中之一不会导致另一个也改变
    this.tempLabelList =  JSON.parse(JSON.stringify(list));
    this.labelList = list;

对数组的的对象属性做响应式赋值

 handleEdit(index,cancel) {
                // 对数组元素直接辅助是没有做响应式的
                // https://cn.vuejs.org/v2/guide/reactivity.html#%E5%A6%82%E4%BD%95%E8%BF%BD%E8%B8%AA%E5%8F%98%E5%8C%96
                // row.show = true; 这样是不行的
                let data = this.labelList[index];
                if(cancel && cancel === 'cancel') {
                    data.name = this.tempLabelList[index].name;
                }
                data = {
                    id: data.id,
                    name: data.name,
                    show: !data.show
                }
                this.$set(this.labelList, index, data)
            },

新增,直接往list加入一个对象,并且编辑状态

addLabel() {
    let label = {
        show: true
    };
    this.labelList.unshift(label); 
},
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue基于Element UI Table的二次封装可以通过创建一个自定义的组件来实现。以下是一个简单的示例,演示了如何封装一个基于Element UI Table的组件: ```vue <template> <el-table :data="tableData" :row-key="rowKey" :height="height"> <!-- 渲染表头 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"> <!-- 自定义插槽 --> <template slot-scope="scope"> <slot :column="column" :scope="scope"></slot> </template> </el-table-column> </el-table> </template> <script> export default { name: 'CustomTable', props: { tableData: { type: Array, required: true }, columns: { type: Array, required: true }, rowKey: { type: String, required: true }, height: { type: String, default: 'auto' } } } </script> ``` 在这个示例中,我们创建了一个名为`CustomTable`的组件。它接受`tableData`、`columns`、`rowKey`和`height`作为props,分别表示表格数据、表格列配置、行数据的唯一标识以及表格的高度。 在模板中,我们使用`el-table`和`el-table-column`来渲染Element UI的表格。我们使用了`v-for`指令来循环渲染表格列,并通过`slot-scope`来传递数据给插槽。插槽可以在父组件中定义,并在插槽中使用自定义的组件来渲染表格单元格内容。 通过这种方式,我们可以在父组件中使用这个封装的自定义表格组件,并通过插槽来定制表格的内容和样式。 希望这个简单的示例能帮助到你进行Vue基于Element UI Table的二次封装。如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值