代码格式化

pull/157/head
gao 2023-08-28 11:53:54 +08:00
parent 2dcff6f5c5
commit d28af316a8
1 changed files with 122 additions and 120 deletions

View File

@ -15,14 +15,14 @@
</template> </template>
<script setup> <script setup>
import { watch } from 'vue' import { watch } from 'vue'
const total = ref(0) // const total = ref(0) //
const initParams = ref({}) const initParams = ref({})
const options = ref([]) const options = ref([])
const spinning = ref(false) const spinning = ref(false)
const emit = defineEmits({ change: null, 'update:value': null }) const emit = defineEmits({ change: null, 'update:value': null })
const props = defineProps({ const props = defineProps({
value: { value: {
type: String, type: String,
default: () => undefined default: () => undefined
@ -49,33 +49,34 @@ const props = defineProps({
type: Boolean, type: Boolean,
default: () => false default: () => false
} }
}) })
const modelValue = ref(props.value) const modelValue = ref(props.value)
watch(props, (newValue) => { watch(props, (newValue) => {
modelValue.value = newValue.value modelValue.value = newValue.value
}) })
// //
const onPage = (param = {}) => { const onPage = (param = {}) => {
if (props.pageFunction) { if (props.pageFunction) {
initParams.value = { ...initParams.value, ...param, size: props.pageSize }; initParams.value = { ...initParams.value, ...param, size: props.pageSize }
// API // API
spinning.value = true; spinning.value = true
props.pageFunction(initParams.value) props
.pageFunction(initParams.value)
.then((data) => { .then((data) => {
// //
initParams.value.current = data.current; initParams.value.current = data.current
// //
total.value = data.total; total.value = data.total
options.value = data.records; options.value = data.records
queryEcho(); queryEcho()
}) })
.finally(() => { .finally(() => {
spinning.value = false; spinning.value = false
}); })
} }
}; }
const queryEcho = () => { const queryEcho = () => {
// //
if (props.echoFunction) { if (props.echoFunction) {
nextTick(() => { nextTick(() => {
@ -86,7 +87,7 @@ const queryEcho = () => {
idList: [modelValue.value] idList: [modelValue.value]
} }
props.echoFunction(param).then((data) => { props.echoFunction(param).then((data) => {
if (data[0]){ if (data[0]) {
options.value.unshift(data[0]) options.value.unshift(data[0])
} }
}) })
@ -94,52 +95,53 @@ const queryEcho = () => {
} }
}) })
} }
} }
// change // change
const handleChange = (value, array) => { const handleChange = (value, array) => {
modelValue.value = value modelValue.value = value
// change // change
emit('change', value, array) emit('change', value, array)
// //
emit('update:value', value) emit('update:value', value)
} }
// //
const handlePopupScroll = (e) => { const handlePopupScroll = (e) => {
const { target } = e const { target } = e
const { scrollTop, scrollHeight, clientHeight } = target const { scrollTop, scrollHeight, clientHeight } = target
if (scrollTop + 2 + clientHeight >= scrollHeight) { if (scrollTop + 2 + clientHeight >= scrollHeight) {
// //
handlePagination() handlePagination()
} }
} }
// //
const handlePagination = () => { const handlePagination = () => {
// //
if (options.value.length < total.value) { if (options.value.length < total.value) {
const param = { ...initParams.value, current: initParams.value.current + 1 }; const param = { ...initParams.value, current: initParams.value.current + 1 }
spinning.value = true; spinning.value = true
props.pageFunction(param) props
.pageFunction(param)
.then((data) => { .then((data) => {
if (data.records.length > 0) { if (data.records.length > 0) {
// //
initParams.value.current = data.current; initParams.value.current = data.current
// //
const newOptions = [...options.value, ...data.records]; const newOptions = [...options.value, ...data.records]
// 使 id // 使 id
const uniqueOptions = newOptions.reduce((acc, cur) => { const uniqueOptions = newOptions.reduce((acc, cur) => {
acc[cur.id] = cur; acc[cur.id] = cur
return acc; return acc
}, {}); }, {})
options.value = Object.values(uniqueOptions); options.value = Object.values(uniqueOptions)
} }
}) })
.finally(() => { .finally(() => {
spinning.value = false; spinning.value = false
}); })
}
} }
};
defineExpose({ defineExpose({
onPage onPage
}) })
</script> </script>