index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 } 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. loading: false,
  41. constant: this.number || 50,
  42. placeholder:placeholder[this.commit],
  43. selectWidt:this.width || 180,
  44. isFirst:true
  45. };
  46. },
  47. async mounted() {
  48. await this.$store.dispatch(this.commit);
  49. this.list = this.selects[this.enumer[this.commit]];
  50. this.options =
  51. this.list.length <= this.constant
  52. ? this.list
  53. : slice(this.list, 0, this.constant);
  54. throttle(this.getOptions,800)('')
  55. },
  56. methods: {
  57. remoteMethod(query) {
  58. // throttle
  59. throttle(this.getOptions,800)(query)
  60. },
  61. getOptions(query) {
  62. this.options = this.list.filter(item=>{
  63. let flag = (query&&item.userName.toLowerCase().indexOf(query.toLowerCase())>-1) || item.userId == query
  64. if(this.multiple){
  65. return flag || this.value.includes(item.userId)
  66. }else{
  67. return flag || item.userId == this.value
  68. }
  69. })
  70. },
  71. changeValue(val){
  72. this.isFirst = false
  73. this.$emit("input", val);
  74. this.$emit("change",val)
  75. }
  76. },
  77. computed: {
  78. enumer() {
  79. return {
  80. setTeachers: "teachers",
  81. setEducations: "educations",
  82. };
  83. },
  84. },
  85. watch:{
  86. value:{
  87. immediate:true,
  88. deep:true,
  89. handler(val){
  90. if(val&&this.isFirst){
  91. throttle(this.getOptions,800)('')
  92. }
  93. }
  94. }
  95. }
  96. };
  97. </script>
  98. <style lang="scss" scoped>
  99. </style>