vue中实现可编辑table及其中加入下拉选项
作者:等樱花的龙猫 时间:2024-04-10 10:24:51
可编辑table及其中加入下拉选项
<template>
<div>
<el-table :data="tabledatas" border>
<el-table-column label="姓名">
<template slot-scope="scope">
<el-input placeholder="请输入内容" 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="年龄">
<template slot-scope="scope">
<el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.age"></el-input>
<span v-show="!scope.row.show">{{scope.row.age}}</span>
</template>
</el-table-column>
<el-table-column label="地址">
<template slot-scope="scope">
<el-input placeholder="请输入内容" v-show="scope.row.show" v-model="scope.row.address"></el-input>
<span v-show="!scope.row.show">{{scope.row.address}}</span>
</template>
</el-table-column>
<el-table-column label="学籍">
<template slot-scope="scope">
<span v-show="!scope.row.show">{{scope.row.stu}}</span>
<el-select v-model="scope.row.stu" placeholder="请选择" v-show="scope.row.show" >
<el-option
v-for="item in options"
:key="item.stu"
:label="item.stu"
:value="item.stu">
</el-option>
</el-select>
</template>
</el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button @click="scope.row.show =true" >编辑</el-button>
<el-button @click="scope.row.show =false">保存</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data(){
return {
options: [{
value: '选项1',
stu: '初中'
}, {
value: '选项2',
stu: '高中'
}, {
value: '选项3',
stu: '大专'
}, {
value: '选项4',
stu: '本科'
}, {
value: '选项5',
stu: '博士'
}],
value: '',
tabledatas: [
{ name: '李一', age: '19',address:"宁波",stu:"本科",show:false},
{ name: '郭明', age: '23',address:"四川",stu:"本科",show:false},
{ name: '天天', age: '12',address:"海南",stu:"初中",show:false},
{ name: '隆', age: '40',address:"上海",stu:"博士",show:false},
],
}
}
</script>
可以通过设置js里的show:true让该行处于默认编辑状态
出来效果图
vue表头下拉选择框使用总结
1.在el-table-culumn中,加入template标签
使用:
<template slot="header" slot-scope="scope">
<el-dropdown trigger="click" @command = "handleCommand">
<span>类型</span>
<el-dropdown-menu slot="dropdown">
<el-radio-group v-model="sx">//这里,会出现一个bug,下文有解决办法
<el-dropdown-item command="属性0"><el-radio label="0">属性0</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性1"><el-radio label="1">属性1</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性2"><el-radio label="2">属性2</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性3"><el-radio label="3">属性3</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性4"><el-radio label="4">属性4</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性5"><el-radio label="5">属性5</el-radio> </el-dropdown-item>
<el-dropdown-item command="属性6"><el-radio label="6">属性6</el-radio> </el-dropdown-item>
</el-radio-group>
</el-dropdown-menu>
</el-dropdown>
</template>
<template slot-scope="scope">(表中元素)</template>
2.设置handleCommand方法
(当时没使用handleCommand方法做缓冲,在刷新时,第一次刷新不会赋值,第二次刷新会得到上次刷新的值。)
handleCommand(command) {
if(command == '属性0' ){
this.sx= '0'
} else if (command === '属性1') {
this.sx = '1'
} else if( command === '属性2') {
this.sx = '2'
} else if (command === '属性3') {
this.sx = '3'
} else if (command === '属性4') {
this.sx = '4'
} else if( command === '属性5') {
this.sx = '5'
} else if (command === '属性6') {
this.sx = '6'
}
this.刷新方法;
},
但是在使用过程中,点击下拉框中数据时,会出现执行两次handleCommand()方法的情况。通过一天的询问与查找,得到解决办法。
问题出现在<el-radio>标签上,当点击框中数据时,数据响应一次handleCommand()方法,<el-radio>也会响应一次handleCommand()方法。
所以,应该去掉<el-radio>标签与<el-radio-group>标签。
<template slot="header" slot-scope="scope">
<el-dropdown trigger="click" @command = "handleCommand">
<span>类型</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item command="属性0">属性0</el-dropdown-item>
<el-dropdown-item command="属性1">属性1</el-dropdown-item>
<el-dropdown-item command="属性2">属性2</el-dropdown-item>
<el-dropdown-item command="属性3">属性3</el-dropdown-item>
<el-dropdown-item command="属性4">属性4</el-dropdown-item>
<el-dropdown-item command="属性5">属性5</el-dropdown-item>
<el-dropdown-item command="属性6">属性6</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template>
<template slot-scope="scope">(表中元素)</template>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/weixin_48508049/article/details/109360685
标签:vue,可编辑,table,下拉选项


猜你喜欢
前端优化,让你的网页显示的更快更流畅
2009-06-08 13:09:00
几款Python编译器比较与推荐(小结)
2022-07-21 01:31:56

python读取并定位excel数据坐标系详解
2022-02-25 19:01:00

苹果Macbook Pro13 M1芯片安装Pillow的方法步骤
2021-01-11 18:23:46
Python编写简单的HTML页面合并脚本
2022-08-13 09:13:07
ORACLE常见错误代码的分析与解决(二)
2024-01-21 16:37:29
Python转json时出现中文乱码的问题及解决
2021-11-29 17:02:16
当设计师遇上前端开发
2009-05-04 14:05:00
django rest framework使用django-filter用法
2023-03-06 01:56:06

coreseek 搜索英文的问题详解
2023-11-20 14:12:10
对PyTorch中inplace字段的全面理解
2022-11-15 13:52:24
Python爬虫requests库多种用法实例
2021-01-10 13:31:56
MySQL Select语句是如何执行的
2024-01-22 20:05:44

一文带你了解Go语言中的单元测试
2024-04-28 09:11:28
处理HTML代码的若干函数
2009-08-21 13:21:00
python3 反射的四种基本方法解析
2023-10-25 16:04:39
Python将list元素转存为CSV文件的实现
2022-03-14 20:15:00

PyQt5每天必学之组合框
2023-08-30 07:55:06

Python利用requests模块下载图片实例代码
2023-11-18 16:10:13
Python os模块常用方法和属性总结
2021-05-06 13:46:12