DeviceTypeCascade.vue 3.56 KB
<template>
	<el-cascader
		v-model="selectedIds"
	    placeholder="请选择"
	    :options="items"
	    :props="defaultProps"
	    filterable
	    change-on-select
	    @change="handleChange"
	    size="small"
	    :disabled="disabled"
	></el-cascader>
</template>
<script>
	import {services} from '../../api'
	const DEVICE_SERVICE =  services.device;

	export default {
		props: ['notPrepend', 'id', 'disabled', 'init', 'dataFilter'],
		data(){
			return {
				treeOriginData: [],
				items: [],
				defaultProps: {
					children: 'children',
					label: 'name',
					value: 'id'
				},
				selectedIds: ['']
			}
		},
		mounted(){
			DEVICE_SERVICE.getDeviceTypeList().then((response)=>{
				let data = response.data;
				if(this.dataFilter){
					this.treeOriginData = data.filter(this.dataFilter)
				}else{
					this.treeOriginData = data
				}
				const parentItem = this.findTreeParent();
				this.treeParentId = parentItem.id;
				this.findTreeChild(parentItem)

				if(!this.notPrepend){
					parentItem.children.unshift({
						name: '全部',
						id: ''
					})
				}
				
				this.items = parentItem.children

			})
			
		},
		watch: {
			id(){
				if(this.id !== ''){
					if(this.init){
						this.initSelectedIds(this.id)
						this.init = false
					}
				}else{
					this.selectedIds = ['']
				}
			}
		},
		methods: {
			findTreeParent(arr = this.treeOriginData){
				return arr.find((item) => item.parentId === -1)
			},
			findTreeChild(parentItem, arr = this.treeOriginData){
				const childItems = arr.filter((item) => item.parentId === parentItem.id)
				
				childItems.forEach((childItem) => {
					this.findTreeChild(childItem)
				})
				if(childItems.length){
					parentItem.children = childItems
				}
			},
			initSelectedIds(id){
				let arr = [id]
				let citem = this.treeOriginData.find((item) => item.id === id)
				let pItem = this.treeOriginData.find((item) => item.id === citem.parentId)
				while(pItem){
					arr.push(pItem.id)
					pItem = this.treeOriginData.find((item) => item.id === pItem.parentId)
				}
				console.log(arr)
				this.selectedIds = arr.reverse()
			},
			handleChange(){
				const id = [...this.selectedIds].pop()
				this.$emit('onTypeChange', id)
			}
		}
	}

</script>
<style type="text/css" media="screen">
.el-cascader__label {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    padding: 0 25px 0 15px;
    color: #606266;
    width: 100%;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    box-sizing: border-box;
    cursor: pointer;
    text-align: left;
    font-size: inherit;
}
.el-cascader-menus {
    white-space: nowrap;
    background: #fff;
    position: absolute;
    margin: 5px 0;
    z-index: 2;
    border: 1px solid #e4e7ed;
    border-radius: 2px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}
.el-cascader-menu {
    display: inline-block;
    vertical-align: top;
    height: 204px;
    overflow: auto;
    border-right: 1px solid #e4e7ed;
    background-color: #fff;
    box-sizing: border-box;
    margin: 0;
    padding: 6px 0;
    min-width: 160px;
}
.el-cascader-menu__item {
    font-size: 13px;
    padding: 8px 20px;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: #606266;
    height: 34px;
    line-height: 1.5;
    box-sizing: border-box;
    cursor: pointer;
    outline: none;
}
.el-cascader-menu__item.is-active {
    color: #094;
}
.el-cascader-menu__item--extensible:after {
    font-family: element-icons;
    content: "\e6e0";
    font-size: 14px;
    color: #bfcbd9;
    position: absolute;
    right: 15px;
}
</style>