|
|
@@ -1,5 +1,5 @@
|
|
|
<script setup lang="ts">
|
|
|
-import { ref, toRefs, withDefaults, onMounted } from "vue"
|
|
|
+import { ref, toRefs, withDefaults, onMounted, watch } from "vue"
|
|
|
import Treeselect from "vue3-treeselect-ts"
|
|
|
import "vue3-treeselect-ts/dist/style.css"
|
|
|
import type { AxiosRequestConfig } from "axios"
|
|
|
@@ -12,6 +12,7 @@ interface TreeOption {
|
|
|
const props = withDefaults(
|
|
|
defineProps<{
|
|
|
value: string | null | undefined
|
|
|
+ id?: string //id变化后重新加载树
|
|
|
name?: string
|
|
|
url?: string
|
|
|
method?: string
|
|
|
@@ -55,8 +56,8 @@ const emits = defineEmits<{
|
|
|
(e: "update:value", v: string): void
|
|
|
(e: "select", v: any): void
|
|
|
}>()
|
|
|
-const { staticOptions, url, value } = toRefs(props)
|
|
|
-const options = ref<Array<TreeOption>>(staticOptions.value ?? [])
|
|
|
+const { staticOptions, url, value, id } = toRefs(props)
|
|
|
+const options = ref<Array<TreeOption>>(Object.assign([], staticOptions.value ?? []))
|
|
|
|
|
|
function formatData(data: any) {
|
|
|
if (props.dataFormatFunction) {
|
|
|
@@ -82,6 +83,7 @@ function formatData(data: any) {
|
|
|
}
|
|
|
|
|
|
function init() {
|
|
|
+ options.value = Object.assign([], staticOptions.value ?? [])
|
|
|
if (url?.value) {
|
|
|
const processData = (data: any) => {
|
|
|
data.forEach((v: any) => {
|
|
|
@@ -104,19 +106,6 @@ function init() {
|
|
|
processData(res.data)
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
- //const configs = Object.assign({}, { url: url.value, method: "get", successAlert: false }, props.configs)
|
|
|
- // Rs.request(configs).then((res: any) => {
|
|
|
- // //const data = [] as Array<TreeOption>
|
|
|
- // res.data.forEach((v: any) => {
|
|
|
- // const item = formatData(v)
|
|
|
- // options.value.push(item)
|
|
|
- // })
|
|
|
- // if (!value.value) {
|
|
|
- // const val = options.value[0].id
|
|
|
- // emits("update:value", val)
|
|
|
- // }
|
|
|
- // })
|
|
|
}
|
|
|
}
|
|
|
function onSelect(data: any) {
|
|
|
@@ -124,13 +113,19 @@ function onSelect(data: any) {
|
|
|
emits("update:value", data.id)
|
|
|
emits("select", data)
|
|
|
}
|
|
|
+watch(() => props.id, init)
|
|
|
onMounted(() => {
|
|
|
init()
|
|
|
})
|
|
|
+function load() {
|
|
|
+ init()
|
|
|
+}
|
|
|
+defineExpose({ load })
|
|
|
</script>
|
|
|
<template>
|
|
|
<treeselect
|
|
|
v-model="value"
|
|
|
+ :id="id"
|
|
|
:placeholder="placeholder"
|
|
|
:loadingText="loadingText"
|
|
|
:multiple="multiple"
|