The process of using vxe-table to make editable tables in vue

The process of using vxe-table to make editable tables in vue

There is a table in the project that needs to be edited online. At first, element's el-table was used to implement it. The basic situation in the cell is to listen to the click of the cell to switch a span tag and an input tag. More complex cells use a lot of conditional judgments to implement corresponding editing operations, such as drop-down selection and popover dialog box editing. The entire table has dozens of columns and more than a dozen pieces of data, but there are already obvious lags. After many operations (such as replacing el-input with native input, reducing judgments, reducing frequent data switching, etc.), the speed has increased, but there are still visibly lags and it is basically unusable. Then I switched to vxe-table and rewrote the table. (Don’t ask me why I don’t just use the better vxe-table. Who would think of refactoring instead of trying to optimize when writing code?)

The following records the usage process.

1. Global installation

npm install xe-utils@3 vxe-table@3

Imported in main.js

import 'xe-utils';
import VXETable from 'vxe-table';
import 'vxe-table/lib/style.css';
​
Vue.use(VXETable);

In fact, it can be loaded on demand to reduce the size of the project, but I thought it was a bit troublesome so I didn't do it. If you want to know more, you can click the link below to view ~ vue-table on demand loading

2. Basic usage

<template>
    <vxe-table :align="allAlign" :data="tableData">
        <vxe-table-column type="seq" width="60"></vxe-table-column>
        <vxe-table-column field="name" title="Name"></vxe-table-column>
        <vxe-table-column field="desc" title="Description"></vxe-table-column>
        <vxe-table-column field="link" title="Link"></vxe-table-column>
    </vxe-table>
</template>
<script>
    export default {
        data () {
            return {
                allAlign: null,
                tableData: [
                    {
                        name: "html",
                        desc: 'Hypertext Markup Language',
                        link: 'https://www.runoob.com/html/html-tutorial.html'
                    },
                    {
                        name: "css",
                        desc: 'Cascading Style Sheets',
                        link: 'https://www.runoob.com/css/css-intro.html'
                    },
                    {
                        name: "js",
                        desc: 'JavaScript',
                        link: 'https://www.runoob.com/js/js-tutorial.html'
                    }
                ]
            }
        }
    }
</script>

The above is enough to realize a basic table, but now it is just a table display, and additional configuration is required to realize editing.

3. Implementation Editing

<template>
    <!--Add edit-config configuration to the table-->
    <vxe-table border :data="tableData" :edit-config="{trigger: 'click', mode: 'cell'}">
        <!--Reform the cell vxe-table-column and use edit-render to configure the editing properties--->
        <vxe-table-column title="Description" width="180" fixed="left" field="desc"
                          :edit-render="{name: 'input', attrs: {type: 'text'}}">
        </vxe-table-column>
    </vxe-table>
</template> 

For specific configuration, please refer to the api

3. Implement drop-down selection

<template>
    <vxe-table border :data="tableData" :edit-config="{trigger: 'click', mode: 'cell'}">
        <!--The only difference between the edit-render and input box editing is the configuration of edit-render. The new option selection is added in data--->
        <vxe-table-column title="Whether to display" width="180" field="isShow"
                          :edit-render="{name: 'select', options: selection, optionProps: {value: 'status', label: 'label'}}">
        </vxe-table-column>
    </vxe-table>
</template>
<script>
    export default {
        data () {
            return {
                allAlign: null,
                tableData: [
                    {
                        name: "html",
                        desc: 'Hypertext Markup Language',
                        link: 'https://www.runoob.com/html/html-tutorial.html',
                        isShow: 1
                    }
                    //Omit multiple pieces of data········
                ],
                selection: [
                    {status: 1, label: 'Yes'},
                    {status: 0, label: 'No'}
                ]
            }
        }
    }
</script>

4. Customize Templates

The vxe-table custom template is implemented using slots, which can be implemented using <template #插槽名></template> , for example:

