index.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script setup lang="ts">
  2. import { ref, watch } from 'vue';
  3. import request from '~/utils/request';
  4. const props = defineProps<{
  5. url?: string,
  6. to?: 'team' | 'grade' | 'class',
  7. modelValue?: string,
  8. modelName?: string,
  9. change?: (item: object | undefined) => void
  10. }>()
  11. const emits = defineEmits(["update:modelValue", "update:modelName"])
  12. const loading = ref(false)
  13. const value = ref(props.modelValue ? props.modelValue.split(',') : [])
  14. const names = props.modelName ? props.modelName.split(',') : []
  15. const options = ref<{ name: string, value: string, origin?: {} }[]>((value.value.length > 0) ? value.value.map((val, idx) => ({
  16. value: val,
  17. name: names[idx]
  18. })) : [])
  19. remoteMethod().then(() => {
  20. //
  21. // console.log('true :>> ', props.modelValue, props.modelName, options.value.find(({ value: val }) => val === props.modelValue));
  22. if (props.modelValue && props.modelName) {
  23. value.value.forEach((val, idx) => {
  24. if (!options.value.find((option) => val === option.value)) {
  25. options.value.unshift({
  26. value: val,
  27. name: names[idx],
  28. })
  29. }
  30. })
  31. }
  32. })
  33. function remoteMethod(keyword?: string) {
  34. loading.value = true
  35. return request({
  36. url: props.to ? `/jcxx/${props.to}/index` : props.url,
  37. data: {
  38. keyword,
  39. limit: 15
  40. }
  41. }).then(({ data }) => {
  42. options.value = data.page_data.map((item: { [index: string]: string }) => ({
  43. name: item[props.to + '_name'],
  44. value: item[props.to + '_id'],
  45. origin: item
  46. }))
  47. loading.value = false
  48. })
  49. }
  50. watch(
  51. () => value.value,
  52. (val) => {
  53. const list = val.map(v => options.value.find(({ value }) => value === v)).sort();
  54. // console.log('list :>> ', list, list.map(({ value }) => value).join(',')), list.map(({ name }) => name).join(',')));
  55. emits("update:modelValue", list.map((i) => i?.value).join(','));
  56. emits("update:modelName", list.map((i) => i?.name).join(','));
  57. props.change?.(list.map((i) => i?.origin));
  58. }
  59. )
  60. watch(() => props.modelValue, (val) => {
  61. if (val === undefined) {
  62. value.value = []
  63. }
  64. })
  65. </script>
  66. <template>
  67. <el-select class="w-full" v-model="value" filterable remote reserve-keyword clearable :remote-method="remoteMethod"
  68. :loading="loading" multiple>
  69. <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value" />
  70. </el-select>
  71. </template>