ExtSelect.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <el-select-v2
  3. :options="state.list"
  4. :disabled="disabled"
  5. :multiple="multiple"
  6. filterable
  7. :clearable="clearable"
  8. :placeholder="placeholder"
  9. :max-collapse-tags="max"
  10. @change="handleValueChange"
  11. @clear="handleValueClear"
  12. v-model="state.modelVal">
  13. <template #empty>
  14. <el-empty :image-size="40"/>
  15. </template>
  16. <template #prefix>
  17. <SvgIcon :name="prefix"/>
  18. </template>
  19. <!-- <el-option :key="item.id" v-for="item in state.list" :value="item.id" :label="item.name||item.title"> {{ item.name || item.title }}</el-option>-->
  20. </el-select-v2>
  21. </template>
  22. <script lang="ts" setup name="ExtSelect">
  23. import {onMounted, ref, nextTick, watch, reactive} from 'vue';
  24. import u from "/@/utils/u";
  25. import {$body, $post, $get} from "/@/utils/request";
  26. const emit = defineEmits(['update:modelValue', 'on-change']);
  27. const props = defineProps({
  28. modelValue: {
  29. type: Number
  30. },
  31. prefix: {
  32. type: String
  33. },
  34. max: {
  35. type: Number,
  36. default: 3
  37. },
  38. url: {
  39. type: String
  40. },
  41. query: {
  42. type: Object
  43. },
  44. disabled: {
  45. type: Boolean,
  46. default: false
  47. },
  48. clearable: {
  49. type: Boolean,
  50. default: true
  51. },
  52. multiple: {
  53. type: Boolean,
  54. default: false
  55. },
  56. placeholder: {
  57. type: String,
  58. default: '请选择'
  59. },
  60. dataList: {
  61. type: Array<any>
  62. },
  63. urlMethod: {
  64. type: String,
  65. default: 'post'
  66. },
  67. labelKey: {
  68. type: String,
  69. default: 'name'
  70. },
  71. valueKey: {
  72. type: String,
  73. default: 'id'
  74. },
  75. dataKey:{
  76. type:String,
  77. default:'list'
  78. }
  79. })
  80. const state = reactive({
  81. list: [] as Array<any>,
  82. modelVal: null
  83. })
  84. watch(() => props.modelValue, (val) => {
  85. console.log("watch>>>", val)
  86. state.modelVal = val;
  87. })
  88. watch(() => props.dataList, (val) => {
  89. console.log("watch>>>", val)
  90. nextTick(() => {
  91. if (!u.isEmptyOrNull(props.dataList)) {
  92. // state.list = props.dataList;
  93. state.list = props.dataList?.map((k: any) => {
  94. return {value: k[props.valueKey], label: k[props.labelKey]}
  95. })
  96. }
  97. })
  98. },{deep:true})
  99. onMounted(() => {
  100. loadData();
  101. })
  102. const loadData = () => {
  103. if (!u.isEmptyOrNull(props.dataList)) {
  104. // state.list = props.dataList;
  105. state.list = props.dataList?.map((k: any) => {
  106. return {value: k[props.valueKey], label: k[props.labelKey]}
  107. })
  108. } else {
  109. if (!props.url) {
  110. return;
  111. }
  112. var query = {
  113. pageIndex: 1,
  114. pageSize: 200,
  115. };
  116. if (props.query) {
  117. query = Object.assign({}, query, props.query);
  118. }
  119. if (props.urlMethod?.toLowerCase() === 'post') {
  120. $body(`${props.url}`, query).then((list: any) => {
  121. // let {list,count} = res;
  122. let dataList = [];
  123. if(props.dataKey){
  124. dataList = list[props.dataKey]
  125. }else{
  126. dataList = list
  127. }
  128. state.list = dataList.map((k: any) => {
  129. return {value: k[props.valueKey], label: k[props.labelKey]}
  130. })
  131. console.log(state.list)
  132. // state.value.list = res?.list
  133. });
  134. } else {
  135. $get(`${props.url}`, query).then((list: any) => {
  136. // let {list,count} = res;
  137. let dataList = [];
  138. if(props.dataKey){
  139. dataList = list[props.dataKey]
  140. }else{
  141. dataList = list
  142. }
  143. state.list = dataList.map((k: any) => {
  144. return {value: k[props.valueKey], label: k[props.labelKey]}
  145. })
  146. /*
  147. if (list.list) {
  148. state.list = list.list.map((k: any) => {
  149. return {value: k[props.valueKey], label: k[props.labelKey]}
  150. })
  151. } else {
  152. state.list = list.map((k: any) => {
  153. return {value: k[props.valueKey], label: k[props.labelKey]}
  154. })
  155. }
  156. */
  157. console.log(state.list)
  158. // state.value.list = res?.list
  159. });
  160. }
  161. }
  162. }
  163. const handleValueChange = (val: any) => {
  164. nextTick(() => {
  165. console.log("handleValueChange--->", val)
  166. emit("update:modelValue", val);
  167. emit("on-change", val);
  168. })
  169. }
  170. const handleValueClear = () => {
  171. // props.query.id = undefined;
  172. // this.query['id'] = undefined;
  173. // let val: any = [];
  174. // if (!props.multiple) {
  175. // val = undefined;
  176. // }
  177. // // emit("update:value", val);
  178. // // emit("change", val);
  179. // loadData();
  180. }
  181. </script>