• 106.(前端)分类管理显示增加值tag——使用elementui中的动态编辑标签搭建结构


    1.概述

    在这里插入图片描述
    官方网址

    1. 去到这里面选择需要用的代码粘贴
    2. data()返回值设置默认不显示inputVisible和空列表inputValue
    3. 添加点击函数showInput():当点击编辑标签时,弹出一个input输入框
    4. 动态编辑标签存在于多行,存在同时被修改的问题(bug,使用第一个,第二个也被改)
    5. 解决方法: 在遍历时,不显示列表,并设置空列表;并且修改原先的绑定的Visibel和inputValue为查槽获取到的
    6. 实现触发动态编辑标签

    2.效果展示

    在这里插入图片描述

    3.完整代码展示

    
    <template>
        <div>
            
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/home' }">首页el-breadcrumb-item>
                <el-breadcrumb-item>商品管理el-breadcrumb-item>
                <el-breadcrumb-item>分类管理el-breadcrumb-item>
            el-breadcrumb>
            
            <el-card>
                
                <el-alert title="注意:只允许为第三级的分类设置相关参数!!!" type="warning" close-text="知道了">
                el-alert>
                <el-row>
                    <el-col>
                        <span>选择商品分类:span>
                        <el-cascader 
                            v-model="selectKeys"
                            :options="cateIdList"
                            :props="{ expandTrigger: 'hover', label:'name', value:'id'}" 
                            clearable 
                            separator=" > "
                            @change="changeSelector">
                        el-cascader>
                    el-col>
                el-row>
                
                <el-row>
                    <el-col>
                        <el-tabs v-model="activeName" @tab-click="handleClick">
                            
                            <el-tab-pane label="静态参数" name="static">
                                <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">增加参数el-button>
                                <el-table :data="staticAttr">
                                    
                                    <el-table-column type="expand">
                                        
                                        <template slot-scope="scope">
                                            <el-tag>{{ scope.row.val }}el-tag>
                                        template>
                                    el-table-column>
                                    
                                    <el-table-column type="index">el-table-column>
                                    <el-table-column label="参数名称" prop="name">el-table-column>
                                    <el-table-column label="操作">
                                        <template slot-scope="scope">
                                            <el-button type="success" size="mini">编辑el-button>
                                            <el-button type="danger" size="mini">删除el-button>
                                        template>
                                    el-table-column>
                                el-table>
                            el-tab-pane>
                            
                            <el-tab-pane label="动态参数" name="dynamic">
                                <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="addDialogVisible=true">增加参数el-button>
                                <el-table :data="dynamicAttr">
                                    
                                    <el-table-column type="expand">
                                        
                                        <template slot-scope="scope">
                                            <el-tag v-for="(v,i) in scope.row.val" :key="i">{{ v }}el-tag>
                                            
                                            <el-input 
                                            class="input-new-tag" 
                                            v-if="scope.row.inputVisible" 
                                            v-model="scope.row.inputValue" 
                                            ref="saveTagInput" 
                                            size="small"
                                            @keyup.enter.native="handleInputConfirm(scope.row)"
                                            @blur="handleInputConfirm(scope.row)">
                                            el-input>
                                            <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">
                                                + New Tag
                                            el-button>
                                        template>
                                    el-table-column>
                                    
                                    <el-table-column type="index">el-table-column>
                                    <el-table-column label="参数名称" prop="name">el-table-column>
                                    <el-table-column label="操作">
                                        <template slot-scope="scope">
                                            <el-button type="success" size="mini">编辑el-button>
                                            <el-button type="danger" size="mini">删除el-button>
                                        template>
                                    el-table-column>
                                el-table>
                            el-tab-pane>
                        el-tabs>
                    el-col>
                el-row>
            el-card>
            <el-dialog :title="'添加'+titleText" :visible.sync="addDialogVisible" width="30%" @close="addDialogClose">
                <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
                    <el-form-item :label="titleText" prop="name">
                        <el-input v-model="addForm.name">el-input>
                    el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="addAttr">立即创建el-button>
                        <el-button>取消el-button>
                    el-form-item>
                el-form>
            el-dialog>
        div>
    template>
    
    <script>
    
    export default{
        data() {
            return {
                cateIdList: [],
                selectKeys:[],
                activeName: 'static',
                staticAttr:[],
                dynamicAttr:[],
                dynamicFlag: false,
                staticFlag: false,
                addDialogVisible: false,
                addForm: {
                    name: ''
                },
                addFormRules: {
                    name: [{ required: true, message: '请填写参数名称', tigger:'blur'}]
                },
                inputValue: [],
                inputVisible: false
            }
        },
        created() {
            this.getCateIDList()
        },
        methods:{
            // 获取整个列表
            async getCateIDList(){
                const { data: resp } = await this.$axios.get('/api/category_list')
                this.cateIdList = resp.data.data
            },
            // 发送变化时触发此函数
            changeSelector(){
                if (this.selectKeys.length < 3){
                    this.staticAttr = []
                    this.dynamicAttr = []
                    return
                }
                // console.log(this.selectKeys);
                this.dynamicFlag = true
                this.staticFlag = true
                this.getAttribute()
            },
            handleClick(tab, event) {
                // console.log(tab, event);
                
                if (!this.staticFlag && this.activeName === 'static') return
                if (!this.dynamicFlag && this.activeName === 'dynamic') return
                if (this.selectKeys.length < 3) return
                // console.log(this.selectKeys[2]);
                console.log(this.activeName);
                this.getAttribute()
            },
            // 获取列表数据
            async getAttribute() {
                const { data: resp } = await this.$axios.get('/api/category/attr_list', {
                    params: { cid: this.selectKeys[2], _type: this.activeName}
                })
                if (resp.status !== 200) return this.$msg.error(resp.msg)
                console.log(resp.data);
                if (this.activeName === 'static'){
                    this.staticAttr = resp.data
                    this.staticFlag = false
                }else{
                    resp.data.forEach( item => {
                        item.val = item.val? item.val.split(','):[]
                        item.inputValue='',
                        item.inputVisible=false
                    })
                    this.dynamicAttr = resp.data
                    this.dynamicFlag = false
                }
            },
            addDialogClose() {
                this.$refs.addFormRef.resetFields()
            },
            // 添加参数
            async addAttr() {
                // 验证内容是否满足rule
                this.$refs.addFormRef.validate(async valid =>{
                    if (!valid) return
                    // // 测试:获取表单内容
                    // console.log(this.addForm.name);
                    // // 测试:获取表单数据的type
                    // console.log(this.activeName);
                    // // 测试:获取表单数据的id
                    // console.log(this.selectKeys[2]);
    
                    // 发送请求
                    const { data: resp } = await this.$axios.post(
                        '/api/category/attribute',
                        this.$qs.stringify({
                            cid: this.selectKeys[2],
                            _type: this.activeName,
                            name: this.addForm.name
                        })
                    )
                    if (resp.status !== 200 ) return this.$msg.error(resp.msg)
                    this.$msg.success(resp.msg)
                    this.getAttribute()
                    this.addDialogVisible = false
    
                })
            },
            // 动态编辑标签,点击函数
            showInput(row) {
                row.inputVisible = true,
                    this.$nextTick(_ => {
                        this.$refs.saveTagInput.$refs.input.focus();
                    });
            },
            // 触发函数
            handleInputConfirm(row) {
                row.inputVisible = false;
                row.inputValue = '';
            }
        },
        computed:{
            titleText(){
                if (this.activeName === 'static') return '静态参数'
                else return '动态参数'
            },
            isBtnDisabled(){
                if (this.selectKeys.length >= 3){
                    return false
                }else{
                    return true
                }
            }
        }
    }
    script>
    
    <style lang="less" scoped>
        .el-tabs{
            margin-top: 5px;
        }
        .el-cascader{
            width: 300px;
            margin-top: 10px;
        }
        .el-tag{
            margin:5px
        }
        .input-new-tag{
            width: 100px;
        }
    style>
    
    • 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
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
  • 相关阅读:
    力扣刷题day27|455分发饼干、376摆动序列、53最大子序和
    AlphaFold2源码解析(1)--安装使用
    LLaMA Adapter和LLaMA Adapter V2
    物理学中的不确定性的根源,有哪些看法
    css布局相关内容
    JAVA毕业设计HTML5“牧经校园疫情防控网站”设计与实现计算机源码+lw文档+系统+调试部署+数据库
    软件测试面试题:自动化测试脚本开发的主要步骤?
    LiveMedia视频中间件视频隐私打码直播解决方案
    迅捷 FW300R固件升级参考
    C语言希尔排序
  • 原文地址:https://blog.csdn.net/m0_63953077/article/details/127621739