1
lzhe
2024-05-13 f5edc2904945f37b164a7874d502cf002fae024e
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!--
 * @Date: 2024-04-09 22:18:47
 * @LastEditors: Sneed
 * @LastEditTime: 2024-04-13 22:11:04
 * @FilePath: /belleson-frontend/Users/mache/Documents/demo/cps-web/src/views/console/system/component-classification.vue
 * 分类维护
-->
<template>
    <el-main>
        <el-card shadow="never">
            <el-container>
                <el-header style="justify-content: flex-start;">
                    <el-button type="primary" plain @click="add">新增</el-button>
                    <el-button @click="del" plain type="danger" :disabled="selection.length == 0">删除</el-button>
                </el-header>
                <el-main>
                    <el-table @selection-change="handleSelectionChange" :data="tableData"
                        style="width: 100%;margin-bottom: 20px;" row-key="id" border
                        :tree-props="{ hasChildren: 'has' }">
                        <el-table-column type="selection" width="55" />
                        <el-table-column prop="name" label="分类名称" />
                        <el-table-column prop="parentId" label="上级分类" />
                        <el-table-column prop="icon" label="分类图标">
                            <template #default="scope">
                                <component v-if="icons.includes(scope.row.icon)" style="width: 20px;height: 20px;"
                                    :is='scope.row.icon'></component>
                            </template>
                        </el-table-column>
                        <el-table-column prop="type" label="分类编号" />
                        <el-table-column prop="orderNum" label="排列顺序" />
                        <el-table-column prop="status" label="状态">
                            <template #default="scope">
                                <span>{{ scope.row.status === 1 ? '启用' : '停用' }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" fixed="right" align="left" width="160">
                            <template #default="scope">
                                <el-button-group>
                                    <el-button text type="primary" size="small"
                                        @click="table_edit(scope.row, scope.$index)">编辑</el-button>
                                    <el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row, scope.$index, '0')">
                                        <template #reference>
                                            <el-button text type="primary" size="small">删除</el-button>
                                        </template>
                                    </el-popconfirm>
                                </el-button-group>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-main>
                <el-dialog v-model="dialogVisible" :title="dialogTitle" width="750" :before-close="handleClose">
                    <el-form :model="row" :rules="rules" ref="dialogForm" label-width="120px" label-position="center">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="分类名称" prop="name">
                                    <el-input style="width: 240px" v-model="row.name" placeholder="请输入分类名称"
                                        clearable></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="上级分类" prop="parentId">
                                    <el-select v-model="row.parentId" filterable placeholder="请选择上级分类"
                                        style="width: 240px">
                                        <el-option v-for="item in options" :key="item.value" :label="item.label"
                                            :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="分类图标" prop="icon">
                                    <el-input @click="showIconDialog = true" style="width: 240px" v-model="row.icon"
                                        placeholder="请选择分类图标" clearable></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="分类编号" prop="type">
                                    <el-input style="width: 240px" v-model="row.type" placeholder="请输入分类编号"
                                        clearable></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="排列顺序" prop="orderNum">
                                    <el-input-number style="width: 240px" default v-model="row.orderNum" :min="0"
                                        placeholder="请输入排列顺序" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态" prop="status">
                                    <el-radio-group style="width: 240px" v-model="row.status">
                                        <el-radio v-for="item in statusList" :label="item.value" :key="item.value">{{
                                            item.label
                                            }}</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <template #footer>
                        <div class="dialog-footer">
                            <el-button @click="dialogVisible = false">取消</el-button>
                            <el-button type="primary" @click="save">
                                保存
                            </el-button>
                        </div>
                    </template>
                </el-dialog>
                <el-dialog v-model="showIconDialog" title="分类图标" width="750">
                    <div class="icons">
                        <component @click="setIcon(item)" class="icon" v-for="item in icons" :is='item' :key="item">
                        </component>
                    </div>
 
                </el-dialog>
            </el-container>
        </el-card>
    </el-main>
</template>
 
<script>
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
let icons = []
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    icons.push(key)
}
export default {
    components: {
        ...ElementPlusIconsVue
    },
    data() {
        return {
            tableData: [],
            selection: [],
            dialogVisible: false,
            row: {
                id: '',
                name: '',
                parentId: '',
                icon: '',
                type: '',
                orderNum: 0,
                status: 1,
            },
            rules: {
                name: [
                    { required: true, message: '请输入分类名称' }
                ],
                type: [
                    { required: true, message: '请输入分类编号' }
                ]
            },
            options: [],
            statusList: [
                {
                    label: '启用',
                    value: 1
                },
                {
                    label: '停用',
                    value: 0
                }
            ],
            showIconDialog: false,
            icons
        }
    },
    created() {
        this.queryList()
    },
    methods: {
        add() {
            this?.$refs?.dialogForm?.resetFields()
            Object.keys(this.row).forEach(v => {
                this.row[v] = ''
            })
            this.row.orderNum = 0
            this.dialogTitle = '新增'
            this.dialogVisible = true
        },
        save() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    let request = this.$API.setting.insert
                    if (this?.row?.id) {
                        request = this.$API.setting.update
                    }
                    request.post(this.row).then(res => {
                        this.dialogVisible = false
                        this.queryList()
                    })
                }
            })
        },
        del() {
            this.$API.setting.del.delete({}, { data: this.selection.map(v => v.id) }).then(res => {
                this.queryList()
            })
        },
        queryList() {
            this.$API.setting.getList.get().then(res => {
                this.tableData = res.data;
                this.options = res.data.map(item => ({
                    value: item.id,
                    label: item.name
                }))
            })
        },
        table_edit(row) {
            this.row = { ...row }
            console.log(this.row)
            this.dialogTitle = '编辑'
            this.dialogVisible = true
        },
        table_del(row) {
            this.$API.setting.del.delete({}, { data: [row.id] }).then(res => {
                this.queryList()
            })
        },
        handleSelectionChange(selection) {
            console.log('>>>>>>')
            this.selection = selection;
        },
        setIcon(icon) {
            console.log('>>>>>>', icon)
            this.row.icon = icon
            this.showIconDialog = false
        }
    },
}
</script>
 
<style lang="scss" scoped>
.icons {
    display: flex;
    flex-wrap: wrap;
}
 
.icon {
    margin: 10px;
    width: 20px;
    height: 20px;
    cursor: pointer;
}
</style>