MyDropdown.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <u-form-item label="商品名称" :prop="'detailInfos[' + index_experience + '].tradeName'" :required="true">
  3. <el-autocomplete
  4. style="width: 100%;"
  5. v-model="inputForm.detailInfos[index_experience].tradeName"
  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.tradeName }}</span>
  14. </template>
  15. </el-autocomplete>
  16. </u-form-item>
  17. </template>
  18. <script>
  19. import materialManagementService from '@/api/materialManagement/MaterialManagementService'
  20. export default {
  21. props: {
  22. index_experience: Number,
  23. inputForm: Object
  24. },
  25. data() {
  26. return {
  27. showDropdownList: false,
  28. dropdownList: [],
  29. selectedTradeName: ''
  30. };
  31. },
  32. created() {
  33. },
  34. methods: {
  35. showDropdown() {
  36. let row = this.inputForm.detailInfos[this.index_experience]
  37. row.tradeNameList = []
  38. row.tradeNameData = []
  39. let procurementTypeId = this.inputForm.detailInfos[this.index_experience].procurementTypeId
  40. if (this.isNotEmpty(procurementTypeId)) {
  41. // Simulated data
  42. materialManagementService.findTradeByTypeId(procurementTypeId)
  43. .then((data) => {
  44. row.tradeNameList = JSON.parse(JSON.stringify(data))
  45. this.dropdownList = JSON.parse(JSON.stringify(data))
  46. this.showDropdownList = true; // Move this line here
  47. })
  48. .catch(() => {
  49. // Handle error
  50. });
  51. }
  52. },
  53. isEmpty(value) {
  54. let result = false;
  55. if (value == null || value == undefined) {
  56. result = true;
  57. }
  58. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  59. result = true;
  60. }
  61. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  62. result = true;
  63. }
  64. return result;
  65. },
  66. isNotEmpty (value) {
  67. return !this.isEmpty(value)
  68. },
  69. fetchSuggestions(queryString, callback) {
  70. if (this.dropdownList && queryString) {
  71. const filteredList = this.dropdownList.filter(item =>
  72. item.tradeName.toLowerCase().includes(queryString.toLowerCase())
  73. );
  74. callback(filteredList);
  75. } else {
  76. callback(this.dropdownList);
  77. // Handle the case when dropdownList or queryString is not properly initialized
  78. }
  79. },
  80. handleSelect(item) {
  81. this.inputForm.detailInfos[this.index_experience].tradeName = item.tradeName;
  82. this.showDropdownList = false;
  83. }
  84. }
  85. };
  86. </script>
  87. <style scoped>
  88. .my-dropdown {
  89. position: absolute;
  90. top: calc(100% + 4px); /* 将下拉框定位在输入框的下方,此处增加了 4px 的间距 */
  91. left: 0;
  92. width: 100%;
  93. background-color: #fff;
  94. border: 1px solid #ccc;
  95. }
  96. .list-item {
  97. padding: 8px;
  98. cursor: pointer;
  99. }
  100. </style>