import { getArea } from '@/api/data' const areaList = { template: ` `, props: { value: { type: Array, defatut: function(){ return [] } }, id: { type: String, default: '' }, placeholder:{ type: String, default: '请选择' }, changeOnSelect:{ type: Boolean, default: false }, level:{ type: [String,Number], default: '' } }, data() { return { defaultVal: this.value, list: [] }; }, mounted() { this.getArea() }, watch: { value(newValue, oldValue) { this.defaultVal = newValue } }, methods: { filterOption(inputValue, path) { return path.some(option => option.name.toLowerCase().indexOf(inputValue.toLowerCase()) > -1); }, handleChange(value,opts) { this.defaultVal = value; this.$emit('input', value); this.$emit('change', value,opts); }, getArea () { getArea({}).then(res => { if (res.status == 200) { this.list = this.getBylevel(res.data) } else { this.list = [] } }) }, getBylevel(data){ const a = data.filter(item => item.type == 2) // 省 const b = data.filter(item => item.type == 3) // 市 const c = data.filter(item => item.type == 4) // 县 // 显示一级分类 if(this.level == 1){ return a } // 显示二级分类 if(this.level == 2){ a.map(item => { item.children = b.filter(k => k.parentId == item.id) }) console.log(a) return a } // 全部显示 b.map(item => { item.children = c.filter(k => k.parentId == item.id) }) a.map(item => { item.children = b.filter(k => k.parentId == item.id) }) return a } }, }; export default areaList