The vue project encapsulates a table component, which is very slow to show hidden columns.


**:**
:20181018 
1. :JSON.parse(JSON.stringify(xxxobj))Object.assign[Object.assign][1]

try to encapsulate a general table component with a function of showing and hiding columns, as shown below:

can show the corresponding column of the hidden table by clicking the column name under "Show / hide column", but when I click many times (repeatedly), I find that the action of showing and hiding the column in table is very slow (as if there is a delay). Sometimes there is no response after clicking. It is necessary to move the mouse away (the name of the column that is clicked) before the show or hide of table can be completed, what is the problem?

the code is as follows:

Note: friends who do not want to copy can directly download the source code , in which only this demo, npm install can be checked, thank you!

myTable.vue components

<template>
    <div class="tab-wrapper">
        <div class="tab-title-wrapper">
            <div class="tab-title">table</div>
            <div class="tab-actions" @mouseover="showTableAction = true" @mouseout="showTableAction = false">
                /
                <div class="tab-action-item-wrapper" v-show="showTableAction">
                    <div v-for="head in heades" :key="head.key" @click="showColumn(head)" :class="{"active": head.isShow != false}">{{ head.label }}</div>
                </div>
            </div>
        </div>
        <table>
            <thead>
                <tr>
                    <th v-for="head in heades" v-show="head.isShow != false" :key="head.key">{{ head.label }}</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item, idx) in tabDatas" :key="idx">
                    <td v-for="(head, idx2) in heades" v-show="head.isShow != false" :key="head.key + idx2">
                        {{ item[head.key] }}
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script>
export default {
    name: "table-component",
    props: {
        tabHeades: Array,
        tabDatas: Array
    },
    data() {
        return {
            showTableAction: false,
            heades: []
        }
    },
    methods: {
        showColumn(head) {
            head.isShow = !head.isShow
            console.log(JSON.stringify(head))
        }
    },
    mounted() {
        this.heades = this.tabHeades;
    }
}
</script>

<style scoped>

.tab-wrapper, table{
    width: 100%;
}

thead th{
    cursor: pointer;
}

.tab-title-wrapper {
    display: flex;
}

.tab-title-wrapper .tab-title{
    flex: 1;
    text-align: left;
}

.tab-title-wrapper .tab-actions{
    position: relative;
    cursor: pointer;
}

.tab-title-wrapper .tab-actions .tab-action-item-wrapper{
    position: absolute;
    text-align: left;
}

.tab-title-wrapper .tab-actions .tab-action-item-wrapper .active{
    background: -sharpccc
}
</style>

parent component:

<template>
    <div>
        <my-table :tabHeades="tabHeades" :tabDatas="tabDatas"></my-table>
    </div>
</template>

<script>
import MyTable from "./myTable"
export default {
    name: "table-use",
    data() {
        return {
            tabHeades: [
                {label: "", key: "id"},
                {label: "", key: "orderId", isShow: false},
                {label: "", key: "username"}
            ],
            tabDatas: [
                {"id": 1, "orderId": "10001", "username": ""},
                {"id": 3, "orderId": "10003", "username": ""},
                {"id": 4, "orderId": "10004", "username": "aaa"},
                {"id": 5, "orderId": "10005", "username": "bbb"},
                
            ]
        }
    },
    components: {
        MyTable
    }
}
</script>
Apr.09,2021

when I saw the title and said the card, I thought it was a huge amount of data in the list. I came in and found that there were only four pieces of data.

as you said:
"the hidden action is very slow (there seems to be a delay), sometimes there is no response after clicking, and the table can only be shown or hidden after the mouse is removed (the name of the column clicked)." the problem is here, so why on earth?

the mouse leave operation is a good inspiration. You can only use mouseout in one place. Think carefully about what happened, click click and there is no response, and then move the mouse away to take effect, what happened.
move the mouse away and the template is redrawn, which, by the way, causes your table table to be updated.

in fact, your root cause is not array changes, but that your data initialization does not contain isShow attributes, and later additions make it impossible to listen.

  

Hello, can sorttable be added to col in this way? it does not take effect after I add it. attr: {prop: 'code', label:' code, sortable: 'true'}

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eab47b-2041.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eab47b-2041.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?