好客租房147-渲染(渲染右侧索引列表)

1封装renderCityIndex方法 用来渲染城市列表

2在方法中 获取到索引数组cityIndex 遍历cityIndex 渲染索引列表

3将缩影hot替换为热

4在state中状态添加状态avtiveIndex

5判断高亮条件

import React from 'react'
import axios from 'axios'
//导入axios
//导入navBar组件
import { NavBar, Icon } from 'antd-mobile'
import './index.scss'
import { getCurrentCity } from '../../utils'
//导入react-virtualized组件
import { List, AutoSizer } from 'react-virtualized'
// 数据格式化的方法
// list: [{}, {}]
const formatCityData = (list) => {
	const cityList = {}
	// const cityIndex = []

	// 1 遍历list数组
	list.forEach((item) => {
		// 2 获取每一个城市的首字母
		const first = item.short.substr(0, 1)
		// 3 判断 cityList 中是否有该分类
		if (cityList[first]) {
			// 4 如果有,直接往该分类中push数据
			// cityList[first] => [{}, {}]
			cityList[first].push(item)
		} else {
			// 5 如果没有,就先创建一个数组,然后,把当前城市信息添加到数组中
			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: [],
        activeIndex:0
	}
	//处理封装字母索引的方法
	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['#'] = [curcity]
		cityIndex.unshift('#')

		this.setState({
			cityList,
			cityIndex,
		})
	}
     // 封装渲染右侧索引列表的方法
  renderCityIndex() {
    // 获取到 cityIndex,并遍历其,实现渲染
    const { cityIndex, activeIndex } = this.state
    return cityIndex.map((item, index) => (
      <li className="city-index-item" key={item}>
        <span className={activeIndex === index ? 'index-active' : ''}>
          {item === 'hot' ? '热' : item.toUpperCase()}
        </span>
      </li>
    ))
  }
	getRowHeight({ index }) {
		// 索引标题高度 + 城市数量 * 城市名称的高度
		// TITLE_HEIGHT + cityList[cityIndex[index]].length * NAME_HEIGHT
		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>
                  {/* 右侧索引列表 */}
        {/* 
          1 封装 renderCityIndex 方法,用来渲染城市索引列表。
          2 在方法中,获取到索引数组 cityIndex ,遍历 cityIndex ,渲染索引列表。
          3 将索引 hot 替换为 热。
          4 在 state 中添加状态 activeIndex ,指定当前高亮的索引。
          5 在遍历 cityIndex 时,添加当前字母索引是否高亮的判断条件。
        */}
        <ul className="city-index">{this.renderCityIndex()}</ul>
			</div>
		)
	}
}

export default cityList

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端大歌谣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值