当前位置:首页 > VUE

vue实现城市索引列表

2026-01-20 15:39:19VUE

Vue 实现城市索引列表

数据结构准备

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

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

按字母分组

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

vue实现城市索引列表

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>

索引栏交互

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

vue实现城市索引列表

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

性能优化

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

  • 使用虚拟滚动技术
  • 添加防抖处理滚动事件
  • 考虑使用 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端应用场景。

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

相关文章

vue实现搜索城市

vue实现搜索城市

Vue 实现搜索城市功能 数据准备 在Vue组件中定义一个城市列表数组和一个搜索关键词变量。城市列表可以静态定义或通过API动态获取。 data() { return { cities:…

react实现城市下拉组件

react实现城市下拉组件

实现城市下拉组件 使用React实现城市下拉组件可以通过以下步骤完成。该组件允许用户从下拉列表中选择城市,并支持搜索和过滤功能。 安装依赖 确保项目已经安装了React和必要的依赖。如果需要使用第三…

js的索引的实现

js的索引的实现

JavaScript 索引的实现方式 在 JavaScript 中,索引通常用于快速访问数据结构中的元素,常见于数组、对象、字符串等场景。以下是几种典型索引实现方式: 数组索引 数组通过数字索引直接…

js索引实现

js索引实现

索引的基本概念 索引是一种数据结构,用于快速查找数据。在 JavaScript 中,索引通常用于优化数组或对象的查找操作,减少时间复杂度。 数组索引的实现 数组的索引可以通过直接访问下标实现,时间复…

php实现城市跳转

php实现城市跳转

实现城市跳转的方法 使用用户IP定位 通过获取用户的IP地址,结合IP定位服务(如高德地图API、百度地图API或第三方IP库)确定用户所在城市,然后跳转到对应城市的页面。 // 获取用户IP $i…

vue实现城市定位

vue实现城市定位

获取用户地理位置 使用浏览器的Geolocation API获取用户当前的经纬度坐标。Vue中可以封装为一个方法,返回Promise以便异步处理。 methods: { getCurrentPo…