index.vue 3.0 KB

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