当前位置:   article > 正文

element-ui-解决下拉框数据量过多问题(selectLoadmore)_element select 数据很多

element select 数据很多
vue-virtual-scroll-list与selectLoadmore对比:
vue-virtual-scroll-list:一次性返回所有数据。即数据量适中,几千到一两万条,可一次性被后台接口返回的情况下,建议使用vue-virtual-scroll-list。
selectLoadmore:可懒加载下拉框,数据量超级大,几万甚至十几万条以上,后台无法一次性返回,只能分页加载的情况下,建议使用selectLoadmore。
一、components/selectLoadmore/index.vue
  1. <template>
  2. <el-select class="ba-content" :popper-append-to-body="false"
  3. :value-key="valueName"
  4. v-model="selectedOptions"
  5. v-el-select-loadmore="loadmore"
  6. @change="change"
  7. :remote-method="remoteMethodThrottle"
  8. :loading="loading"
  9. @visible-change="visibleChange" clearable filterable remote
  10. :placeholder="!disabled?'请输入关键词':'——'" :disabled="disabled">
  11. <el-option v-for="item in showList" :key="item[valueName]" :label="item[valueName]" :value="item"></el-option>
  12. </el-select>
  13. </template>
  14. <script>
  15. export default {
  16. name: 'el-select-loadmore',
  17. props: {
  18. value: {
  19. type: String
  20. },
  21. showList: {
  22. type: Array
  23. },
  24. placeholder: {
  25. type: String
  26. },
  27. disabled: {
  28. type: Boolean,
  29. default: false
  30. },
  31. //相同名称的label和value不需传参给子组件,采用默认值
  32. valueName: {
  33. type: String,
  34. default: 'label'
  35. },
  36. labelName: {
  37. type: String,
  38. default: 'value'
  39. }
  40. },
  41. data() {
  42. return {
  43. //data重新定义变量接受组件v-model(value:名称必须为value)值,不改变props接收的值
  44. selectedOptions: this.value,
  45. loading: false
  46. }
  47. },
  48. //监听值的变化
  49. watch: {
  50. value(newVal) {
  51. this.selectedOptions = newVal
  52. }
  53. //若选择器的值发生改变,则传参给父组件
  54. //this.$emit(‘input’,val),在父组件直接用v-model绑定,则可以直接获取到
  55. // selectedOptions(newVal, oldVal) {
  56. // this.$emit('input', this.selectedOptions)
  57. // }
  58. },
  59. // 自定义指令(和method同级)
  60. directives: {
  61. // 下拉框懒加载(el-select-loadmore是自定义的指令,使用时前面加v-)
  62. 'el-select-loadmore': {
  63. // el:使用自定义指令的元素(下拉框),
  64. bind(el, binding) {
  65. // 下拉框下拉的框
  66. const SELECTWRAP_DOM = el.querySelector(
  67. '.el-select-dropdown .el-select-dropdown__wrap'
  68. )
  69. // 增加滚动监听,
  70. SELECTWRAP_DOM.addEventListener('scroll', function () {
  71. // scrollHeight:当前所有选项的高度
  72. // scrollTop:滚动的距离
  73. // clientHeight:下拉框的高度
  74. // const condition =
  75. // this.scrollHeight - this.scrollTop == this.clientHeight ||
  76. // this.scrollHeight - this.scrollTop == this.clientHeight + 1
  77. const condition =
  78. this.scrollHeight - this.scrollTop <= this.clientHeight + 40
  79. // 当滚动条滚动到最底下的时候执行接口加载下一页
  80. if (condition) {
  81. binding.value()
  82. }
  83. })
  84. }
  85. }
  86. },
  87. computed: {
  88. // 防抖
  89. remoteMethodThrottle() {
  90. let time = null
  91. return (param) => {
  92. if (time) {
  93. clearTimeout(time)
  94. }
  95. time = setTimeout(() => {
  96. // 搜索方法
  97. this.remoteMethod(param)
  98. clearTimeout(time)
  99. }, 1000)
  100. }
  101. }
  102. },
  103. methods: {
  104. // 下拉框出现、隐藏
  105. visibleChange(val) {
  106. this.$emit('visibleChange', val)
  107. },
  108. // 自定义指令的方法:下拉框滚动到底懒加载
  109. loadmore() {
  110. this.$emit('loadmore')
  111. },
  112. // 远程搜索物料类别
  113. remoteMethod(query) {
  114. this.$emit('remoteMethod', query)
  115. },
  116. // 选择器的值发生改变
  117. change(item) {
  118. this.$emit('change', item)
  119. }
  120. }
  121. }
  122. </script>
  123. <style lang="scss" scoped>
  124. </style>
