通过改变css使el-tree只显示第一层的复选框

效果

 代码:

 <el-form-item label="商品分类" prop="cate">
        <el-select v-model="careTag" placeholder="请选择" multiple>
               <el-option :value="treeselectArr" style="height: auto">
                  <el-tree ref="tree" 
                    check-strictly 
                    highlight-current 
                    :data="treedata" 
                    :props="defaultProps"
                    node-key="id" 
                    show-checkbox 
                    @check-change="handleNodeClick">
                    </el-tree>
                 </el-option>
         </el-select>
  
<style lang="scss" scoped>
:deep(.el-tree-node) {
    .el-checkbox .el-checkbox__inner {
        display: inline-block;
    }
    .el-tree-node__children .el-checkbox__inner {
        display: none;
    }
}
</style>

只要对比class类,就能使用

举例:

<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm" size="mini">
            
            <el-form-item label="商品分类" prop="cate">
                <el-select v-model="cateTg" placeholder="" multiple>
                    <el-input placeholder="输入关键字查找" v-model="filterText"> </el-input>
                    <el-option :value="treeselectArr" style="height: auto" class="cate">
                        <el-tree ref="tree" check-strictly :data="treedata" :filter-node-method="filterNode" :props="defaultProps" node-key="id" show-checkbox @check-change="handleNodeClick"></el-tree>
                    </el-option>
                </el-select>
            </el-form-item>
            <!--  -->
            
        </el-form>
 
    </div>
</template>

<script> 
export default { 
    data() {
        return {
            filterText: '',
            cateTg: [],
            treeselectArr: [],
            defaultProps: {
                children: 'children',
                label: 'label'
            },
            // 分类
            treedata: [
                {
                    id: 123,
                    label: '河北'
                },
                {
                    id: 32,
                    label: '河南'
                },
                {
                    id: 1,
                    label: '云南',
                    children: [
                        {
                            id: 2,
                            label: '昆明',
                            children: [
                                { id: 3, label: '五华区', children: [{ id: 8, label: '北辰小区' }] },
                                { id: 4, label: '盘龙区' }
                            ]
                        }
                    ]
                },
                {
                    id: 5,
                    label: '湖南',
                    children: [
                        { id: 6, label: '长沙' },
                        { id: 7, label: '永州' }
                    ]
                }
            ],
          
        }
    },
    created() {
        console.log(this.ruleForm)
    },
    // 分类查找
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val)
        }
    },
    methods: {
        // 分类查找
        filterNode(value, data) {
            if (!value) return true
            return data.label.indexOf(value) !== -1
        },
        // 选择分类
        handleNodeClick(data) {
            // 选中的key
            var epIds = this.$refs.tree.getCheckedKeys()
            console.log('选中的KEYid', epIds)

            var cateArr = []
            cateArr = this.$refs.tree.getCheckedNodes().map(function(item) {
                return item.label
            })
            this.cateTg = cateArr
            console.log('选中的tag', cateArr)
            // 选中的所有数据
            this.ruleForm.cate = this.$refs.tree.getCheckedNodes()
            console.log('选中的完整数据', this.ruleForm.cate)
            if (this.ruleForm.cate) {
                // this.$nextTick(() => {
                //     this.$refs['ruleForm'].clearValidate()
                // })
            }
        },
      
    }
}
</script>

<style lang="scss" scoped> 
:deep(.el-tag.el-tag--info .el-tag__close) {
    display: none;
}
 
.cate {
    padding: 0 0 !important;
}
</style>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值