index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div>
  3. <!-- @focus="onFocus" -->
  4. <el-select
  5. v-bind="{ ...$attrs }"
  6. :value="value"
  7. filterable
  8. remote
  9. reserve-keyword
  10. collapse-tags
  11. clearable
  12. :multiple="multiple"
  13. :placeholder="placeholder"
  14. :remote-method="remoteMethod"
  15. ref="select"
  16. :loading="loading"
  17. @focus="onFocus"
  18. @change="changeValue"
  19. :style="{ width: this.selectWidt + 'px!important' }"
  20. >
  21. <el-option
  22. v-for="item in options"
  23. :key="item.userId"
  24. :label="item.userName"
  25. :value="item.userId"
  26. >
  27. </el-option>
  28. </el-select>
  29. </div>
  30. </template>
  31. <script>
  32. const placeholder = {
  33. setTeachers: "请选择老师",
  34. setEducations: "请选乐团主管",
  35. setEmploys:"请选择操作人"
  36. };
  37. import { throttle, slice, uniqBy } from "lodash";
  38. import selects from "@/store/modules/selects";
  39. export default {
  40. name: "remote-search",
  41. props: ["commit", "number", "value", "width", "multiple", "ariaPlaceholder"],
  42. data() {
  43. return {
  44. options: [],
  45. list: [],
  46. listById: {},
  47. loading: false,
  48. constant: this.number || 50,
  49. placeholder: this.ariaPlaceholder || placeholder[this.commit],
  50. selectWidt: this.width || 180,
  51. isFirst: true,
  52. };
  53. },
  54. async mounted() {
  55. // this.getList();
  56. // console.log(this.value)
  57. this.getOptions(this.value || "");
  58. },
  59. methods: {
  60. async getList() {
  61. await this.$store.dispatch(this.commit);
  62. this.list = this.selects[this.enumer[this.commit]];
  63. const data = {};
  64. for (const item of this.list) {
  65. data[item.userId] = item;
  66. }
  67. this.listById = data;
  68. this.options =
  69. this.list.length <= this.constant
  70. ? this.list
  71. : slice(this.list, 0, this.constant);
  72. this.options = uniqBy(this.options, "userId");
  73. // console.log(this.options)
  74. },
  75. remoteMethod(query) {
  76. // throttle
  77. throttle(this.getOptions, 800)(query);
  78. },
  79. async getOptions(query) {
  80. if (query && query.length > 0) {
  81. let flag;
  82. this.options = uniqBy(
  83. this.list.filter((item) => {
  84. flag =
  85. item.userName
  86. .toLowerCase()
  87. .indexOf(query.toString().toLowerCase()) > -1 ||
  88. item.userId == query;
  89. if (this.multiple) {
  90. return flag || this.value.includes(item.userId);
  91. } else {
  92. // console.log(query,this.value)
  93. return flag || item.userId == this.value;
  94. }
  95. }),
  96. "userId"
  97. );
  98. } else {
  99. try {
  100. await this.getList();
  101. const optionids = this.options.map((item) => item.userId);
  102. const valueItem = this.listById[this.value];
  103. if (!optionids.includes(this.value) && valueItem) {
  104. this.options.push(valueItem);
  105. this.options = uniqBy(this.options, "userId");
  106. }
  107. } catch (e) {
  108. // console.log(e)
  109. }
  110. }
  111. },
  112. changeValue(val) {
  113. this.isFirst = false;
  114. this.$emit("input", val);
  115. this.$emit("change", val);
  116. // console.log('来了',this.$refs.select.query)
  117. this.$nextTick((res) => {
  118. this.getOptions(this.$refs.select.query);
  119. });
  120. },
  121. onFocus() {
  122. this.$nextTick((res) => {
  123. this.getOptions(this.$refs.select.query);
  124. });
  125. },
  126. },
  127. computed: {
  128. enumer() {
  129. return {
  130. setTeachers: "teachers",
  131. setEducations: "educations",
  132. setEmploys:'employs'
  133. };
  134. },
  135. },
  136. watch: {
  137. value: {
  138. immediate: true,
  139. deep: true,
  140. handler(val) {
  141. // && this.isFirst
  142. if (this.multiple && this.isFirst) {
  143. if (val?.length > 0) {
  144. this.getOptions();
  145. }
  146. } else {
  147. if (val && this.isFirst) {
  148. this.getOptions();
  149. } else {
  150. this.getOptions(val);
  151. }
  152. }
  153. },
  154. },
  155. },
  156. };
  157. </script>
  158. <style lang="scss" scoped>
  159. </style>