二、调用页面
  1. <template>
  2. <selectLoadmore :disabled="disabled" :value="base.otpTcmDise" :showList="selectOptions" valueName="diagName" @loadmore="loadmore" @remoteMethod="remoteMethod" @visibleChange="visibleChange" @change="(val) => change(val, 'tcmDiseCode')"></selectLoadmore>
  3. </template>
  4. <script>
  5. data() {
  6. return {
  7. // 搜索,远程,懒加载下拉相关数据
  8. querys: '', // 远程搜索输入的内容
  9. selectLoading: false, //select加载
  10. stopLoading: false, // 最后一次加载之后,不再加载
  11. pageData: {
  12. // 懒加载相关参数,这里代表从第一条数据开始加载,一次加载十项
  13. current: 1,
  14. size: 10
  15. },
  16. selectOptions: [],
  17. }
  18. },
  19. methods: {
  20. // 下拉框出现、隐藏
  21. visibleChange(val) {
  22. if (!val) {
  23. //隐藏
  24. // 重置懒加载
  25. this.stopLoading = false
  26. // 重置数据
  27. this.querys = null
  28. this.pageData = {
  29. // 懒加载相关参数,这里代表从第一条数据开始加载,一次加载十项
  30. current: 1,
  31. size: 10
  32. }
  33. this.selectOptions = []
  34. }
  35. },
  36. // 自定义指令的方法:下拉框滚动到底懒加载
  37. loadmore() {
  38. if (!this.stopLoading) {
  39. this.pageData.current++ // 搜索下一页
  40. this.getTypeOption(this.pageData) //调用接口获取下拉框数据
  41. }
  42. },
  43. // 远程搜索物料类别
  44. remoteMethod(query) {
  45. this.querys = query // 保存搜索内容
  46. // 重置懒加载
  47. this.stopLoading = false
  48. this.pageData = {
  49. current: 1,
  50. size: 10
  51. }
  52. let params = this.pageData
  53. params.query = query
  54. // 调用接口
  55. this.selectLoading = true
  56. _interface(params).then((res) => {
  57. if (res.code == 200) {
  58. this.selectOptions = res.result.records
  59. } else {
  60. this.$message({ type: 'error', message: '错误返回提示' })
  61. }
  62. this.selectLoading = false
  63. })
  64. },
  65. // 获取物料类别下拉内容
  66. getTypeOption(params) {
  67. params.query = this.querys
  68. this.selectLoading = true
  69. _interface(params).then((res) => {
  70. if (res.code == 200) {
  71. // 如果某次返回值是[],表示已经加载完了,不必再加载
  72. if (res.result.records.length == 0) {
  73. this.stopLoading = true
  74. return
  75. }
  76. if (params.current == 1) {
  77. // 保存数据
  78. this.selectOptions = res.result.records
  79. } else {
  80. this.selectOptions = [...this.selectOptions, ...res.result.records]
  81. }
  82. } else {
  83. this.$message({ type: 'error', message: '错误返回提示' })
  84. }
  85. this.selectLoading = false
  86. })
  87. },
  88. // 西医
  89. change(obj, key) {
  90. this.base[key] = obj.diagCode
  91. }
  92. }
  93. </script>

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

闽ICP备14008679号