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 单元格编辑的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程