ant-design可编辑单元格加入select选择器
1. 需求:
2. 分析:
- 使用ant-design里面Table表格的可编辑单元格模板以及select选择器进行改造。
https://www.antdv.com/components/table-cn/#components-table-demo-editable-cells
- 将Table表格可编辑单元格中的input框替换成select选择器,并且加上鼠标悬浮气泡提示
- 再进行数据传输
3. 代码
1 <template> 2 <div class="editable-cell"> 3 <div v-if="editable" class="editable-cell-input-wrapper"> 4 <a-select 5 mode="multiple" 6 style="width: 100%" 7 placeholder="Please select" 8 @change="handleChange" 9 > 10 <a-select-option v-for="option in options" :key="option"> 11 {{ option }} 12 </a-select-option> 13 </a-select> 14 <a-icon 15 type="save" 16 theme="filled" 17 class="editable-cell-icon-check" 18 @click="check" 19 /> 20 </div> 21 <div v-else class="editable-cell-text-wrapper"> 22 <a-tooltip :title="value"> 23 <span class="text">{{ value }}</span> 24 </a-tooltip> 25 <a-icon type="edit" class="editable-cell-icon" @click="edit" /> 26 </div> 27 </div> 28 </template> 29 30 <script> 31 export default { 32 props: { 33 options: { 34 type: Array, 35 default: [], 36 }, 37 }, 38 data() { 39 return { 40 value: "", 41 editable: false, 42 }; 43 }, 44 methods: { 45 handleChange(value) { 46 this.value = value.join(","); 47 }, 48 edit() { 49 this.editable = true; 50 }, 51 check() { 52 this.editable = false; 53 }, 54 }, 55 }; 56 </script> 57 58 <style> 59 .editable-cell { 60 position: relative; 61 } 62 63 .editable-cell-input-wrapper, 64 .editable-cell-text-wrapper { 65 padding-right: 24px; 66 display: flex; 67 align-items: center; 68 } 69 70 .editable-cell-text-wrapper { 71 padding: 5px 24px 5px 5px; 72 } 73 .editable-cell-text-wrapper .text { 74 max-width: 200px; 75 overflow: hidden; 76 text-overflow: ellipsis; 77 white-space: nowrap; 78 } 79 80 .editable-cell-icon, 81 .editable-cell-icon-check { 82 position: absolute; 83 right: 0; 84 width: 20px; 85 cursor: pointer; 86 } 87 88 .editable-cell-icon, 89 .editable-cell-icon-check { 90 line-height: 18px; 91 display: inline-block; 92 } 93 94 .editable-cell-icon:hover, 95 .editable-cell-icon-check:hover { 96 color: #108ee9; 97 } 98 </style>
3.1 代码分析:
- options:父组件传过来的值,为select选择器的选项
- 定义两个变量:
- value:显示在span标签内以及提示框内的值
- editable:是否可编辑(true为可编辑,即显示select框)
- 定义的方法:
- handleChange:拿到当前选择的选项(多选情况下为数组),并且将其拼接成字符串
- edit和check:分别对应编辑按钮和保存按钮的点击事件
标签:
ant-design
, 前端
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话