<vxe-table-column field="name" width="120" title="Name"
                  :edit-render="{name: 'input', attrs: {type: 'text'}}">
    <!--Use #header to customize the header -->
    <template #header>
        <span>Name</span>
        <span style="font-size: 12px; color: #ccc">Technology</span>
    </template>
    <!--Use #default to customize content-->
    <template #default="{row}">
        <span>Technology Name</span>
        <span>{{row.name}}</span>
    </template>
    <!--Use #edit to customize the editing -->
    <template #edit="{row}">
        <p>Technology name</p>
        <input type="text" v-model="row.name" class="vxe-default-input">
    </template>
</vxe-table-column>

For demonstration purposes, the name column is made into an editable column, and the column header, default display content, and edit display content are customized using #header, #default, and #edit, respectively, as shown below:

5. Real-time saving function

Use the edit-closed method of vxe-table to listen for the edit box closing, and call the update interface to achieve it.

<template>
    <vxe-table border :data="tableData" :edit-config="{trigger: 'click', mode: 'cell'}"
               @edit-closed="updateData">
        <vxe-table-column title="Whether to display" width="180" field="isShow"
                          :edit-render="{name: 'select', options: selection, optionProps: {value: 'status', label: 'label'}}">
        </vxe-table-column>
    </vxe-table>
</template>
<script>
    export default {
        data () {
            // Omit...
        },
        methods: {
            updateData ({ row, column }) {
                // The background update interface accepts one piece of data, so just pass row console.log(row);
            }
        }
    }
</script>

In fact, the official method also implements checking whether the current cell content has changed, but our data structure is a bit complicated, and the method in the source code is not very applicable. Paste it here.

editClosedEvent ({ row, column }) {
    const $table = this.$refs.xTable
    const field = column.property
    const cellValue = row[field]
    // Determine whether the cell value has been modified if ($table.isUpdateByRow(row, field)) {
        setTimeout(() => {
            this.$XModal.message({
                content: `Partial save successful! ${field}=${cellValue}`,
                status: 'success'
            })
            // Partially update the cell to the saved state $table.reloadRow(row, null, field)
        }, 300)
    }
}

The above is the basic writing method for implementing an editable table. Let me study how to detect whether the data has been changed when the data is very deep.

To sum up, the editable table of vxe-table has built-in editable functions, which can be used after configuration, avoiding various judgment switches of el-table, and can implement editing functions more elegantly. In addition, it also supports virtual scrolling, which can have better performance when loading large amounts of data. The disadvantage is that when the UI diagram is determined, the table style needs to be rewritten, which is time-consuming.

I suggest that if you encounter complex tables, don't think about optimizing performance yourself. Just use vxe-table to get it done in one step. It will only increase the cost of reconstruction later. It's a lesson learned the hard way.

This is the end of this article about using vxe-table in vue to create editable tables. For more relevant vue editable table content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • vxe-table vue table table component function
  • Example of getting content and filling in the form when clicking on a row of the edit table in vuejs+element UI
  • Vue+iview sample code to implement editable table
  • Vue encapsulated editable table plug-in method

<<:  Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]

>>:  MySQL scheduled task implementation and usage examples

Recommend

Solutions to black screen when installing Ubuntu (3 types)

My computer graphics card is Nvidia graphics card...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Tutorial on resetting the root password of Mac MySQL

Disclaimer: This password reset method can direct...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

How to add abort function to promise in JS

Table of contents Overview Promise Race Method Re...

How to build a new image based on an existing image in Docker

Building new images from existing images is done ...

CSS sets Overflow to hide the scroll bar while allowing scrolling

CSS sets Overflow to hide the scroll bar while al...

Detailed explanation of the application of CSS Sprite

CSS Sprite, also known as CSS Sprite, is an image...

MySQL 5.7.17 winx64 installation and configuration graphic tutorial

I summarized the previous notes on installing MyS...

Detailed explanation of how to create an array in JavaScript

Table of contents Creating Arrays in JavaScript U...

Detailed explanation of Vue's monitoring method case

Monitoring method in Vue watch Notice Name: You s...

Vue custom v-has instruction, steps for button permission judgment

Table of contents Application Scenario Simply put...