当前位置: 首页 > 知识库问答 >
问题:

前端 - 输入框滚动加载与搜索如何进行封装?

乌翰学
2023-09-20

技术 vue2 项目 框架是 antdv 1.7.8
我项目中有很多表单,表单中有很多Select 选择器 选择器的内容是项目中的某一个列表,有很多这种下拉,在不同的界面。
我的问题是,单独的每一个下拉框都可以进行 远程搜索与滚动加载,但是接口是不同的,所以如何进行封住一下写一个,所有的下拉框都能用。
代码demo如此

<template>  <div>    <div>      <a-select style="width: 500px;" v-model="queryParam.industry_id" show-search :filter-option="false"        :default-active-first-option="false" @search="handleSearch" @popupScroll="handlePopupScroll">        <a-select-option v-for="(item, index) in industryList" :key="index" :value="item.industry_id">            {{item.industry_name }}        </a-select-option>      </a-select>    </div>    <div>      <a-select style="width: 500px;" show-search :filter-option="false" :default-active-first-option="false"        @search="handleSearchEnt" v-model="queryParam.ent_id" @popupScroll="handlePopupScrollEnt">        <a-select-option v-for="(item, index) in industryEntList" :key="index" :value="item.ent_id">            {{ item.ent_name}}        </a-select-option>      </a-select>    </div>  </div></template><script>import {  orgSelectAPI,  orgEntSelectAPI,} from "@/api/enterprise.js";import _ from "lodash";export default {  data() {    return {      queryParam: {},      industryList: [], // 产业下拉数据      industryCount: 10, // 产业默认10个      industrySize: 0, // 总条数数据改变时候必须低于这个值      industrySearch: "", // 远程搜索值      industryEntList: [],      entCount: 10,      entSize: 0,      industryEntSearch: "",    };  },  created() {    this.getIndustryList();    this.getIndustryEntList();  },  methods: {    // 产业下拉加载    handlePopupScroll: _.debounce(function (e) {      const { target } = e;      const { scrollTop, scrollHeight, offsetHeight } = target;      if (        this.industryCount <= this.industrySize &&        scrollTop + 2 + offsetHeight >= scrollHeight      ) {        this.industryCount = Math.min(          this.industryCount + 10,          this.industrySize        );        this.getIndustryList(this.industrySearch);      }    }, 500),    // 远程搜索    handleSearch: _.debounce(function (e) {      this.industrySearch = e;      this.getIndustryList(this.industrySearch);    }, 500),    // 获取产业数据    async getIndustryList(e) {      let res = await orgSelectAPI({        page: 1,        rows: e ? 0 : this.industryCount,        name: e,      });      this.industry_id = "";      this.industryList = [];      this.industrySize = res.data.total;      this.industryList = res.data.rows;    },    handleSearchEnt: _.debounce(function (e) {      this.industryEntSearch = e;      this.getIndustryEntList("", e);    }, 500),    handlePopupScrollEnt: _.debounce(function (e) {      const { target } = e;      const { scrollTop, scrollHeight, offsetHeight } = target;      if (        this.entCount <= this.entSize &&        scrollTop + 2 + offsetHeight >= scrollHeight      ) {        this.entCount = Math.min(this.entCount + 10, this.entSize);        this.getIndustryEntList(this.industry_id, this.industryEntSearch);      }    }, 500),    // 获取企业数据    async getIndustryEntList(e, name) {      let res = await orgEntSelectAPI({        page: 1,        rows: this.entCount,        industry_id: e,        name,      });      this.industryEntList = []      this.industryEntList = res.data.rows;      this.entSize = res.data.total;    },  },};</script>

像这种数据我有很多,这种下拉出现一个我就得定义一个 count与 size 还有搜索与滚动的函数,写起来很麻烦,但是接口又不相同,我该如何进行封装一下,让这个东西写一次,后边的都能用

求大佬给个封装思路,接口与数据如何搞

共有2个答案

楮法
2023-09-20

官方示例:
https://3x.ant.design/components/select-cn/

姜良哲
2023-09-20

封装:

<template>  <div>    <a-select       style="width: 500px;"       v-model="selectedValue"       show-search       :filter-option="false"      :default-active-first-option="false"      @search="handleSearch"       @popupScroll="handlePopupScroll"    >      <a-select-option         v-for="(item, index) in itemList"         :key="index"         :value="item[valueKey]">          {{ item[labelKey] }}      </a-select-option>    </a-select>  </div></template><script>import _ from "lodash";export default {  props: {    apiFunction: {      type: Function,      required: true,    },    queryParams: {      type: Object,      default: () => ({}),    },    valueKey: {      type: String,      default: 'id',    },    labelKey: {      type: String,      default: 'name',    },    initialCount: {      type: Number,      default: 10,    },  },  data() {    return {      selectedValue: null,      itemList: [],      itemCount: this.initialCount,      itemSize: 0,      itemSearch: "",    };  },  created() {    this.getList();  },  methods: {    async getList(searchValue = "") {      const response = await this.apiFunction({        ...this.queryParams,        page: 1,        rows: searchValue ? 0 : this.itemCount,        name: searchValue,      });      this.itemList = response.data.rows;      this.itemSize = response.data.total;    },    handleSearch: _.debounce(function(value) {      this.itemSearch = value;      this.getList(this.itemSearch);    }, 500),    handlePopupScroll: _.debounce(function(e) {      const { target } = e;      const { scrollTop, scrollHeight, offsetHeight } = target;      if (        this.itemCount <= this.itemSize &&        scrollTop + 2 + offsetHeight >= scrollHeight      ) {        this.itemCount = Math.min(this.itemCount + 10, this.itemSize);        this.getList(this.itemSearch);      }    }, 500),  },};</script>

用的地方:

<template>  <div>    <CustomSelect       :api-function="orgSelectAPI"       :query-params="{ additionalParam: 'someValue' }"      value-key="industry_id"      label-key="industry_name"    />    <CustomSelect       :api-function="orgEntSelectAPI"       :query-params="{ anotherParam: 'someOtherValue' }"      value-key="ent_id"      label-key="ent_name"    />  </div></template><script>import { orgSelectAPI, orgEntSelectAPI } from "@/api/enterprise.js";import CustomSelect from "@/components/CustomSelect.vue";export default {  components: {    CustomSelect,  },};</script>
 类似资料:
  • 搜索输入框是一个新兴的html元素,外观为圆角,当你输入文字后右边会出现一个叉的图标,点击则会清除你输入的内容。给input增加type="search" 属性来定义 注意要把label的for属性设为input的id值,使他们能够在语义上相关联,并且要用div容器包裹它们,并给他设定data-role="fieldcontain"属性 HTML 代码: <div data-role="field

  • 本文向大家介绍如何对淘宝搜索框进行测试相关面试题,主要包含被问及如何对淘宝搜索框进行测试时的应答技巧和注意事项,需要的朋友参考一下 参考回答: 一, 功能测试 输入关键字,查看: 返回结果是否准确,返回的文本长度需限制 1.1输入可查到结果的正常关键字、词、语句,检索到的内容、链接正确性; 1.2输入不可查到结果的关键字、词、语句; 1.3输入一些特殊的内容,如空、特殊符、标点符、极限值等,可引入

  • 我有搜索输入以及下拉存储为li元素。如何向搜索标签添加自动完成功能,从li标签获取数据,并在搜索输入中显示相应的结果。 自动完成程序应从ul li标签中提取内容并执行操作。可以通过li搜索输入标签完成吗?

  • 问题内容: 有一个网格,其中有1000行,其中有一个名为Username(具有不同值)的列。 网格每个视图仅显示20行,其他行仅在滚动时才会 加载 (ajax)。 因此,如何搜索网格中的特定用户名,因为只有元素被滚动加载。 请问方法的帮助?还是我需要使用直到找到搜索到的物品? 问题答案: 首先,我很抱歉,因为我以前从未在网格上工作过。我认为这将是一个框架,并且使用 JavascriptExecut

  • 嗨,伙计们,我是JSON的新手,所以我想创建一个“城市”搜索栏,以便用户可以使用openweathermap API检索该特定城市的天气。 这是您应该如何请求它,但我希望以这样的方式设置“q”,即当在搜索栏中正确键入城市时,它会自动检索该城市的数据。"http://api.openweathermap.org/data/2.5/weather?q=Toronto,加利福尼亚州 谢谢你的帮助。

  • 通过扫码枪扫码自动输入id到input框,自动搜索id输出列表数据,列表数据是通过调起接口触发,ui是iview input框,现在的代码是 用过@keyup.enter.native,都没效果 这怎么弄咧,大佬们