index.vue 4.1 KB

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