city-select.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <!-- 城市选择-->
  3. <view class="city-select">
  4. <scroll-view :scroll-top="scrollTop" scroll-y="true" class="city-select-main" id="city-select-main"
  5. :scroll-into-view="toView">
  6. <!-- 预留搜索-->
  7. <view class="city-serach" v-if="isSearch"><input @input="keyInput" :placeholder="placeholder"
  8. class="city-serach-input" /></view>
  9. <!-- 当前定位城市 -->
  10. <view class="hot-title" v-if="activeCity && !serachCity">当前定位城市</view>
  11. <view class="hot-city" v-if="activeCity && !serachCity">
  12. <view class="hot-item" @click="cityTrigger(activeCity)">{{ activeCity[formatName] }}</view>
  13. </view>
  14. <!-- 热门城市 -->
  15. <view class="hot-title" v-if="hotCity.length > 0 && !serachCity">热门城市</view>
  16. <view class="hot-city" v-if="hotCity.length > 0 && !serachCity">
  17. <template v-for="(item, index) in hotCity">
  18. <view :key="index" @click="cityTrigger(item, 'hot')" class="hot-item">{{ item[formatName] }}</view>
  19. </template>
  20. </view>
  21. <!-- 城市列表(搜索前) -->
  22. <view class="citys" v-if="!serachCity">
  23. <view v-for="(city, index) in sortItems" :key="index" v-show="city.isCity" class="citys-row">
  24. <view class="citys-item-letter" :id="'city-letter-' + (city.name === '#' ? '0' : city.name)">
  25. {{ city.name }}</view>
  26. <view class="citys-item" v-for="(item, inx) in city.citys" :key="inx" @click="cityTrigger(item)">
  27. {{ item.cityName }}</view>
  28. </view>
  29. </view>
  30. <!-- 城市列表(搜索后) -->
  31. <view class="citys" v-if="serachCity">
  32. <view v-for="(item, index) in searchDatas" :key="index" class="citys-row">
  33. <view class="citys-item" :key="index" @click="cityTrigger(item)">{{ item.name }}</view>
  34. </view>
  35. </view>
  36. </scroll-view>
  37. <!-- 城市选择索引-->
  38. <view class="city-indexs-view" v-if="!serachCity">
  39. <view class="city-indexs">
  40. <view v-for="(cityIns, index) in handleCity" class="city-indexs-text" v-show="cityIns.isCity"
  41. :key="index" @click="cityindex(cityIns.forName)">
  42. {{ cityIns.name }}
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import citySelect from './citySelect.js';
  50. export default {
  51. props: {
  52. //查询提示文字
  53. placeholder: {
  54. type: String,
  55. default: '请输入城市名称'
  56. },
  57. //传入要排序的名称
  58. formatName: {
  59. type: String,
  60. default: 'cityName'
  61. },
  62. //当前定位城市
  63. activeCity: {
  64. type: Object,
  65. default: () => null
  66. },
  67. //热门城市
  68. hotCity: {
  69. type: Array,
  70. default: () => []
  71. },
  72. //城市数据
  73. obtainCitys: {
  74. type: Array,
  75. default: () => []
  76. },
  77. //是否有搜索
  78. isSearch: {
  79. type: Boolean,
  80. default: true
  81. }
  82. },
  83. data() {
  84. return {
  85. toView: 'city-letter-Find', //锚链接 初始值
  86. scrollTop: 0, //scroll-view 滑动的距离
  87. cityindexs: [], // 城市索引
  88. activeCityIndex: '', // 当前所在的城市索引
  89. handleCity: [], // 处理后的城市数据
  90. serachCity: '', // 搜索的城市
  91. cityData: []
  92. };
  93. },
  94. computed: {
  95. /**
  96. * @desc 城市列表排序
  97. * @return Array
  98. */
  99. sortItems() {
  100. for (let index = 0; index < this.handleCity.length; index++) {
  101. if (this.handleCity[index].isCity) {
  102. let cityArr = this.handleCity[index].citys;
  103. cityArr = cityArr.sort(function(a, b) {
  104. var value1 = a.unicode;
  105. var value2 = b.unicode;
  106. return value1 - value2;
  107. });
  108. }
  109. }
  110. return this.handleCity;
  111. },
  112. /**
  113. * @desc 搜索后的城市列表
  114. * @return Array
  115. */
  116. searchDatas() {
  117. var searchData = [];
  118. for (let i = 0; i < this.cityData.length; i++) {
  119. if (this.cityData[i][this.formatName].indexOf(this.serachCity) !== -1) {
  120. searchData.push({
  121. oldData: this.cityData[i],
  122. name: this.cityData[i][this.formatName]
  123. });
  124. }
  125. }
  126. return searchData;
  127. }
  128. },
  129. created() {
  130. // 初始化城市数据
  131. this.cityData = this.obtainCitys;
  132. this.initializationCity();
  133. this.buildCityindexs();
  134. },
  135. watch: {
  136. obtainCitys(newData) {
  137. this.updateCitys(newData);
  138. }
  139. },
  140. methods: {
  141. /**
  142. * @desc 初始化
  143. */
  144. updateCitys(data) {
  145. if (data && data.length) {
  146. this.cityData = data;
  147. this.initializationCity();
  148. this.buildCityindexs();
  149. }
  150. },
  151. /**
  152. * @desc 监听输入框的值
  153. */
  154. keyInput(event) {
  155. this.serachCity = event.detail.value;
  156. },
  157. /**
  158. * @desc 初始化城市数据
  159. * @return undefind
  160. */
  161. initializationCity() {
  162. this.handleCity = [];
  163. const cityLetterArr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
  164. 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '#'
  165. ];
  166. for (let index = 0; index < cityLetterArr.length; index++) {
  167. this.handleCity.push({
  168. name: cityLetterArr[index],
  169. isCity: false, // 用于区分是否含有当前字母开头的城市
  170. citys: [], // 存放城市首字母含是此字母的数组
  171. forName: 'city-letter-' + (cityLetterArr[index] == '#' ? '0' : cityLetterArr[
  172. index]) //label的绑定
  173. });
  174. }
  175. },
  176. /**
  177. * @desc 得到城市的首字母
  178. * @param str String
  179. */
  180. getLetter(str) {
  181. return citySelect.getFirstLetter(str[0]);
  182. },
  183. /**
  184. * @desc 构建城市索引
  185. * @return undefind
  186. */
  187. buildCityindexs() {
  188. this.cityindexs = [];
  189. for (let i = 0; i < this.cityData.length; i++) {
  190. // 获取首字母
  191. const cityLetter = this.getLetter(this.cityData[i][this.formatName]).firstletter;
  192. // 获取当前城市首字母的unicode,用作后续排序
  193. const unicode = this.getLetter(this.cityData[i][this.formatName]).unicode;
  194. const index = this.cityIndexPosition(cityLetter);
  195. if (this.cityindexs.indexOf(cityLetter) === -1) {
  196. this.handleCity[index].isCity = true;
  197. this.cityindexs.push(cityLetter);
  198. }
  199. this.handleCity[index].citys.push({
  200. cityName: this.cityData[i][this.formatName],
  201. unicode: unicode,
  202. oldData: this.cityData[i]
  203. });
  204. }
  205. },
  206. /**
  207. * @desc 滑动到城市索引所在的地方
  208. * @param id String 城市索引
  209. */
  210. cityindex(id) {
  211. this.toView = id;
  212. // //创建节点查询器
  213. // const query = uni.createSelectorQuery().in(this)
  214. // var that = this
  215. // that.scrollTop = 0
  216. // //滑动到指定位置(解决方法:重置到顶部,重新计算,影响:页面会闪一下)
  217. // setTimeout(() => {
  218. // query
  219. // .select('#city-letter-' + (id === '#' ? '0' : id))
  220. // .boundingClientRect(data => {
  221. // // console.log("得到布局位置信息" + JSON.stringify(data));
  222. // // console.log("节点离页面顶部的距离为" + data.top);
  223. // data ? (that.scrollTop = data.top) : void 0
  224. // })
  225. // .exec()
  226. // }, 0)
  227. },
  228. /**
  229. * @desc 获取城市首字母的unicode
  230. * @param letter String 城市索引
  231. */
  232. cityIndexPosition(letter) {
  233. if (!letter) {
  234. return '';
  235. }
  236. const ACode = 65;
  237. return letter === '#' ? 26 : letter.charCodeAt(0) - ACode;
  238. },
  239. /** @desc 城市列表点击事件
  240. * @param Object
  241. */
  242. cityTrigger(item) {
  243. // 传值到父组件
  244. this.$emit('cityClick', item.oldData ? item.oldData : item);
  245. }
  246. }
  247. };
  248. </script>
  249. <style lang="scss">
  250. //宽度转换vw
  251. @function vww($number) {
  252. @return ($number / 375) * 750+rpx;
  253. }
  254. page {
  255. background-color: #F7F7F7;
  256. }
  257. .bg {
  258. background-color: #FFFFFF;
  259. }
  260. view {
  261. box-sizing: border-box;
  262. }
  263. .city-serach {
  264. width: 100%;
  265. // color: #fff;
  266. padding: 0 vww(10);
  267. &-input {
  268. margin: vww(10) 0;
  269. height: vww(40);
  270. line-height: vww(40);
  271. font-size: vww(14);
  272. padding: 0 vww(5);
  273. border: 1px solid #e5e5e5;
  274. border-radius: 3px;
  275. }
  276. }
  277. .city-select-main {
  278. position: relative;
  279. // overflow: scroll;
  280. // -webkit-overflow-scrolling: touch;
  281. width: 100%;
  282. height: 100%;
  283. // background: #f6f5fa;
  284. // overflow-y: auto;
  285. }
  286. .city-select {
  287. position: relative;
  288. width: 100vw;
  289. height: 100vh;
  290. background: #ffffff;
  291. // 热门城市
  292. .hot-title {
  293. padding-left: vww(23);
  294. width: 100vw;
  295. font-size: 14px;
  296. line-height: vww(40);
  297. color: #9b9b9b;
  298. }
  299. .hot-city {
  300. padding-left: vww(23);
  301. padding-right: vww(20);
  302. overflow: hidden;
  303. width: 100vw;
  304. .hot-item {
  305. float: left;
  306. padding: 0 vww(5);
  307. margin-right: vww(16);
  308. margin-bottom: vww(6);
  309. overflow: hidden;
  310. width: vww(100);
  311. height: vww(31);
  312. font-size: 14px;
  313. text-align: center;
  314. display: -webkit-box;
  315. -webkit-box-orient: vertical;
  316. -webkit-line-clamp: 1;
  317. line-height: vww(31);
  318. color: #4a4a4a;
  319. background: #fff;
  320. border: 1px solid #ebebf0;
  321. &:nth-child(3n) {
  322. margin-right: 0;
  323. }
  324. }
  325. .hot-hidden {
  326. display: none;
  327. margin-right: 0;
  328. }
  329. }
  330. .citys {
  331. .citys-row {
  332. padding: 0 vww(18);
  333. width: 100%;
  334. font-size: 14px;
  335. // background: #fff;
  336. .citys-item-letter {
  337. margin-left: vww(-18);
  338. padding-left: vww(18);
  339. margin-top: -1px;
  340. width: 100vw;
  341. line-height: vww(30);
  342. // color: #fff;
  343. // background: #f6f5fa;
  344. border-top: none;
  345. }
  346. .citys-item {
  347. width: 100%;
  348. line-height: vww(50);
  349. // color: #FFFFFF;
  350. border-bottom: 1px solid #e5e5e5;
  351. &:last-child {
  352. border: none;
  353. }
  354. }
  355. }
  356. }
  357. .city-indexs-view {
  358. position: absolute;
  359. right: 0;
  360. top: 0;
  361. z-index: 999;
  362. display: flex;
  363. width: vww(20);
  364. height: 100%;
  365. text-align: center;
  366. .city-indexs {
  367. width: vww(20);
  368. text-align: center;
  369. vertical-align: middle;
  370. align-self: center;
  371. .city-indexs-text {
  372. margin-bottom: vww(10);
  373. width: vww(20);
  374. font-size: 12px;
  375. color: #000000;
  376. &:last-child {
  377. margin-bottom: 0;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. </style>