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

Detailed explanation of Linux mpstat command usage

1. mpstat command 1.1 Command Format mpstat [ -A ...

Detailed explanation of Linux server status and performance related commands

Server Status Analysis View Linux server CPU deta...

Detailed explanation of mysql basic operation statement commands

1. Connect to MySQL Format: mysql -h host address...

When to use table and when to use CSS (experience sharing)

The main text page of TW used to have a width of 8...

How to choose between MySQL CHAR and VARCHAR

Table of contents VARCHAR and CHAR Types Conclusi...

Analysis of pitfalls in rounding operation of ROUND function in MySQL

This article uses examples to illustrate the pitf...

Vue implements local storage add, delete and modify functions

This article example shares the specific code of ...

Detailed explanation of MySQL's FreeList mechanism

1. Introduction After MySQL is started, BufferPoo...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

Docker build PHP environment tutorial detailed explanation

Docker installation Use the official installation...

Installation and use of mysql mycat middleware

1. What is mycat A completely open source large d...

Web page header optimization suggestions

Logo optimization: 1.The logo image should be as ...

React Class component life cycle and execution order

1. Two ways to define react components 1. Functio...