element-ui直接在表格中点击单元格编辑

 更新时间:2021年12月07日 09:59:19   作者:阿雷前进中...  
最近通过在网上查找资料,自己整合研究了一个可以直接在表格中操作数据的基于element-ui的前端模板。可以让增改数据的操作显得方便一点,感兴趣的可以了解一下

最近由于公司开始使用elementUI,开发的过程中需要用到对表格的单元格进行编辑,下面是我自己实现表格可编辑的方式,感兴趣的可以了解一下

实现效果

在这里插入图片描述

编辑之后对应表格数据该字段值也就发生了变化,控制台输出所有数据即可查看变化

实现代码

1、自定义编辑组件

<template>
  <div class="editCell">
    <div class="canEdit" v-if="CanEdit" @click="beginEdit">
      <label v-show="!editStatus">
        <span v-if="this.value!== null && this.value !== undefined && this.value !== ''">{{ value }}{{this.suffix}}</span>
        <span v-else style="padding-left: 100%;padding-top: 100%;"/>
      </label>
      <label v-show="editStatus">
        <input
          type="text"
          class="inputClass"
          ref="input"
          v-on:keyup.13="loseFocus"
          :value="value"
          @blur="loseFocus"
        />
      </label>
    </div>

    <label class="cannotEdit" v-else>
      <span>{{ value }}{{ suffix }}</span>
    </label>
  </div>
</template>

<script>
export default {
  name: "EditCell",
  props: {
    /**
     * 绑定值
     */
    value: {
      required: true
    },
    /**
     * 是否可编辑
     */
    CanEdit: {
      type: Boolean,
      default: true
    },
    /**
     * 格式化函数
     */
    formatData: {
      type: Function,
      default: value => {
        return value;
      }
    },
    /**
     * 编辑后事件
     */
    afterEdit: {
      type: Function,
      default: () => {}
    },
    /**
     * 是否初始格式化
     */
    initFormat: {
      type: Boolean,
      default: false
    },
    suffix: {
      default: ""
    }
  },
  data() {
    return {
      editStatus: false,
      showData: "",
      defaultData: "",
      timeout: null
    };
  },
  methods: {
    /**
     * 单击开始编辑
     */
    beginEdit() {
      this.editStatus = true;
      setTimeout(() => {
        this.$refs.input.focus();
      }, 1);
    },

    /**
     * @param {event} event
     * 丢失焦点关闭编辑状态,并保存数据
     */
    loseFocus(event) {
      let value = this.formatData(event.target.value);
      this.editData(value);
      this.closeEditStatus(value);
      this.afterEdit(value);
    },

    /**
     * 发布input事件修改数据
     * @param value
     */
    editData(value) {
      this.$emit("input", value);
    },

    /**
     * 关闭编辑状态
     * @param value
     */
    closeEditStatus(value) {
      this.editStatus = false;
    },
    /**
     * 初始格式化数据
     */
    initData() {
      let newValue = this.formatData(this.value);
      this.$emit("input", newValue);
    }
  },
  mounted() {
    if (this.initFormat) {
      this.initData();
    }
  },
  watch: {
    'value': function(newVal){
      this.$emit("input", this.formatData(newVal));
    }
  }
};
</script>

<style scoped>
.editCell {
  height: 100%;
  width: 100%;
}
.inputClass {
  height: 30px;
  width: 100%;
  background-color: #fff;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  line-height: 30px;
  outline: 0;
  padding: 0 15px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  overflow: visible;
  touch-action: manipulation;
  margin: 0;
}
</style>

页面调用

import EditCell from "@/components/EditCell/EditCell";
components: { EditCell},

 <el-table-column
    v-for="item in tableColumn"
      :prop="item.dataIndex"
      :label="item.title"
      :width="item.width"
      :align="item.align"
      :key="item.id"
      :fixed="item.fixed"
  >
  	  //此处调用自定义组件(dataIndex 为表头数据中字段,相当于 展示表头 老师对应的 teacher名称)
      <template slot-scope="scope">
          <span v-if="item.dataIndex !== 'batchInvest' && item.dataIndex !== 'remark'">{{scope.row[item.dataIndex]}}</span>
          // 若需要格式化数据 可设置 :format-data="formatFun" formatFun此方法在当前页methods中定义即可
          <edit-cell v-else v-model="scope.row[item.dataIndex]" :can-edit="true"/>
      </template>
      <el-table-column
          v-for="item2 in item.children"
          :prop="item2.dataIndex"
          :label="item2.title"
          :width="item2.width"
          :align="item2.align"
          :key="item2.id"
          :fixed="item2.fixed"
      >
      </el-table-column>
  </el-table-column>

到此这篇关于element-ui直接在表格中点击单元格编辑的文章就介绍到这了,更多相关element 单元格编辑内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue CL3 配置路径别名详解

    Vue CL3 配置路径别名详解

    这篇文章主要介绍了Vue CL3 配置路径别名详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • 解决Vue开发中对话框被遮罩层挡住的问题

    解决Vue开发中对话框被遮罩层挡住的问题

    在Vue的开发中,一旦我们用到对话框,经常出现的问题是对话框被遮罩层挡住,怎么来解决这个问题呢?下面小编给大家带来了Vue开发中对话框被遮罩层挡住的问题及解决方法,一起看看吧
    2018-11-11
  • elementUI中的$confirm调换两个按钮位置的实例代码

    elementUI中的$confirm调换两个按钮位置的实例代码

    这篇文章主要介绍了elementUI中的$confirm调换两个按钮位置的实例代码,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • 一文带你搞懂Vue中i18n的使用方法

    一文带你搞懂Vue中i18n的使用方法

    i18n是"国际化"的简称,它的主要目的是让应用程序能够适应不同的语言和地区的需求,下面就跟随小编一起来看看Vue中i18n的具体使用方法吧
    2025-01-01
  • 基于 flexible 的 Vue 组件:Toast -- 显示框效果

    基于 flexible 的 Vue 组件:Toast -- 显示框效果

    这篇文章主要介绍了基于 flexible 的 Vue 组件:Toast -- 显示框效果,需要的朋友可以参考下
    2017-12-12
  • 详解Vue3页面如何自适应表格滚动高度

    详解Vue3页面如何自适应表格滚动高度

    这篇文章主要为大家详细介绍了Vue3页面如何自适应表格滚动高度,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • Vue3+TS实现语音播放组件的示例代码

    Vue3+TS实现语音播放组件的示例代码

    这篇文章主要介绍了如何利用Vue+TS实现一个简易的语音播放组件,文中的示例代码讲解详细,对我们学习Vue有一定的帮助,需要的可以参考一下
    2022-03-03
  • vue中的data,computed,methods,created,mounted用法及说明

    vue中的data,computed,methods,created,mounted用法及说明

    这篇文章主要介绍了vue中的data,computed,methods,created,mounted用法及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Vue3实现动态面包屑的代码示例

    Vue3实现动态面包屑的代码示例

    这篇文章主要给大家介绍一下Vue3动态面包屑是如何实现的,实现思路又是什么,以及发给大家介绍一下面包屑的功能,文章通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • vue3 axios安装及使用示例详解

    vue3 axios安装及使用示例详解

    这篇文章主要介绍了vue3 axios安装及使用示例代码,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-04-04

最新评论