当前位置:首页 > VUE

名字筛选用vue实现

2026-02-23 21:14:12VUE

实现名字筛选的Vue组件

创建一个Vue组件来实现名字筛选功能,需要包含输入框和筛选结果展示。以下是一个基本实现方案:

<template>
  <div class="name-filter">
    <input 
      v-model="searchQuery" 
      placeholder="输入名字进行筛选" 
      class="filter-input"
    />
    <ul class="name-list">
      <li 
        v-for="name in filteredNames" 
        :key="name"
        class="name-item"
      >
        {{ name }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      names: ['张三', '李四', '王五', '赵六', '钱七']
    }
  },
  computed: {
    filteredNames() {
      return this.names.filter(name => 
        name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  }
}
</script>

<style>
.filter-input {
  padding: 8px;
  width: 200px;
  margin-bottom: 10px;
}
.name-list {
  list-style: none;
  padding: 0;
}
.name-item {
  padding: 5px;
  border-bottom: 1px solid #eee;
}
</style>

优化筛选功能

对于更复杂的筛选需求,可以添加以下改进:

computed: {
  filteredNames() {
    const query = this.searchQuery.trim().toLowerCase()
    if (!query) return this.names

    return this.names.filter(name => {
      const lowerName = name.toLowerCase()
      // 支持拼音首字母匹配
      const pinyinMatch = this.getPinyinInitials(name).toLowerCase().includes(query)
      // 支持完整拼音匹配
      const fullPinyinMatch = this.getFullPinyin(name).toLowerCase().includes(query)

      return lowerName.includes(query) || pinyinMatch || fullPinyinMatch
    })
  }
},
methods: {
  getPinyinInitials(name) {
    // 实现汉字转拼音首字母逻辑
    // 可以使用第三方库如pinyin-pro
    return 'zslwq' // 示例返回值
  },
  getFullPinyin(name) {
    // 实现汉字转完整拼音逻辑
    return 'zhangsan lisi wangwu zhaoliu qianqi' // 示例返回值
  }
}

集成第三方拼音库

要实现更准确的汉字拼音转换,可以集成第三方库:

名字筛选用vue实现

安装pinyin-pro:

npm install pinyin-pro

在组件中使用:

名字筛选用vue实现

import { pinyin } from 'pinyin-pro'

methods: {
  getPinyinInitials(name) {
    return pinyin(name, { pattern: 'first', toneType: 'none' })
  },
  getFullPinyin(name) {
    return pinyin(name, { toneType: 'none' })
  }
}

添加性能优化

对于大型名字列表,可以添加防抖和虚拟滚动:

import { debounce } from 'lodash'

export default {
  data() {
    return {
      searchQuery: '',
      names: [], // 大型名字数组
      displayNames: []
    }
  },
  created() {
    this.debouncedFilter = debounce(this.updateDisplayNames, 300)
  },
  watch: {
    filteredNames: {
      handler() {
        this.debouncedFilter()
      },
      immediate: true
    }
  },
  methods: {
    updateDisplayNames() {
      // 实现虚拟滚动逻辑,只显示可见区域的名字
      this.displayNames = this.filteredNames.slice(0, 50)
    }
  }
}

完整组件示例

结合上述所有改进的完整组件示例:

<template>
  <div class="name-filter-container">
    <div class="search-box">
      <input
        v-model="searchQuery"
        placeholder="输入名字/拼音筛选"
        class="search-input"
        @input="handleInput"
      />
      <span class="result-count">{{ filteredNames.length }}个结果</span>
    </div>

    <div class="name-list-container">
      <virtual-list
        :size="40"
        :remain="10"
        :items="filteredNames"
      >
        <template v-slot="{ item }">
          <div class="name-item">
            {{ item }}
            <span class="pinyin-hint">({{ getFullPinyin(item) }})</span>
          </div>
        </template>
      </virtual-list>
    </div>
  </div>
</template>

<script>
import { pinyin } from 'pinyin-pro'
import { debounce } from 'lodash'
import VirtualList from 'vue-virtual-scroll-list'

export default {
  components: {
    VirtualList
  },
  data() {
    return {
      searchQuery: '',
      names: [] // 从API或其他来源加载
    }
  },
  computed: {
    filteredNames() {
      const query = this.searchQuery.trim().toLowerCase()
      if (!query) return this.names

      return this.names.filter(name => {
        const lowerName = name.toLowerCase()
        const initials = this.getPinyinInitials(name).toLowerCase()
        const fullPinyin = this.getFullPinyin(name).toLowerCase()

        return lowerName.includes(query) || 
               initials.includes(query) || 
               fullPinyin.includes(query)
      })
    }
  },
  methods: {
    getPinyinInitials(name) {
      return pinyin(name, { pattern: 'first', toneType: 'none' })
    },
    getFullPinyin(name) {
      return pinyin(name, { toneType: 'none' })
    },
    handleInput: debounce(function() {
      // 处理输入变化
    }, 300)
  }
}
</script>

<style>
/* 添加适当的样式 */
</style>

这个实现包含了基本的筛选功能、拼音支持、性能优化和良好的用户体验。可以根据实际需求调整和扩展功能。

标签: 名字vue
分享给朋友:

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: np…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生…

vue实现公式

vue实现公式

在Vue中实现公式展示或计算可以通过多种方式实现,以下为具体方法: 使用模板插值与计算属性 若公式较简单,可直接在模板中使用插值表达式或计算属性。例如计算圆的面积: <template&…

vue实现通知

vue实现通知

Vue 实现通知功能的方法 使用 Vue 实现通知功能可以通过多种方式实现,以下是一些常见的方法: 使用 Vue 的全局事件总线 在 Vue 中可以通过事件总线实现通知功能。创建一个全局事件总线,…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或…

vue实现排序

vue实现排序

Vue 实现排序的方法 在 Vue 中实现排序可以通过多种方式完成,以下是一些常见的方法: 使用计算属性排序数组 计算属性非常适合对数据进行排序,因为它会在依赖的数据变化时自动更新。以下是一个示例…