MyDropdown.vue 3.8 KB

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