赞
踩
新建指令 :
1、 在 src 目录下 新建文件夹 directive / loadmore
2、在 loadmore 文价夹下新建 elSelectLoadmore.js 和 index.js 文件:
elSelectLoadmore.js
- export default {
- inserted(el, binding, vnode) {
- const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
- SELECTWRAP_DOM.addEventListener('scroll', function() {
- const condition = Math.floor(this.scrollHeight - this.scrollTop) <= this.clientHeight;
- if (condition) {
- binding.value();
- }
- });
- }
- };
index.js
- import elSelectLoadmore from './elSelectLoadmore';
- const install = function(Vue) {
- Vue.directive('el-select-loadmore', elSelectLoadmore);
- };
- if (window.Vue) {
- window['el-select-loadmore'] = elSelectLoadmore
- Vue.use(install); // eslint-disable-line
- }
- export default install;
3、在main 文件中注册该指令
- import elSelectLoadmore from './directive/loadmore'
-
-
- Vue.use(elSelectLoadmore)
4、 基于 el-select 封装 懒加载 远程搜索框
remoteSelect.vue
- <template>
- <el-select
- ref="remoteSelect"
- :disabled="disabled"
- style="width:100%"
- v-model="remoteCode"
- filterable
- remote
- clearable
- reserve-keyword
- placeholder="请输入软件id / 软件名称"
- :remote-method="remoteMethod"
- v-el-select-loadmore="loadmore"
- :loading="loading"
- @input="input"
- :multiple="multiple"
- >
- <el-option
- v-for="item in options"
- :key="item.id"
- :label="item.id + ' ' +item.name"
- :value="item.id"
- >
- </el-option>
- <div class="uploadTip"
- v-if="loadMoreLoading && !loadNoMore"
- >数据加载中......</div>
- <div
- class="uploadTip"
- v-if="loadNoMore"
- >暂无更多数据</div>
- </el-select>
- </template>
- <script>
- import { getSoftwareForRef } from "@/api/softwar/manage";
- export default {
- name:'remote-select',
- props:{
- value:{
- type:[Number, String, Array],
- default:undefined
- },
- disabled:{
- type: Boolean,
- default:false
- },
- multiple:{
- type:Boolean,
- default:false
- },
- status:{
- type:Number,
- default:undefined
- }
- },
- data(){
- return {
- remoteQuery:{
- pageNum:1,
- pageSize:10,
- status:this.status //接口状态参数
- },
- options:[],
- loading:false, // 组件搜索加载中参数
- loadMoreLoading:false, // 翻页加载中参数
- remoteCode:undefined, // select value
- loadNoMore:false // 没有更多的状态
- }
- },
- mounted(){
- },
- watch:{
- value: {
- handler(data) {
- this.remoteCode = data
- },
- immediate: true
- },
- },
- methods: {
- input(val){
- this.$emit('input',val)
- const index = this.options.findIndex(i => i.id === val)
- this.$emit('inputAll', index === -1 ? val : this.options[index])
- },
- remoteMethod(query, review) {
- this.loading = true
- this.loadNoMore = false
- this.remoteQuery.pageNum = 1
- if(typeof query === 'object'){
- this.remoteQuery.swIds = query
- }else{
- this.remoteQuery.param = query
- delete this.remoteQuery.swIds
- }
- this.remoteQuery.status = this.status
- getSoftwareForRef(this.remoteQuery).then(res => {
- this.options = res.data
- this.loading = false;
- if(typeof review === 'boolean' && review){
- let index = this.options.findIndex(i => i.id === this.value)
- if(index !== -1){
- this.remoteCode = this.value + ' ' + this.options[index].name
- }else{
- this.remoteCode = this.value
- }
- }
- })
- },
- loadmore(){
- if(this.loadMoreLoading){
- return
- }
- var remoteQuery = {...this.remoteQuery};
- remoteQuery.pageNum++;
- this.loadMoreLoading = true
- getSoftwareForRef(remoteQuery).then(res => {
- if(res.code === 0 && typeof res.data === 'object' && res.data.length){
- this.options = this.options.concat(res.data)
- this.remoteQuery.pageNum++
- }
- if(res.code === 0 && typeof res.data === 'object' && res.data.length === 0){
- this.loadNoMore = true;
- }
- this.$nextTick(() => {
- setTimeout(() => {
- this.loadMoreLoading = false
- }, 500)
- })
- }).catch(err => {
- this.$nextTick(() => {
- setTimeout(() => {
- this.loadMoreLoading = false
- }, 500)
- })
- })
-
- },
- setValue(){
- typeof this.value === 'object' ? this.remoteMethod(this.value) : this.remoteMethod(this.value, true)
- },
- clearOptions(){
- this.options = []
- }
- },
-
- }
-
- </script>
- <style scoped>
- .uploadTip{
- width: 100%;
- text-align: center;
- }
- </style>
组件提示 tips:
由于组件搜索下拉框分页通过 scroll 事件实现的
所以远程加载提示涉及到元素隐藏和显示会一直触发 scroll 到底事件;
需要常驻!!!!
错误例子: 在 options 底部加载中显示 <div>加载中。。。</div> 接口请求结束在隐藏
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。