SearchInput.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <a-select
  3. show-search
  4. :value="selectValue"
  5. style="width: 200px"
  6. :allow-clear="true"
  7. :not-found-content="fetching ? undefined : null"
  8. :placeholder="$t('lang.QueryConditionComplex.pleaseInputTheContent')"
  9. :options="options"
  10. @search="searchDatas"
  11. @change="selectChanged"
  12. >
  13. <template v-if="fetching" #notFoundContent>
  14. <a-spin size="small" />
  15. </template>
  16. </a-select>
  17. </template>
  18. <script>
  19. import Common from '../../common/Common';
  20. import { message } from 'ant-design-vue';
  21. // 防抖处理(避免重复请求)
  22. const debounce = (fn, delay) => {
  23. let timer = null;
  24. return function (...args) {
  25. if (timer) {
  26. clearTimeout(timer);
  27. }
  28. timer = setTimeout(() => {
  29. fn.apply(this, args);
  30. }, delay);
  31. };
  32. };
  33. export default {
  34. props: {
  35. value: {
  36. type: String,
  37. default: null,
  38. },
  39. apiUrl: {
  40. type: String,
  41. default: null,
  42. },
  43. apiResponseDataTextFiledName: {
  44. type: String,
  45. default: null,
  46. },
  47. },
  48. emits: ['update:value'],
  49. data() {
  50. return {
  51. selectValue: null,
  52. fetching: false,
  53. options: [{ value: '请输入查询内容', label: '请输入查询内容' }],
  54. };
  55. },
  56. watch: {
  57. apiResponseDataTextFiledName: {
  58. handler(newValue) {
  59. console.log(newValue, 'apiResponseDataTextFiledName');
  60. },
  61. immediate: true,
  62. },
  63. },
  64. methods: {
  65. // 搜索事件
  66. searchDatas:debounce(function(value){
  67. this.getDatas(value);
  68. this.$emit('update:value', value);
  69. },600),
  70. // 选择后更新值
  71. selectChanged(value) {
  72. this.selectValue = value;
  73. this.$emit('update:value', value);
  74. },
  75. // 请求获取搜索数据
  76. getDatas(value) {
  77. const that = this;
  78. if (!value) {
  79. return;
  80. }
  81. that.options = [];
  82. that.fetching = true;
  83. let filedName;
  84. const params = { start: 0, length: 99999, condition: value };
  85. if (that.apiResponseDataTextFiledName && that.apiResponseDataTextFiledName.includes('->')) {
  86. filedName = that.apiResponseDataTextFiledName.split('->')[1];
  87. }
  88. $.ajax({
  89. type: 'get',
  90. url: Common.getApiURL(that.apiUrl),
  91. data: params,
  92. beforeSend: function (request) {
  93. Common.addTokenToRequest(request);
  94. },
  95. success: function ({ errorCode, errorMessage, datas }) {
  96. if (errorCode == 0) {
  97. if (datas && datas.length) {
  98. // 数据处理(value,label)
  99. datas.forEach(item => {
  100. if (filedName) {
  101. item.value = item[filedName];
  102. item.label = item[filedName];
  103. that.options.push(item);
  104. } else {
  105. that.options.push({ value: item, label: item });
  106. }
  107. });
  108. }
  109. } else {
  110. message.warning(errorMessage);
  111. }
  112. that.fetching = false;
  113. },
  114. error: function (XMLHttpRequest, textStatus, errorThrown) {
  115. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  116. that.fetching = false;
  117. },
  118. });
  119. },
  120. },
  121. };
  122. </script>