Vue封装分页下拉选择器的组件

简介: 该组件名为,它将整合Element UI的下拉选择器和分页组件,以实现一个功能丰富的下拉选择器。用户可以通过搜索来过滤选项,并通过分页来浏览结果。

 在Vue项目中,经常需要实现带有分页功能的下拉选择器组件,以满足用户在大量数据中选择项的需求。本文将介绍如何封装一个Vue组件,该组件结合了分页和搜索功能,使得用户可以在大量数据中快速找到并选择所需项。

组件概述

该组件名为SelectWithPage,它将整合Element UI的<el-select>下拉选择器和<el-pagination>分页组件,以实现一个功能丰富的下拉选择器。用户可以通过搜索来过滤选项,并通过分页来浏览结果。

组件结构

SelectWithPage组件主要包括以下几个部分:

  1. 下拉选择框:使用<el-select>展示选项,并支持搜索。
  2. 搜索输入框:用于输入搜索关键词,过滤选项。
  3. 分页组件:使用<el-pagination>进行分页控制。
  4. 数据请求与处理:根据用户的搜索和分页请求,从后端获取数据并更新组件状态。

组件模板

<template>  
  <el-select  
    v-model="selectedValue"  
    filterable  
    remote  
    :remote-method="handleSearch"  
    :loading="loading"  
    style="width: 100%"  
    placeholder="请选择"  
  >  
    <el-option  
      v-for="item in options"  
      :key="item[valueKey]"  
      :label="item[labelKey]"  
      :value="item[valueKey]"  
    ></el-option>  
    <el-option  
      v-if="options.length === 0 && !loading"  
      value=""  
      disabled  
    >  
      无数据  
    </el-option>  
  </el-select>  
  <el-pagination  
    v-if="total > pageSize"  
    @size-change="handleSizeChange"  
    @current-change="handleCurrentChange"  
    :current-page="currentPage"  
    :page-sizes="[10, 20, 30, 40]"  
    :page-size="pageSize"  
    layout="total, sizes, prev, pager, next, jumper"  
    :total="total"  
  ></el-pagination>  
</template>

image.gif

组件脚本

<script>  
export default {  
  name: 'SelectWithPage',  
  props: {  
    valueKey: {  
      type: String,  
      default: 'id'  
    },  
    labelKey: {  
      type: String,  
      default: 'name'  
    },  
    searchKey: {  
      type: String,  
      default: 'search'  
    },  
    otherParams: {  
      type: Object,  
      default: () => ({})  
    },  
    requestUrl: {  
      type: Function,  
      required: true  
    },  
    pageSize: {  
      type: Number,  
      default: 10  
    },  
    value: {  
      default: null  
    }  
  },  
  data() {  
    return {  
      selectedValue: this.value,  
      options: [],  
      loading: false,  
      total: 0,  
      currentPage: 1  
    };  
  },  
  watch: {  
    value(newVal) {  
      this.selectedValue = newVal;  
    },  
    currentPage(newVal) {  
      this.fetchData();  
    },  
    pageSize(newVal) {  
      this.currentPage = 1; // Reset page when page size changes  
      this.fetchData();  
    }  
  },  
  methods: {  
    handleSearch(queryString) {  
      this.otherParams[this.searchKey] = queryString;  
      this.currentPage = 1; // Reset page when searching  
      this.fetchData();  
    },  
    handleSizeChange(val) {  
      this.pageSize = val;  
    },  
    handleCurrentChange(val) {  
      this.currentPage = val;  
    },  
    fetchData() {  
      this.loading = true;  
      this.requestUrl({  
        ...this.otherParams,  
        page: this.currentPage,  
        limit: this.pageSize  
      }).then(res => {  
        this.options = res.data.records;  
        this.total = res.data.total;  
        this.loading = false;  
      }).catch(error => {  
        console.error('Failed to fetch data:', error);  
        this.loading = false;
      });
    },
    // 同步父组件的selectedValue
    syncSelectedValue(newValue) {
        this.$emit('update:select-value', newValue);
    }
 },
 mounted() {
    this.fetchData(); // 初始化时加载数据
 },
 computed: {
    // 计算属性,用于同步父组件的value
    syncValue: {
        get() {
            return this.selectedValue;
        },
        set(value) {
            this.selectedValue = value;
            this.syncSelectedValue(value);
        }
    }
 }
};
</script>

image.gif

组件样式

