main.vue 2.9 KB

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