RemoteSelect.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <el-select
  3. filterable
  4. :multiple="multiple"
  5. :multiple-limit="multiple ? 0 : 1"
  6. clearable
  7. :placeholder="placeholder"
  8. :loading="loading"
  9. @change="selectItem"
  10. @focus="getSelectList"
  11. >
  12. <el-option
  13. v-for="item in options"
  14. :key="item.key"
  15. :label="item.value"
  16. :value="{ value: item.key, key: item.value }"
  17. />
  18. </el-select>
  19. </template>
  20. <script>
  21. import { onMounted, ref } from "vue";
  22. import api from "../apis/fetch";
  23. import _ from "lodash";
  24. export default {
  25. props: {
  26. placeholder: {
  27. type: String,
  28. default: "请填写",
  29. },
  30. size: {
  31. type: String,
  32. default: "small",
  33. },
  34. clearable: {
  35. type: Boolean,
  36. default: true,
  37. },
  38. api: String,
  39. params: Object,
  40. multiple: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. },
  45. emits: ["selectItem"],
  46. setup(props, { emit }) {
  47. let loading = ref(false);
  48. let options = ref([]);
  49. const getSelectList = async () => {
  50. if (options.value.length > 0) {
  51. return;
  52. }
  53. loading.value = true;
  54. try {
  55. let res = await api[props.api]({
  56. ...props.params,
  57. term: "",
  58. });
  59. if (res.data.status == 0) {
  60. options.value = res.data.result;
  61. } else {
  62. options.value = [];
  63. }
  64. } catch (error) {
  65. console.error("Failed to fetch select list:", error);
  66. options.value = [];
  67. } finally {
  68. loading.value = false;
  69. }
  70. };
  71. const selectItem = (item) => {
  72. emit("selectItem", item);
  73. };
  74. onMounted(() => {});
  75. return {
  76. getSelectList,
  77. selectItem,
  78. options,
  79. loading,
  80. };
  81. },
  82. };
  83. </script>
  84. <style></style>