# 2.2. Element-UI 表格操作

前端中后台项目基本使用 Element-UI 库, 本文重点介绍表格使用

# 简介

# 效果图

xcooo

# 基本用法

  <template>
    <el-card>
        <el-table :data="rightsData"
                  border
                  stripe
                  :row-key="row => row.id"
                  v-loading="loading"
                  :header-cell-style="{
                        background: '#ececec',
                        color: '#909399'
                    }"
                  >
            <el-table-column type="selection"
                                :reserve-selection="true"
                                 width="55">
                </el-table-column>
            <el-table-column v-for="item in rightHeader"
                             :key="item.key"
                             :property="item.key"
                             :label="item.label"
                             :min-width="item.width"
                             :fixed="item.fixed"
                             align="center"
                             >
                <template v-slot="{row,column}">
                    <span v-if="column.property == 'time' ">
                            {{row.startTime}} ~ {{row.endTime}}
                    </span>
                    <span :style="row.actvityGroupState | statusColor"
                              v-if="column.property == 'actvityGroupState'">
                            {{row.actvityGroupState | status}}
                        </span>
                        <span v-else>{{row[column.property]}}</span>
                    <span v-else>{{row[column.property]}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template v-slot="{row}">
                    <span @click="remove(row.id)">删除</span>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
</template>

<script>
export default {
    filters: {
        status (val) {
            const arr = ['未开始', '活动中', '已结束', '已下线']
            return arr[val]
        },
        statusColor (val) {
            const arr = ['#D86704', '#32A3DC', '#F40000', '#909399']
            return `color: ${arr[val]}`
        },
    },
    data () {
        return {
            loading: false,
            rightHeader: [
                {
                    label: '编码',
                    key: 'code',
                    width: 120,
                    fixed: 'fixed'
                },
                {
                    label: '时间',
                    key: 'time',
                    width: 200,
                    fixed: 'fixed'
                },
                {
                    label: '状态',
                    key: 'actvityGroupState'
                }
            ],
            rightsData: [
                {
                    "id": 1,
                    "code": "01",
                    "name": "星城哈哈",
                    "actvityGroupState": "1",
                }, {
                    "id": 2,
                    "code": "02",
                    "name": "哈哈",
                    "actvityGroupState": "2",
                }
            ]
        };
    },
    methods: {
        remove (id) {
            const confirmResult = await this.$confirm(
                '请问是否要永久删除该数据',
                '删除提示',
                {
                    confirmButtonText: '确认删除',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err)
            if (confirmResult !== 'confirm') {
                return this.$message.info('已经取消删除')
            }
            let res = await delItem({ id })
            if (res.code !== 0) {
                return this.$message.error('删除数据失败');
            }
            const index = this.rightsDate.findIndex(item => item.id === id)
            this.rightsDate.splice(index, 1)
        },
    },
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

# 关于element-ui删除最后一页全部数据时显示无数据的bug

上次更新: 2021/5/30 下午5:43:36