main.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <el-select
  3. v-model="value"
  4. multiple
  5. filterable
  6. remote
  7. clearable
  8. :multiple-limit="1"
  9. reserve-keyword
  10. :size="size || 'medium'"
  11. style="width: 100%"
  12. :placeholder="placeholder || ''"
  13. :disabled="disabled"
  14. :remote-method="remoteMethod"
  15. :loading="selectLoading"
  16. @change="selectChange"
  17. >
  18. <el-option
  19. v-for="(item, index) in options"
  20. :key="item.id + index + 'brand'"
  21. :label="item.brand_name"
  22. :value="item.id"
  23. :disabled="item.status === '0'"
  24. >
  25. </el-option>
  26. </el-select>
  27. </template>
  28. <script>
  29. import asyncRequest from "@/apis/components/search-brand";
  30. import resToken from "@/mixins/resToken";
  31. export default {
  32. name: "SearchBrand",
  33. mixins: [resToken],
  34. props: [
  35. "size",
  36. "value",
  37. "placeholder",
  38. "isDetail",
  39. "disabled",
  40. "type",
  41. "names",
  42. ],
  43. /**
  44. * 属性集合
  45. * @param {String} size : 组件大小 非必填
  46. * @param {Array} value : 选中值 必填
  47. * @param {String} placeholder : 提示信息 非必填
  48. * @param {Boolean} isDetail : 是否是详情逻辑 必填
  49. * @param {Boolean} disabled : 是否禁用 必填
  50. * @param {String} names : 选中值label 展示详情必填
  51. */
  52. /**
  53. * 事件集合
  54. * @searchChange : 选中值变化调用 抛出选中数据
  55. */
  56. data() {
  57. return {
  58. options: [],
  59. selectLoading: false,
  60. searchName: "",
  61. formValue: {
  62. page: 1,
  63. size: 100,
  64. start: "",
  65. end: "",
  66. creater: "",
  67. status: "",
  68. brand_name: "",
  69. },
  70. };
  71. },
  72. watch: {
  73. names: function (val, old) {
  74. // console.log(val, old, this.isDetail, "1");
  75. this.searchName = val;
  76. if (this.isDetail && this.searchName) {
  77. this.remoteMethod(this.searchName);
  78. }
  79. },
  80. isDetail: function (val, old) {
  81. // console.log(val, old, this.isDetail, "2");
  82. if (val && this.searchName) {
  83. this.remoteMethod(this.searchName);
  84. }
  85. },
  86. },
  87. mounted() {
  88. this.options = [];
  89. this.selectLoading = false;
  90. },
  91. methods: {
  92. async selectChange(e) {
  93. if (e && e.length > 0) {
  94. let index = this.options.findIndex((v) => v.id === e[0]);
  95. if (index !== -1) {
  96. let model = {
  97. id: this.options[index].id,
  98. code: this.options[index].id,
  99. label: this.options[index].brand_name,
  100. };
  101. this.$emit("searchChange", model);
  102. } else {
  103. this.$emit("searchChange", {});
  104. }
  105. } else {
  106. this.$emit("searchChange", {});
  107. }
  108. },
  109. async remoteMethod(query) {
  110. this.selectLoading = true;
  111. if (query !== "") {
  112. this.options = [];
  113. this.formValue.brand_name = query;
  114. const { code, data, message } = await asyncRequest.list(this.formValue);
  115. if (code === 0) {
  116. const { list } = data;
  117. this.options = list;
  118. } else if (code >= 100 && code <= 104) {
  119. await this.logout();
  120. } else {
  121. this.$message.warning(message);
  122. }
  123. } else {
  124. this.options = [];
  125. }
  126. this.selectLoading = false;
  127. },
  128. },
  129. };
  130. </script>
  131. <style>
  132. </style>