index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. // 处理乐团主管数据请求
  64. const commit = this.commit == 'setEducations' ? 'setOrganRole' : this.commit
  65. await this.$store.dispatch(commit);
  66. if(commit == 'setOrganRole') {
  67. this.list = this.selects.roles[this.enumer[commit]];
  68. } else {
  69. this.list = this.selects[this.enumer[commit]];
  70. }
  71. const data = {};
  72. for (const item of this.list) {
  73. data[item.userId] = item;
  74. }
  75. this.listById = data;
  76. this.options =
  77. this.list.length <= this.constant
  78. ? this.list
  79. : slice(this.list, 0, this.constant);
  80. this.options = uniqBy(this.options, "userId");
  81. // (this.options)
  82. },
  83. remoteMethod(query) {
  84. // throttle
  85. throttle(this.getOptions, 800)(query);
  86. },
  87. async getOptions(query) {
  88. if (query && query.length > 0) {
  89. let flag;
  90. this.options = uniqBy(
  91. this.list.filter((item) => {
  92. flag =
  93. item.userName
  94. .toLowerCase()
  95. .indexOf(query.toString().toLowerCase()) > -1 ||
  96. item.userId == query;
  97. if (this.multiple) {
  98. return flag || this.value.includes(item.userId);
  99. } else {
  100. // (query,this.value)
  101. return flag || item.userId == this.value;
  102. }
  103. }),
  104. "userId"
  105. );
  106. } else {
  107. try {
  108. await this.getList();
  109. const optionids = this.options.map((item) => item.userId);
  110. const valueItem = this.listById[this.value];
  111. if (!optionids.includes(this.value) && valueItem) {
  112. this.options.push(valueItem);
  113. this.options = uniqBy(this.options, "userId");
  114. }
  115. } catch (e) {
  116. // (e)
  117. }
  118. }
  119. },
  120. changeValue(val) {
  121. this.isFirst = false;
  122. this.$emit("input", val);
  123. this.$emit("change", val);
  124. // ('来了',this.$refs.select.query)
  125. this.$nextTick((res) => {
  126. this.getOptions(this.$refs.select.query);
  127. });
  128. },
  129. onFocus() {
  130. this.$nextTick((res) => {
  131. this.getOptions(this.$refs.select.query);
  132. });
  133. },
  134. },
  135. computed: {
  136. enumer() {
  137. return {
  138. setTeachers: "teachers",
  139. setOrganRole: "educationId",
  140. setEmploys:'employs'
  141. };
  142. },
  143. },
  144. watch: {
  145. value: {
  146. immediate: true,
  147. deep: true,
  148. handler(val) {
  149. // && this.isFirst
  150. if (this.multiple && this.isFirst) {
  151. if (val?.length > 0) {
  152. this.getOptions();
  153. }
  154. } else {
  155. if (val && this.isFirst) {
  156. this.getOptions();
  157. } else {
  158. this.getOptions(val);
  159. }
  160. }
  161. },
  162. },
  163. },
  164. };
  165. </script>
  166. <style lang="scss" scoped>
  167. </style>