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-12-26JavaScript入门教程:从零开始学习JavaScript编程
- 2024-12-26JavaScript入门教程:从零开始学习JavaScript
- 2024-12-26JS编程入门指南:从零开始学习JavaScript
- 2024-12-25Java编程面试题详解与解答
- 2024-12-25TS基础知识详解:初学者必看教程
- 2024-12-252024面试题解析与攻略:从零开始的面试准备指南
- 2024-12-25数据结构与算法学习:新手入门教程
- 2024-12-25初学者必备:订单系统资料详解与实操教程
- 2024-12-24内网穿透资料入门教程
- 2024-12-24微服务资料入门指南