SupplierSelector.vue 3.5 KB

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