tree结构数据单选示例

1、tree结构数据单选示例

1、首先处理接口请求到的数据
import { getTestTypeAPI } from '@/api/questions/set'

import { formatSortQue } from '@/utils/format'

format.js文件中:

export function formatSortQue(arr) {
    let formatArr = []

    function formatFun(arr) {
        arr.forEach(item => {
            formatArr.push({ code: item.code, label: item.name })
            if (item.list.length > 0) {
                formatFun(item.list)
            }
        })
    }
    formatFun(arr)
    return formatArr
}

应用页面.vue文件中:

getSortList() {//分类列表
    getClassListAPI({code:'01',bankId:this.bankId}).then(res => {
        if(res.data.code === 0){
            this.sortList = res.data.data[0].list
            this.options = formatSortQue(this.sortList)
		    this.sortList.unshift({name:'默认分类',code:'',id:null})
        } else {
            this.$message.error('获取数据失败');
        }
    })
},
2、将数据处理成tree结构
data() {
	return {
		typeName: null,
		defaultProps: {
            children: 'list',
            label: 'name'
        },
        sortList: [],
		options: [],
	}
},

加上接口数据处理方式:

getSortList() {//分类列表
    getClassListAPI({code:'01',bankId:this.bankId}).then(res => {
        if(res.data.code === 0){
            this.sortList = res.data.data[0].list
            
            //将数据处理成层级选择下拉样式
            this.options = formatSortQue(this.sortList)
		    this.sortList.unshift({name:'默认分类',code:'',id:null})
        } else {
            this.$message.error('获取数据失败');
        }
    })
},
3、页面布局,层级展示tree数据,与select标签结合,下拉选择数据
<el-form-item label="试题分类:" style="display:inline-block">
	<el-popover popper-class="category-popover" placement="bottom-start" trigger="click" ref="popover">
		<el-tree :data="sortList" node-key="code" :props="defaultProps"
			:highlight-current="true" :expand-on-click-node="false"
			:current-node-key="typeName" @node-click="handleNodeClick"
			style=" max-height: 400px; overflow-y: auto; width:380px;"></el-tree>
		<el-select v-model="typeName" style="width:400px" popper-class="hidden-selection" slot="reference">
			<el-option :value="null" label="默认分类"></el-option>
			<el-option v-for="item in options" :key="item.code" :label="item.label" :value="item.code"></el-option>
		</el-select>
	</el-popover>
</el-form-item>

4、选择事件,控制传值与关闭下拉弹窗

handleNodeClick(data) {//点击分类--搜索
    //数据选择后,向后台传值
    this.typeName = data.name
	this.dataForm.diyTypeCode = data.code
	this.dataForm.diyTypeId = data.id
	
	//数据选择后关闭选择下拉弹窗,以保证只能每次选一条数据,即单选功能
    this.$refs.popover.doClose()
},

5、若是编辑页面,需要返显选中的分类,则在访问数据详情时,对select的v-model进行赋值即可

getDet() {// 获取试题详情
	getTestDetailsAPI(this.testEditId).then(res => {
		if(res.data.code === 0) {
			let data = res.data.data
			this.dataForm = data
			this.dataForm.content = data.content
			
			//对typeName进行赋值,保持数据统一,即可返显分类数据
			this.typeName = data.diyTypeCode
			
		} else {
			this.$message.error('获取数据失败')
		}
	})
},
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值