当前位置:首页 > 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' // 示例返回值
  }
}

集成第三方拼音库

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

安装pinyin-pro:

npm install pinyin-pro

在组件中使用:

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)
    }
  }
}

完整组件示例

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

名字筛选用vue实现

<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 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现级联

vue实现级联

Vue 实现级联选择器的方法 级联选择器(Cascader)是常见的表单组件,用于处理多层级联动数据选择(如省市区选择)。以下是几种实现方式: 基于 Element UI 的 el-cascader…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue实现开关

vue实现开关

Vue 实现开关组件的方法 使用原生HTML和Vue数据绑定 通过v-model绑定复选框的checked状态,结合CSS样式实现开关效果: <template> <label…

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…