SupplierSelector.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <u-form-item label="供应商" :prop="'detailInfos[' + index_experience + '].supplierName'" :required="true">
  3. <el-autocomplete
  4. style="width: 100%;"
  5. v-model="inputForm.detailInfos[index_experience].supplierName"
  6. placeholder="请选择供应商"
  7. clearable
  8. @focus="showDropdown"
  9. :fetch-suggestions="fetchSuggestions"
  10. @select="handleSelect"
  11. >
  12. <template slot-scope="{ item }">
  13. <span style="width: 100%;">{{ item.name }}</span>
  14. </template>
  15. </el-autocomplete>
  16. </u-form-item>
  17. </template>
  18. <script>
  19. import SupplierService from '@/api/materialManagement/SupplierService'
  20. export default {
  21. props: {
  22. index_experience: Number,
  23. inputForm: Object
  24. },
  25. data() {
  26. return {
  27. showDropdownList: false,
  28. selectedSupplier: null,
  29. suppliers: [] // 供应商对象数组 [{id: 1, name: 'Supplier 1'}, ...]
  30. };
  31. },
  32. supplierService: null,
  33. created () {
  34. this.supplierService = new SupplierService()
  35. this.showDropdown()
  36. },
  37. methods: {
  38. showDropdown() {
  39. // 将获取的供应商赋值给 this.suppliers
  40. this.supplierService.list({
  41. 'current': 1,
  42. 'size': -1,
  43. }).then((data) => {
  44. this.suppliers = data.records
  45. this.showDropdownList = true;
  46. })
  47. },
  48. isEmpty(value) {
  49. let result = false;
  50. if (value == null || value == undefined) {
  51. result = true;
  52. }
  53. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  54. result = true;
  55. }
  56. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  57. result = true;
  58. }
  59. return result;
  60. },
  61. isNotEmpty (value) {
  62. return !this.isEmpty(value)
  63. },
  64. fetchSuggestions(queryString, callback) {
  65. if (this.suppliers && queryString) {
  66. const filteredList = this.suppliers.filter(item =>
  67. item.name.toLowerCase().includes(queryString.toLowerCase())
  68. );
  69. callback(filteredList);
  70. } else {
  71. callback(this.suppliers);
  72. // Handle the case when suppliers or queryString is not properly initialized
  73. }
  74. },
  75. handleSelect(item) {
  76. this.inputForm.detailInfos[this.index_experience].supplierName = item.name;
  77. this.inputForm.detailInfos[this.index_experience].supplierId = item.id;
  78. this.showDropdownList = false;
  79. }
  80. }
  81. };
  82. </script>
  83. <style scoped>
  84. .my-dropdown {
  85. position: absolute;
  86. top: calc(100% + 4px); /* 将下拉框定位在输入框的下方,此处增加了 4px 的间距 */
  87. left: 0;
  88. width: 100%;
  89. background-color: #fff;
  90. border: 1px solid #ccc;
  91. }
  92. .list-item {
  93. padding: 8px;
  94. cursor: pointer;
  95. }
  96. </style>