通常,<el-select><el-pagination>的样式已经足够使用,但如果需要自定义样式,可以在组件的<style>部分进行添加。例如,调整下拉选择框的宽度或分页组件的布局等。

使用组件

在父组件中,你可以这样使用SelectWithPage组件:

<template>  
  <div>  
    <select-with-page  
      :value-key="id"  
      :label-key="name"  
      :search-key="name"  
      :other-params="{ sblx: 'chjc' }"  
      :requestUrl="fetchDeviceData"  
      :select-value.sync="selectedId"  
      style="width: 225px;"  
    />  
  </div>  
</template>  
  
<script>  
import SelectWithPage from './components/SelectWithPage.vue';  
  
export default {  
  components: {  
    SelectWithPage  
  },  
  data() {  
    return {  
      selectedId: null  
    };  
  },  
  methods: {  
    fetchDeviceData(params) {  
      // 发送请求到后端,根据params获取数据  
      // 返回Promise,解析为包含records和total的对象  
      return new Promise((resolve, reject) => {  
        // 模拟异步请求  
        setTimeout(() => {  
          const data = {  
            records: [  
              // 模拟数据  
              { id: 1, name: '名称1' },  
              { id: 2, mane: '名称2' }  
              // ...  
            ],  
            total: 100 // 假设总记录数为100  
          };  
          resolve(data);  
        }, 1000);  
      });  
    }  
  }  
};  
</script>

image.gif

这样,你就成功封装了一个带有分页和搜索功能的下拉选择器组件,并在父组件中进行了使用。通过调整SelectWithPage组件的props和methods,你可以轻松地将其应用到其他类似的场景中。

相关文章
|
2天前
|
缓存 JavaScript
Vue 中 computed 与 method 的区别
【10月更文挑战第15天】computed 和 method 是 Vue 中两个重要的选项,它们在功能和特点上存在着明显的区别。理解并合理运用它们的区别,可以帮助我们构建更高效、更具可维护性的 Vue 应用。在实际开发中,要根据具体情况灵活选择使用,以满足不同的需求。
5 2
|
1天前
|
JavaScript 前端开发 Java
vue2知识点:Vue封装的过度与动画
vue2知识点:Vue封装的过度与动画
7 0
|
2天前
|
JavaScript UED
在 Vue 中使用自定义指令
【10月更文挑战第14天】通过合理地使用自定义指令,可以为 Vue 应用带来更多的灵活性和扩展性,提高开发效率和用户体验。
|
2天前
|
缓存 JavaScript 前端开发
《基础篇第4章:vue2基础》:使用vue脚手架创建项目
《基础篇第4章:vue2基础》:使用vue脚手架创建项目
11 3
|
5天前
|
JavaScript 前端开发 开发者
Vue v-for 进阶指南:in 与 of 的区别及应用场景 | 笔记
Vue.js 中的 v-for 是强大的遍历指令,但其中的 in 和 of 关键字往往被开发者忽视。尽管它们的用法相似,但适用的场景和数据结构却各有不同。本文将详细探讨 v-for 中 in 和 of 的区别、适用场景以及在实际开发中的最佳使用时机。通过理解它们的差异,你将能够编写更加高效、简洁的 Vue.js 代码,灵活应对各种数据结构的遍历需求。
40 6
|
2天前
|
JavaScript 搜索推荐 UED
vue的自定义指令
【10月更文挑战第14天】Vue 自定义指令为我们提供了一种强大的工具,使我们能够更灵活地控制和扩展 Vue 应用的行为。通过合理地使用自定义指令,可以提高开发效率,增强应用的功能和用户体验。
|
3天前
|
JavaScript
|
5天前
|
缓存 JavaScript 前端开发
Vue 中动态导入的注意事项
【10月更文挑战第12天】 在 Vue 项目中,动态导入是一种常用的按需加载模块的技术,可以提升应用性能和效率。本文详细探讨了动态导入的基本原理及注意事项,包括模块路径的正确性、依赖关系、加载时机、错误处理、缓存问题和兼容性等,并通过具体案例分析和解决方案,帮助开发者更好地应用动态导入技术。
|
5天前
|
JavaScript API
vue 批量自动引入并注册组件或路由等等
【10月更文挑战第12天】 vue 批量自动引入并注册组件或路由等等
|
5天前
|
JavaScript 算法 前端开发
深入剖析Vue中v-for的使用及index作为key的弊端
深入剖析Vue中v-for的使用及index作为key的弊端
14 2