npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@corgicoding-el/remote-select

v2.1.1

Published

基于element-plus的远程下拉组件

Downloads

11

Readme

@corgicoding-el/remote-select

@corgicoding/web-quick-start 工程模板设定的远程下拉组件

绑定依赖

  1. @corgicoding/web-types
    • NormalizedError: 统一错误返回
    • NormalizedResponse: 统一接口返回
  2. @corgicoding/axios-hook
    • useService: 获取当前 axios 实例

如何使用

安装工程到本地,并按需使用或全局使用

前置依赖

  • element-plus
  • axios
  • @vueuse/core
  • vue (3.x)

如果没有以上依赖,工程执行以下命令进行安装

pnpm install element-plus vue @vueuse/core axios -S

安装

使用 pnpm 下载

pnpm install @corgicoding-el/remote-select -S

使用

假设有个接口 /test/getusers, 返回的内容为包含 usernameuserId 的对象数组

<script setup>
import { RemoteSelect } from '@corgicoding-el/remote-select';
// import '@corgicoding-el/remote-select/style.css';

const selectValue = ref();
</script>

<template>
  <RemoteSelect
    v-model="selectValue"
    url="/test/getusers"
    option-label="username"
    option-value="userId"
  ></RemoteSelect>
</template>

props入参

/**
 * @description RemoteSelect的组件入参
 */
export interface RemoteSelectProps {
  modelValue: Array<string> | string | number | undefined; // 绑定值
  url: string; // 选项获取地址
  optionLabel: string; // 选项label选取key
  optionValue: string; // 选项value选取value
  isList?: boolean; // 是否列表获取接口
  multiple?: boolean; // 是否多选
  isString?: boolean; // 是否绑定值为字符串的多选
  query?: string; // 请求额外参数
  textType?: boolean; // 是否为文本
  eventOptions?: any; // 组件的原生事件
  resultKey?: string;
  strict?: boolean; // 严格模式,初始化去除未含有的值
  selectFirstOption?: boolean; // 选择第一项
}

/**
 * @description RemoteTreeSelect的组件入参
 */
export interface RemoteTreeSelectProps {
  modelValue: Array<string> | string | number | undefined; // 绑定值
  url: string; // 选项获取地址
  optionLabel: string; // 选项label选取key
  optionValue: string; // 选项value选取value
  optionChildren?: string;
  resultKey?: string;
  isList?: boolean; // 是否列表获取接口
  multiple?: boolean; // 是否多选
  isString?: boolean; // 是否绑定值为字符串的多选
  query?: string; // 请求额外参数
  textType?: boolean;
  eventOptions?: any;
  strict?: boolean; // 严格模式,初始化去除未含有的值
  selectFirstOption?: boolean; // 选择第一项
}

emits 事件

const Emits = defineEmits(['update:modelValue', 'change', 'request-done']);

暴露参数

defineExpose({
  loadOptions: getRemoteOptions,
  remoteOptions
});

按需使用

完整的案例如下

<script setup>
import { RemoteSelect } from '@corgicoding-el/remote-select';
import '@corgicoding-el/remote-select/style.css';

const selectValue = ref('');
const remoteSelectRef = ref<InstanceType<typeof RemoteSelect>>()

const onSelectChange = () => {
  console.log('change');
};

const selectOptions = ref<any>([]);
const onSelectRequestDone = (data) => {
    // remoteSelectRef.value?.remoteOptions
  console.log('options', data);
}
</script>

<template>
  <RemoteSelect
    ref="remoteSelectRef"
    v-model="selectValue"
    url="/test/getuserlist"
    query="?page=1&pageSize=100"
    option-label="username"
    option-value="userId"
    :is-list="false"
    result-key="records"
    is-string
    mutiple
    :clearable="false"
    :text-type="false"
    @change="onSelectChange"
    @request-done="onSelectRequestDone"
  ></RemoteSelect>
</template>

全局引入

main.ts 引入

import ElRemoteSelect from '@corgicoding-el/remote-select';
import '@corgicoding-el/remote-select/style.css';

app.use(ElRemoteSelect);

场景说明

列表使用

由于列表接口一般为 records 包在 res.data.result 内,且传参为 pageSize=-1 时,能返回所有列表数据。

RemoteSelect 可以使用 isList 直接获取到作为列表接口的返回

<script setup>
import { RemoteSelect } from '@corgicoding-el/remote-select';
// import '@corgicoding-el/remote-select/style.css';

const selectValue = ref < any > [];
</script>

<template>
  <RemoteSelect
    v-model="selectValue"
    url="/test/getuserlist"
    option-label="username"
    option-value="userId"
    :is-list="true"
  ></RemoteSelect>
</template>

其他 result 字段

也可以使用 result-key 直接选中返回的某个字段,配合 query 参数

<script setup>
import { RemoteSelect } from '@corgicoding-el/remote-select';
// import '@corgicoding-el/remote-select/style.css';

const selectValue = ref < any > [];
</script>

<template>
  <RemoteSelect
    v-model="selectValue"
    url="/test/getuserlist"
    query="?page=1&pageSize=-1"
    option-label="username"
    option-value="userId"
    result-key="records"
  ></RemoteSelect>
</template>

多选

multiple 设置为 true

逗号数组

isString 设置为 true,组件将自动将绑定的值转化为 , 分割的字符串

文本显示

为满足 @corgicoding-el/data-form-grid 功能的详情功能,将 textType 设置为 true 则自动转化当前模式为文本模式,非下拉模式

原生 element-plus 使用

文档: https://element-plus.org/zh-CN/component/select.html

  • 所有 element-plusselect 参数属性直接直接绑定在组件元素上
  • 所有 element-plusselect 事件可以通用 eventOptions 进行绑定
<RemoteSelect
  v-model="selectValue"
  url="/test/getuserlist"
  query="?page=1&pageSize=-1"
  option-label="username"
  option-value="userId"
  result-key="records"
  :event-options="{
    change: () => {}
  }"
></RemoteSelect>