当前位置:首页 > VUE

vue实现城市索引列表

2026-01-20 15:39:19VUE

Vue 实现城市索引列表

数据结构准备

城市数据通常以数组形式存储,每个城市对象包含名称和拼音首字母。示例数据结构如下:

const cities = [
  { name: '北京', pinyin: 'bj' },
  { name: '上海', pinyin: 'sh' },
  { name: '广州', pinyin: 'gz' },
  // 更多城市...
]

按字母分组

使用 reduce 方法将城市按首字母分组:

const groupedCities = cities.reduce((acc, city) => {
  const initial = city.pinyin[0].toUpperCase()
  if (!acc[initial]) acc[initial] = []
  acc[initial].push(city)
  return acc
}, {})

模板结构

在 Vue 模板中创建左右布局,左侧为城市列表,右侧为字母索引栏:

<div class="city-container">
  <div class="city-list">
    <div v-for="(group, initial) in groupedCities" :key="initial">
      <h3>{{ initial }}</h3>
      <div v-for="city in group" :key="city.name" @click="selectCity(city)">
        {{ city.name }}
      </div>
    </div>
  </div>

  <div class="index-bar" @touchstart="onTouchStart" @touchmove="onTouchMove">
    <div v-for="initial in initials" :key="initial" @click="scrollTo(initial)">
      {{ initial }}
    </div>
  </div>
</div>

索引栏交互

实现索引栏的触摸事件处理:

methods: {
  scrollTo(initial) {
    const element = document.querySelector(`h3:contains('${initial}')`)
    if (element) element.scrollIntoView()
  },

  onTouchStart(e) {
    this.handleTouch(e)
  },

  onTouchMove(e) {
    this.handleTouch(e)
  },

  handleTouch(e) {
    const indexHeight = e.currentTarget.offsetHeight
    const itemHeight = indexHeight / this.initials.length
    const touchY = e.touches[0].clientY - e.currentTarget.getBoundingClientRect().top
    const index = Math.floor(touchY / itemHeight)
    if (index >= 0 && index < this.initials.length) {
      this.scrollTo(this.initials[index])
    }
  }
}

样式优化

添加必要的基础样式确保布局合理:

.city-container {
  display: flex;
  height: 100vh;
}

.city-list {
  flex: 1;
  overflow-y: auto;
}

.index-bar {
  width: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #f5f5f5;
}

性能优化

对于大数据量情况,建议:

vue实现城市索引列表

  • 使用虚拟滚动技术
  • 添加防抖处理滚动事件
  • 考虑使用 Web Worker 处理数据分组

完整组件示例

export default {
  data() {
    return {
      cities: [...], // 城市数据
      groupedCities: {},
      initials: []
    }
  },
  created() {
    this.groupCities()
  },
  methods: {
    groupCities() {
      this.groupedCities = this.cities.reduce((acc, city) => {
        const initial = city.pinyin[0].toUpperCase()
        if (!acc[initial]) acc[initial] = []
        acc[initial].push(city)
        return acc
      }, {})

      this.initials = Object.keys(this.groupedCities).sort()
    },
    // 其他方法...
  }
}

这种实现方式提供了完整的城市索引功能,包括字母分组、快速导航和触摸交互,适合大多数移动端和PC端应用场景。

标签: 索引城市
分享给朋友:

相关文章

react如何获取数组的索引值

react如何获取数组的索引值

获取数组索引值的常见方法 在React中,处理数组并获取其索引值可以通过多种方式实现,具体取决于使用场景(如渲染列表、事件处理等)。以下是几种典型方法: 使用map方法时的索引参数 在渲染数组时,m…

php实现索引

php实现索引

PHP 实现索引的方法 使用数组索引 PHP 的数组可以自动或手动设置索引,索引可以是整数或字符串。 $array = array(10, 20, 30); echo $array[0]; // 输…

js实现省份城市

js实现省份城市

实现省份城市联动选择 数据结构设计 使用嵌套对象或数组存储省份和城市数据,例如: const provinceData = { "广东省": ["广州市", "深圳市", "珠海市"], "…

jquery 索引

jquery 索引

jQuery 索引的基本概念 jQuery 索引通常指通过索引位置获取或操作 DOM 元素。jQuery 提供了多种方法来实现基于索引的操作,例如 eq()、:eq() 选择器或直接通过数组下标访问。…

php实现索引

php实现索引

PHP 实现索引的方法 在 PHP 中实现索引通常涉及数据库操作、数组处理或文件系统优化。以下是几种常见场景下的实现方法: 数据库表索引 在 MySQL 等数据库中使用 PHP 创建索引: $pd…

城市切换 实现 php

城市切换 实现 php

城市切换功能的实现(PHP) 数据库设计 创建城市数据表,包含字段如id、name、code等。示例SQL: CREATE TABLE cities ( id INT AUTO_INCREM…