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

el-virtual-select

v0.12.0

Published

a VUE virtual select ui component, which extends element ui

Downloads

72

Readme

el-virtual-select

一个基于vue-virtual-scroller 和 element ui 选择框el-select的具有虚拟滚动的下拉选择组件。

具体用法

import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

导入组件及样式

属性、事件

| 参数 | 说明 | 默认值 | | :-------------------- | :------------------------------------- | :----- | | options | 需要渲染的下拉数据 | [] | | valueKey | 数据 value 键值 | value | | labelKey | 数据 label 键值 | label | | filterable | 是否过滤 | true | | clearable | 是否展示删除 | true | | minItemSize | 每一项的最小高度 | 34 | | beautifyScrollerStyle | 是否启用美化后的滚动样式 | false | | dropdownItemsCount | 下拉视图中展示的个数 | 6 | | listClass | 可传递给 vue-virtual-scroller 组件属性 | '' | | itemClass | 可传递给 vue-virtual-scroller 组件属性 | '' | | listTag | 可传递给 vue-virtual-scroller 组件属性 | 'div' | | itemTag | 可传递给 vue-virtual-scroller 组件属性 | 'div' |

el-select 提供的其他属性,事件大多都支持。

例子

demo 源码

以下例子基于 vue 2.7x 举例

基本用法

<template>
  <div>
    <ElVirtualSelect v-model="value" :options="list" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

const list = Array.from({ length: 1000 }).map((i, index) => ({
  label: 'label' + index,
  value: 'value' + index
}))
const value = ref('')
</script>

映射 value、label 的键值

<template>
  <div>
    <ElVirtualSelect
      v-model="value"
      :options="list"
      value-key="code"
      label-key="name"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

const list = Array.from({ length: 1000 }).map((i, index) => ({
  name: 'name' + index,
  code: 'code' + index
}))
const value = ref('')
</script>

自定义 ElOption label 的插槽

<template>
  <div>
    <ElVirtualSelect v-model="value" :options="list">
      <template #label="{ item }">
        <span>{{ item.value }} &nbsp;&nbsp; {{ item.label }}</span>
      </template>
    </ElVirtualSelect>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

const list = Array.from({ length: 1000 }).map((i, index) => ({
  label: 'label' + index,
  value: 'value' + index
}))
const value = ref('')
</script>

美化滚动条

<template>
  <div>
    <ElVirtualSelect
      v-model="value"
      :options="list"
      beautify-scroller-style
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

const list = Array.from({ length: 1000 }).map((i, index) => ({
  label: 'label' + index,
  value: 'value' + index
}))
const value = ref('')
</script>

远程搜索

<template>
  <div>
    <ElVirtualSelect
      v-model="value"
      :options="list"
      remote
      :remote-method="handleRemoteSearch"
      @change="handleChange"
      @focus="handleFocus"
    ></ElVirtualSelect>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import ElVirtualSelect from 'el-virtual-select'
import 'el-virtual-select/dist/style.css'

const rawList = Array.from({ length: 1000 }).map((i, index) => ({
  label: 'label' + index,
  value: 'value' + index
}))
const list = ref(rawList)
let cachedLastQuery = ''
let currentLabel = ''
const value = ref('')
const handleRemoteSearch = val => {
  cachedLastQuery = val
  list.value = rawList.filter(i => i.label.includes(val))
}
const handleFocus = () => {
  if (!value.value) {
    list.value = rawList
  }

  // 处理一些特殊情况。比如已经选择了一项,再次聚焦,搜索,但是没选。
  // 然后聚焦选择时,可以显示当前选中的value对应的下拉列表
  if (value.value && !currentLabel.includes(cachedLastQuery)) {
    handleRemoteSearch('')
  }
}
const handleChange = val => {
  console.log('current value:', val)
  currentLabel = list.value.find(i => i.value === val)?.label ?? ''
  console.log('current label:', currentLabel)
}
const reset = () => {
  value.value = ''
}
</script>