Element-ui directly clicks on the cell in the table to edit

Element-ui directly clicks on the cell in the table to edit

Recently, because the company started using elementUI, it is necessary to edit the cells of the table during the development process. The following is my own way to make the table editable. If you are interested, you can learn about it.

Achieve results

insert image description here

After editing, the value of the corresponding table data field will also change. The console outputs all data to view the changes

Implementation Code

1. Custom editing components

<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: {
    /**
     * Binding value */
    value: {
      required: true
    },
    /**
     * Editable? */
    CanEdit:
      type: Boolean,
      default: true
    },
    /**
     * Format function */
    formatData: {
      type: Function,
      default: value => {
        return value;
      }
    },
    /**
     * Editing event */
    afterEdit: {
      type: Function,
      default: () => {}
    },
    /**
     * Whether to format initially */
    initFormat: {
      type: Boolean,
      default: false
    },
    suffix: {
      default: ""
    }
  },
  data() {
    return {
      editStatus: false,
      showData: "",
      defaultData: "",
      timeout: null
    };
  },
  methods: {
    /**
     * Click to start editing */
    beginEdit() {
      this.editStatus = true;
      setTimeout(() => {
        this.$refs.input.focus();
      }, 1);
    },

    /**
     * @param {event} event
     * Close the editing state when focus is lost and save the data*/
    loseFocus(event) {
      let value = this.formatData(event.target.value);
      this.editData(value);
      this.closeEditStatus(value);
      this.afterEdit(value);
    },

    /**
     * Publish input event to modify data * @param value
     */
    editData(value) {
      this.$emit("input", value);
    },

    /**
     * Close editing state * @param value
     */
    closeEditStatus(value) {
      this.editStatus = false;
    },
    /**
     * Initial formatting data */
    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>

Page call

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"
  >
  	  //Call the custom component here (dataIndex is the field in the header data, which is equivalent to displaying the teacher name corresponding to the header teacher)
      <template slot-scope="scope">
          <span v-if="item.dataIndex !== 'batchInvest' && item.dataIndex !== 'remark'">{{scope.row[item.dataIndex]}}</span>
          // If you need to format the data, you can set: format-data="formatFun" formatFun This method can be defined in the methods of the current page <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>

This is the end of this article about element-ui directly clicking on a cell to edit in a table. For more content related to element cell editing, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue element-ui table embedded progress bar function implementation method
  • About the issue of line break in element-ui form or table label in vue
  • Detailed explanation of the use of element-ui component el-autocomplete
  • VUE+element-ui file upload sample code
  • Detailed explanation of the code for dynamic skinning based on element-ui

<<:  Example of adding attributes using style in html

>>:  CSS 3.0 text hover jump special effects code

Recommend

js to realize a simple advertising window

This article shares the specific code of js to im...

Common causes and solutions for slow MySQL SQL statements

1. Slow query due to lack of index or invalid ind...

Velocity.js implements page scrolling switching effect

Today I will introduce a small Javascript animati...

Implementation of webpack-dev-server to build a local server

Table of contents Preface webpack-deb-server webp...

js implements single click to modify the table

Pure js implements a single-click editable table ...

Windows Server 2008 R2 Multi-User Remote Desktop Connection Licensing

At work, we often need remote servers and often e...

Summary of the understanding of virtual DOM in Vue

It is essentially a common js object used to desc...

How to install mysql5.6 in docker under ubuntu

1. Install mysql5.6 docker run mysql:5.6 Wait unt...

Discussion on horizontal and vertical centering of elements in HTML

When we design a page, we often need to center th...

mysql group_concat method example to write group fields into one row

This article uses an example to describe how to u...

MySQL 8.0.12 Quick Installation Tutorial

The installation of MySQL 8.0.12 took two days an...

A brief discussion on how to write beautiful conditional expressions in JS

Table of contents Multiple conditional statements...

Let IE support CSS3 Media Query to achieve responsive web design

Today's screen resolutions range from as smal...

Detailed explanation of Vue's seven value transfer methods

1. From father to son Define the props field in t...

How to install docker under centos and remotely publish docker in springboot

Table of contents 1. Installation of JDK1.8 under...