当前位置:   article > 正文

Vue2 + element ui el-select 远程搜索分页懒加载功能实现_el-select 分页加载

el-select 分页加载

新建指令 :

        1、 在 src 目录下 新建文件夹 directive / loadmore

        2、在 loadmore 文价夹下新建 elSelectLoadmore.js 和 index.js 文件:        

   elSelectLoadmore.js

  1. export default {
  2. inserted(el, binding, vnode) {
  3. const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
  4. SELECTWRAP_DOM.addEventListener('scroll', function() {
  5. const condition = Math.floor(this.scrollHeight - this.scrollTop) <= this.clientHeight;
  6. if (condition) {
  7. binding.value();
  8. }
  9. });
  10. }
  11. };

index.js

  1. import elSelectLoadmore from './elSelectLoadmore';
  2. const install = function(Vue) {
  3. Vue.directive('el-select-loadmore', elSelectLoadmore);
  4. };
  5. if (window.Vue) {
  6. window['el-select-loadmore'] = elSelectLoadmore
  7. Vue.use(install); // eslint-disable-line
  8. }
  9. export default install;

3、在main 文件中注册该指令

  1. import elSelectLoadmore from './directive/loadmore'
  2. Vue.use(elSelectLoadmore)

4、 基于 el-select 封装 懒加载 远程搜索框

remoteSelect.vue

  1. <template>
  2. <el-select
  3. ref="remoteSelect"
  4. :disabled="disabled"
  5. style="width:100%"
  6. v-model="remoteCode"
  7. filterable
  8. remote
  9. clearable
  10. reserve-keyword
  11. placeholder="请输入软件id / 软件名称"
  12. :remote-method="remoteMethod"
  13. v-el-select-loadmore="loadmore"
  14. :loading="loading"
  15. @input="input"
  16. :multiple="multiple"
  17. >
  18. <el-option
  19. v-for="item in options"
  20. :key="item.id"
  21. :label="item.id + ' ' +item.name"
  22. :value="item.id"
  23. >
  24. </el-option>
  25. <div class="uploadTip"
  26. v-if="loadMoreLoading && !loadNoMore"
  27. >数据加载中......</div>
  28. <div
  29. class="uploadTip"
  30. v-if="loadNoMore"
  31. >暂无更多数据</div>
  32. </el-select>
  33. </template>
  34. <script>
  35. import { getSoftwareForRef } from "@/api/softwar/manage";
  36. export default {
  37. name:'remote-select',
  38. props:{
  39. value:{
  40. type:[Number, String, Array],
  41. default:undefined
  42. },
  43. disabled:{
  44. type: Boolean,
  45. default:false
  46. },
  47. multiple:{
  48. type:Boolean,
  49. default:false
  50. },
  51. status:{
  52. type:Number,
  53. default:undefined
  54. }
  55. },
  56. data(){
  57. return {
  58. remoteQuery:{
  59. pageNum:1,
  60. pageSize:10,
  61. status:this.status //接口状态参数
  62. },
  63. options:[],
  64. loading:false, // 组件搜索加载中参数
  65. loadMoreLoading:false, // 翻页加载中参数
  66. remoteCode:undefined, // select value
  67. loadNoMore:false // 没有更多的状态
  68. }
  69. },
  70. mounted(){
  71. },
  72. watch:{
  73. value: {
  74. handler(data) {
  75. this.remoteCode = data
  76. },
  77. immediate: true
  78. },
  79. },
  80. methods: {
  81. input(val){
  82. this.$emit('input',val)
  83. const index = this.options.findIndex(i => i.id === val)
  84. this.$emit('inputAll', index === -1 ? val : this.options[index])
  85. },
  86. remoteMethod(query, review) {
  87. this.loading = true
  88. this.loadNoMore = false
  89. this.remoteQuery.pageNum = 1
  90. if(typeof query === 'object'){
  91. this.remoteQuery.swIds = query
  92. }else{
  93. this.remoteQuery.param = query
  94. delete this.remoteQuery.swIds
  95. }
  96. this.remoteQuery.status = this.status
  97. getSoftwareForRef(this.remoteQuery).then(res => {
  98. this.options = res.data
  99. this.loading = false;
  100. if(typeof review === 'boolean' && review){
  101. let index = this.options.findIndex(i => i.id === this.value)
  102. if(index !== -1){
  103. this.remoteCode = this.value + ' ' + this.options[index].name
  104. }else{
  105. this.remoteCode = this.value
  106. }
  107. }
  108. })
  109. },
  110. loadmore(){
  111. if(this.loadMoreLoading){
  112. return
  113. }
  114. var remoteQuery = {...this.remoteQuery};
  115. remoteQuery.pageNum++;
  116. this.loadMoreLoading = true
  117. getSoftwareForRef(remoteQuery).then(res => {
  118. if(res.code === 0 && typeof res.data === 'object' && res.data.length){
  119. this.options = this.options.concat(res.data)
  120. this.remoteQuery.pageNum++
  121. }
  122. if(res.code === 0 && typeof res.data === 'object' && res.data.length === 0){
  123. this.loadNoMore = true;
  124. }
  125. this.$nextTick(() => {
  126. setTimeout(() => {
  127. this.loadMoreLoading = false
  128. }, 500)
  129. })
  130. }).catch(err => {
  131. this.$nextTick(() => {
  132. setTimeout(() => {
  133. this.loadMoreLoading = false
  134. }, 500)
  135. })
  136. })
  137. },
  138. setValue(){
  139. typeof this.value === 'object' ? this.remoteMethod(this.value) : this.remoteMethod(this.value, true)
  140. },
  141. clearOptions(){
  142. this.options = []
  143. }
  144. },
  145. }
  146. </script>
  147. <style scoped>
  148. .uploadTip{
  149. width: 100%;
  150. text-align: center;
  151. }
  152. </style>

组件提示 tips:

        由于组件搜索下拉框分页通过 scroll 事件实现的 

        所以远程加载提示涉及到元素隐藏和显示会一直触发 scroll 到底事件;

        需要常驻!!!!

        错误例子: 在 options 底部加载中显示 <div>加载中。。。</div> 接口请求结束在隐藏

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