ClazzSelect.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <el-select
  3. v-model="selected"
  4. class="clazz-select"
  5. popper-class="popper-filter"
  6. :placeholder="placeholder"
  7. :style="styles"
  8. :clearable="clearable"
  9. :disabled="disabled"
  10. :multiple="multiple"
  11. @change="select"
  12. >
  13. <div class="el-select-filter">
  14. <el-input
  15. v-model="filterLabel"
  16. placeholder="请输入"
  17. clearable
  18. @input="labelChange"
  19. ></el-input>
  20. </div>
  21. <el-option
  22. v-for="item in filterOptionList"
  23. :key="item.id"
  24. :value="item.id"
  25. :label="item.name"
  26. >
  27. </el-option>
  28. </el-select>
  29. </template>
  30. <script>
  31. import { clazzQuery } from "../../modules/base/api";
  32. export default {
  33. name: "clazz-select",
  34. props: {
  35. disabled: { type: Boolean, default: false },
  36. placeholder: { type: String, default: "请选择" },
  37. value: { type: [Number, String, Array], default: "" },
  38. styles: { type: String, default: "" },
  39. clearable: { type: Boolean, default: true },
  40. multiple: { type: Boolean, default: false },
  41. campusId: { type: String, default: "" },
  42. datas: {
  43. type: Array
  44. }
  45. },
  46. data() {
  47. return {
  48. optionList: [],
  49. filterOptionList: [],
  50. selected: "",
  51. filterLabel: ""
  52. };
  53. },
  54. watch: {
  55. value: {
  56. immediate: true,
  57. handler(val) {
  58. this.selected = val;
  59. }
  60. },
  61. datas: {
  62. immediate: true,
  63. handler(val) {
  64. if (!val) return;
  65. this.optionList = val.map(item => {
  66. return { ...item };
  67. });
  68. this.labelChange();
  69. }
  70. },
  71. campusId(val, oldval) {
  72. if (val !== oldval) {
  73. this.search();
  74. this.$emit("input", "");
  75. this.$emit("change", null);
  76. }
  77. }
  78. },
  79. created() {
  80. if (!this.datas) this.search();
  81. },
  82. methods: {
  83. async search() {
  84. const res = await clazzQuery({
  85. campusId: this.campusId
  86. });
  87. this.optionList = res || [];
  88. this.labelChange();
  89. },
  90. labelChange() {
  91. const escapeRegexpString = (value = "") =>
  92. String(value).replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
  93. const reg = new RegExp(escapeRegexpString(this.filterLabel), "i");
  94. this.filterOptionList = this.optionList.filter(item =>
  95. reg.test(item.name)
  96. );
  97. },
  98. select() {
  99. this.$emit("input", this.selected);
  100. if (this.multiple) {
  101. this.$emit(
  102. "change",
  103. this.optionList.filter(item => this.selected.includes(item.id))
  104. );
  105. } else {
  106. this.$emit(
  107. "change",
  108. this.optionList.find(item => item.id === this.selected)
  109. );
  110. }
  111. }
  112. }
  113. };
  114. </script>