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
|
<template> <Cascader :data="country.children" :load-data="loadData" v-model="selection" change-on-select @on-change="onChange"/> </template>
<script> export default { props: { address: { type: Object, default: () => { return { regionIds: [] }; } }, }, data() { return { selection: [], country : { value: 0, children: [] }, }; }, mounted() { this.loadHierarchyRegions(this.country, [...this.address.regionIds], () => { this.selection.push(...this.address.regionIds); }); }, methods: { onChange(value, selectedData) { this.address.path = ''; this.address.regionIds = [];
for (const region of selectedData) { this.address.regionIds.push(region.value); this.address.path += region.label; }
this.$emit('on-change', this.address); }, loadData(item, callback) { item.loading = true;
this.loadRegions(item, () => { item.loading = false; callback(); }); }, loadRegions(parent, callback) { Rest.get({ url: '/api/regions', data: { parentId: parent.value } }).then(regions => { for (const region of regions) { if (region.childrenCount === 0) { parent.children.push({ label: region.name, value: region.id, children: [] }); } else { parent.children.push({ label: region.name, value: region.id, children: [], loading: false }); } }
callback && callback(); }); },
loadHierarchyRegions(parent, regionIds, finishCallback) {
this.loadRegions(parent, () => { let topId = regionIds.shift(); let topRegion = parent.children.find(child => child.value === topId);
if (topRegion) { this.loadHierarchyRegions(topRegion, regionIds, finishCallback); } else { finishCallback && finishCallback(); } }); } }, }; </script>
|