el-table 单元格编辑
2021/7/21 23:12:45
本文主要是介绍el-table 单元格编辑,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<template> <div class="app-container"> <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row > <el-table-column align="center" label="ID" width="95"> <template slot-scope="scope"> <el-input v-model="scope.row.id" v-bind:ref="'r'+ scope.$index + 'c' + 1" @input='input(scope.row, scope.row.id, range.value)' @keyup.native="keyup($event, scope.$index, 1)" @change='save(scope.row)'></el-input> </template> </el-table-column> <el-table-column align="center" label="author" width="95"> <template slot-scope="scope"> <el-input v-model="scope.row.author" v-bind:ref="'r'+ scope.$index + 'c' + 2" @input='input(scope.row, scope.row.author, range.value)' @keyup.native="keyup($event, scope.$index, 2)" @change='save(scope.row)'></el-input> </template> </el-table-column> </el-table> </div> </template> <script> import { getList } from '@/api/table' export default { filters: { statusFilter(status) { const statusMap = { published: 'success', draft: 'gray', deleted: 'danger' } return statusMap[status] } }, data() { return { list: null, listLoading: true } }, created() { this.fetchData() }, methods: { fetchData() { this.listLoading = true getList().then(response => { this.list = response.data.items this.listLoading = false }) }, keyup(ev, row, col) { this.keyupTo(ev, row, col, this.list.length,2) }, keyupTo(ev, row, col, rowCount, colCount) { // 替代 switch 的优雅写法 const actions = { 'ArrowUp': () => { row-- if (row < 0) row = rowCount - 1 }, 'ArrowDown': () => { row++ if (row >= rowCount) row = 0; }, 'ArrowLeft': () => { col-- if (col < 1) col = colCount; }, 'ArrowRight': () => { col++ if (col > colCount) col = 1 }, 'Enter': () => { col++ if (col > colCount) { // 右下角的话保持不动 if (row === (rowCount - 1)) { col = colCount this.list.push({}) } else { col = 1 row++ } } }, } let action = actions[ev.key]; if (action !== undefined) { action.call() // 用 nextTick 避免 input 还没渲染出来 this.$nextTick(() => { this.$refs['r' + row + 'c' + col].focus() }) } }, } } </script>
这篇关于el-table 单元格编辑的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南