Skip to content

API 数据选择器组件

概述

ZqApiSelect 是一个通用的 API 数据选择器组件,通过调用后端 API 获取数据列表,支持分页加载、搜索过滤、单选和多选。

组件路径web/apps/web-ele/src/components/zq-form/zq-api-selector/

组件名称ZqApiSelect

功能特性

  • 通过 API 动态加载数据
  • 分页加载(触底加载更多)
  • 搜索过滤
  • 单选/多选模式
  • 支持数据回显(通过 ID 加载)
  • 自定义值字段和标签字段
  • 额外信息显示

Props

属性类型默认值说明
modelValuestring | string[]-选中的值
apiFunction必填获取列表数据的 API 函数
apiByIdsFunction-根据 ID 获取数据的 API 函数(用于回显)
multiplebooleanfalse是否多选
placeholderstring'请选择'占位符
disabledbooleanfalse是否禁用
clearablebooleantrue是否可清除
filterablebooleantrue是否可搜索
valueFieldstring'id'值字段名
labelFieldstring'name'标签字段名
extraFieldstring-额外信息字段名
dialogTitlestring'请选择'弹窗标题
dialogWidthstring'45%'弹窗宽度
pageSizenumber20每页数量

API 函数签名

typescript
// 列表 API
interface ListApiParams {
  page: number;
  pageSize: number;
  keyword?: string;
}

interface ListApiResult {
  items: any[];
  total: number;
}

type ListApi = (params: ListApiParams) => Promise<ListApiResult>;

// 根据 ID 获取 API
type ApiByIds = (ids: string[]) => Promise<any[]>;

Events

事件参数说明
update:modelValuestring | string[]值更新
changestring | string[]选中值变化

Expose 方法

方法参数说明
openModal-打开选择弹窗

使用示例

基本用法

vue
<template>
  <ZqApiSelect
    v-model="userId"
    :api="getUserList"
    :api-by-ids="getUsersByIds"
    value-field="id"
    label-field="name"
    placeholder="请选择用户"
  />
</template>

<script setup>
import { ref } from 'vue';
import { getUserListApi, getUsersByIdsApi } from '@/api/user';

const userId = ref('');

const getUserList = async (params) => {
  const result = await getUserListApi(params);
  return { items: result.data, total: result.total };
};

const getUsersByIds = async (ids) => {
  return await getUsersByIdsApi(ids);
};
</script>

多选模式

vue
<template>
  <ZqApiSelect
    v-model="userIds"
    :api="getUserList"
    :api-by-ids="getUsersByIds"
    :multiple="true"
    dialog-title="选择用户"
  />
</template>

<script setup>
import { ref } from 'vue';

const userIds = ref([]);
</script>

显示额外信息

vue
<template>
  <ZqApiSelect
    v-model="productId"
    :api="getProductList"
    value-field="id"
    label-field="name"
    extra-field="price"
    placeholder="请选择产品"
  />
</template>

与表单设计器集成

在表单设计器中,该组件作为 api-selector 类型的表单项使用:

json
{
  "type": "api-selector",
  "field": "user_id",
  "label": "用户",
  "props": {
    "apiUrl": "/api/users",
    "apiByIdsUrl": "/api/users/batch",
    "valueField": "id",
    "labelField": "name",
    "multiple": false,
    "pageSize": 20
  }
}

典型应用场景

  • 用户选择:从用户列表中选择用户
  • 产品选择:从产品列表中选择产品
  • 数据关联:选择关联的数据记录
  • 动态数据:需要从 API 动态加载的选择场景

Released under the MIT License.