当前位置:   article > 正文

使用uni-ui 实现下拉搜索框(uniapp+uni-ui+vue3 开发微信小程序)_uni-ui 下拉框 远程加载

uni-ui 下拉框 远程加载

需求:输入框中输入内容-->远端搜索匹配的选项-->展示选项(可点击选择选项)

代码实现

htm:(一个输入框input + list)

  1. <view class="search-select-box" >
  2. <uni-easyinput
  3. v-model="searchCode"
  4. placeholder="输入搜索"
  5. @input="onInput"
  6. @change="onInput"
  7. class="search-input"
  8. />
  9. <uni-list class="Dropdown" v-show="searchCode.length > 0">
  10. <uni-list-item
  11. class="Dropdown-item"
  12. v-for="(item,index) in state.codeList" :key="index"
  13. clickable
  14. @click="onCodeClick(item)"
  15. >
  16. <template v-slot:body>
  17. {{item.text}}
  18. </template>
  19. </uni-list-item>
  20. </uni-list>
  21. </view>

js:

  1. let isInfoShow = ref(false)
  2. let state = reactive({
  3. codeList:[]
  4. })
  5. let searchCode = ref('')
  6. // 输入搜索唯一编码
  7. const onCodeInput = async (val)=>{
  8. if(searchCode.value.length == 0){
  9. state.codeList = []
  10. return
  11. }
  12. const params = {
  13. code: val
  14. }
  15. //调取后端接口获取 state.codeList
  16. const { data } = await GetUniqueCodeSearchList(params)
  17. if (data.success) {
  18. state.codeList = data.data
  19. } else {
  20. state.codeList = []
  21. uni.showToast({
  22. title: data.message,
  23. icon: 'none'
  24. })
  25. }
  26. }
  27. // 选定唯一编码添加构件
  28. const onCodeClick = (item)=>{
  29. //根据不同业务
  30. console.log(item,'选定的值')
  31. searchCode.value = ''
  32. state.codeList = []
  33. }

css

  1. .search-select-box{
  2. position: relative;
  3. margin-top: 8rpx;
  4. }
  5. .Dropdown{
  6. position: absolute;
  7. top:78rpx;
  8. left: 0rpx;
  9. width:100%;
  10. height: 300rpx;
  11. overflow-y: auto;
  12. }
  13. .Dropdown-item{
  14. background-color: rgb(198, 198, 198);
  15. z-index: 1;
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/678912
推荐阅读
相关标签
  

闽ICP备14008679号