| import React from 'react' |
| import axios from 'axios' |
| |
| |
| import { NavBar, Icon } from 'antd-mobile' |
| import './index.scss' |
| import { getCurrentCity } from '../../utils' |
| |
| import { List, AutoSizer } from 'react-virtualized' |
| |
| |
| const formatCityData = (list) => { |
| const cityList = {} |
| |
| |
| |
| list.forEach((item) => { |
| |
| const first = item.short.substr(0, 1) |
| |
| if (cityList[first]) { |
| |
| |
| cityList[first].push(item) |
| } else { |
| |
| cityList[first] = [item] |
| } |
| }) |
| |
| |
| const cityIndex = Object.keys(cityList).sort() |
| |
| return { |
| cityList, |
| cityIndex, |
| } |
| } |
| |
| const list = Array(100).fill('react-virtualized') |
| |
| const TITLE_HEIGHT = 36 |
| |
| const NAME_HEIGHT = 50 |
| |
| class cityList extends React.Component { |
| state = { |
| cityList: [], |
| cityIndex: [], |
| } |
| |
| formDataCityIndex = (letter) => { |
| switch (letter) { |
| case '#': |
| return '当前定位' |
| case 'hot': |
| return '热门城市' |
| default: |
| return letter.toUpperCase() |
| } |
| } |
| componentDidMount() { |
| this.getCityList() |
| } |
| |
| |
| rowRenderer({ |
| key, // Unique key within array of rows |
| index, // 索引号 |
| isScrolling, // 当前项是否正在滚动中 |
| isVisible, // 当前项在 List 中是可见的 |
| style, // 注意:重点属性,一定要给每一个行数据添加该样式!作用:指定每一行的位置 |
| }) { |
| |
| const { cityIndex, cityList } = this.state |
| const letter = cityIndex[index] |
| |
| |
| |
| return ( |
| <div key={key} style={style} className="city"> |
| <div className="title">{this.formDataCityIndex(letter)}</div> |
| {/* <div className="name">上海</div> */} |
| {cityList[letter].map((item) => ( |
| <div className="name" key={item.value}> |
| {item.label} |
| </div> |
| ))} |
| </div> |
| ) |
| } |
| async getCityList() { |
| const res = await axios.get('http://localhost:8080/area/city?level=1') |
| console.log(res, 'resss') |
| const { cityList, cityIndex } = formatCityData(res.data.body) |
| console.log(cityList, cityIndex) |
| |
| const hotRes = await axios.get('http://localhost:8080/area/hot') |
| console.log(hotRes, 'hotRes') |
| |
| cityList['hot'] = hotRes.data.body |
| cityIndex.unshift('hot') |
| console.log(cityList, cityIndex, 'hotList') |
| |
| const curcity = await getCurrentCity() |
| |
| cityList['#'] = [cityList] |
| cityIndex.unshift('#') |
| |
| this.setState({ |
| cityList, |
| cityIndex, |
| }) |
| } |
| getRowHeight({ index }) { |
| |
| |
| const { cityList, cityIndex } = this.state |
| return TITLE_HEIGHT + cityList[cityIndex[index]].length * NAME_HEIGHT |
| } |
| render() { |
| return ( |
| <div className="citylist"> |
| <NavBar |
| className="navbar" |
| mode="light" |
| icon={<i className="iconfont icon-back" />} |
| onLeftClick={() => this.props.histoty.push.go(-1)} |
| // 导航栏右边内容 |
| // rightContent={[ |
| // <Icon |
| // key="0" |
| // type="search" |
| // style={{ marginRight: '16px' }} |
| // />, |
| // <Icon key="1" type="ellipsis" />, |
| // ]} |
| > |
| 城市选择 |
| </NavBar> |
| {/* 城市列表 */} |
| <AutoSizer> |
| {({ width, height }) => ( |
| <List |
| width={width} |
| height={height} |
| rowCount={this.state.cityIndex.length} |
| rowHeight={this.getRowHeight.bind(this)} |
| rowRenderer={this.rowRenderer.bind(this)} |
| /> |
| )} |
| </AutoSizer> |
| </div> |
| ) |
| } |
| } |
| |
| export default cityList |
运行结果

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南