首页 > 其他分享 >uniapp使用uview组件的indexList写选择城市

uniapp使用uview组件的indexList写选择城市

时间:2023-06-06 19:33:38浏览次数:55  
标签:uniapp indexList index iniIndex uview allList item var

<template>
    <view class="">
        <view class="top">
            <view class="search-box mt10 flex alcenter">
                <view class="search-input"><u-search :showAction="false" placeholder="请输入想要搜索的城市" v-model="search"></u-search></view> 
                <view class="filter ft16 " @click="onSearch">搜索</text></view>
            </view>
        </view>
        <view class="ft14 pd12_10 ">
            当前:{{locationInfo.city}}
        </view>
        <view  style="width: 90%; height: 2rpx; background-color: #bcbcbc; margin-left: 20rpx;">
        </view>
        <view  class="pd12_10">
            <view class="ft14">
                热门城市
            </view>
            <view class="flex wrap ">
                <view class="ft14 hot_city border" v-for="(item,index) in hot" @click="hotItem(item)" >
                    {{item.name}}
                </view> 
            </view>
                
        </view>
        <u-index-list :index-list="indexList">
            <template v-for="(item, index) in itemArr">
                <!-- #ifdef APP-NVUE -->
                <u-index-anchor :text="indexList[index]"></u-index-anchor>
                <!-- #endif -->
                <u-index-item>
                    <!-- #ifndef APP-NVUE -->
                    <u-index-anchor :text="indexList[index]"></u-index-anchor>
                    <!-- #endif -->
                    <view class="list-cell" v-for="(it, index) in item" @click="cityItem(it)">
                        {{it.name}}
                    </view>
                </u-index-item>
            </template>
        </u-index-list>
    </view>

</template>

<script>
    export default {
        data() {
            return {
                search: '',
                allList: [],
                hot: [],
                indexList: [],
                itemArr: [],
            }
        },
        onl oad() {
            this.getCity()
        },
        methods: {
            onSearch(){
                this.getCity()
            },
            // 获取定位城市
            getCity() {
                this.$http.api('stores/miniapp/getCity', {
                    search: this.search
                }).then(res => {
                    this.hot = res.hot
                    this.allList = res.list
                    this.setIndex()
                    this.allcity()
                    
                })
            },
            hotItem(item){
                this.$store.commit('setLocation', {
                    city: item.name,
                    lng: item.lng,
                    lat: item.lat,
                });
                uni.switchTab({
                    url: '/pages/home/index/index'
                });
            },
            cityItem(item){
                this.$store.commit('setLocation', {
                    city: item.name,
                    lng: item.lng,
                    lat: item.lat,
                });
                uni.switchTab({
                    url: '/pages/home/index/index'
                });
            },
            // // 右侧索引去重
            setIndex() {
                let iniIndex = []; //定义数组
                this.allList.map((item) => { //遍历当前的数组对象
                    iniIndex.push(item.initials); //将当前某个值productPushCode取出来放入数组中
                })
                iniIndex = Array.from(new Set(iniIndex))
                this.indexList = iniIndex.sort()
            },
            // 城市列表重组
            allcity() {
                var tmp = [];
                for (var i = 0; i < this.indexList.length; i++) {
                    var item = this.indexList[i];
                    for (var j = 0; j < this.allList.length; j++) {
                        var py = this.allList[j].initials;
                        if (py == item) {
                            if (tmp.indexOf(item) == -1) {
                                this.itemArr[i] = [this.allList[j]];
                                tmp.push(item);
                            } else {
                                this.itemArr[i].push(this.allList[j]);
                            }
                        }
                    }
                }
            }
        }
    }
</script>

<style lang="scss" scoped>
    .top {
        // background: #fff;
        padding: 10rpx 30rpx 20rpx;
        position: relative;
        z-index: 10;
        margin-bottom: 20rpx;

        .search-input {
            flex: 1;
        }

        .filter {
            margin-left: 8rpx;
            background-color: red;
            color: #fff;
            padding: 10rpx 30rpx;
            border-radius: 40rpx;
        }
    }

    .list-cell {
        display: flex;
        box-sizing: border-box;
        width: 100%;
        padding: 10px 24rpx;
        overflow: hidden;
        color: #323233;
        font-size: 14px;
        line-height: 24px;
    }
    .hot_city{
        padding: 10rpx 15rpx;
        margin: 10rpx;
    }
</style>

 

标签:uniapp,indexList,index,iniIndex,uview,allList,item,var
From: https://www.cnblogs.com/hs20011205/p/17461498.html

相关文章

  • uniapp主题切换功能的第一种实现方式(scss变量+vuex)
    随着用户端体验的不断提升,很多应用在上线的时候都要求做不同的主题,最基本的就是白天与夜间主题。就像b站app主题切换,像这样的uniapp因为能轻松实现多端发布而得到很多开发者的青睐,但每个端的实现也有可能不同,现我把已实现的功能一点点的大家分享给大家,须要的可以参考一下,可......
  • uniapp能对接蓝牙打印机吗? 答案是肯定的
    ​答案是肯定的,以下是一个使用“uni-bluetooth-print”插件连接蓝牙打印机的示例代码:1.在项目目录中运行以下命令安装插件:npminstalluni-bluetooth-print2.在代码中导入插件:importbluetoothPrintfrom'uni-bluetooth-print';3.调用 `connect()` 方法连接蓝......
  • uniapp中js中的闭包使用
    问题:在uniapp里面,使用闭包函数处理的时候,会导致$this不能全局使用。 解决方案:第一种代码写法(以循环为例):constobj={a:1,b:2,c:3}varkeys=Object.getOwnPropertyNames(obj)keys.forEach(function(key){console.log(key+'--......
  • uniapp安卓移动终端拨打普通电话及4GVolte通话实现
    //代码//普通语音电话audio.onclick=function(){//导入Activity、Intent类varIntent=plus.android.importClass("android.content.Intent");varUri=plus.android.importClass("android.net.Uri");//获取主Activity对象......
  • uniapp专题学习(五)
    前言在uniapp专题学习(四)中学习了以下知识点:native修饰符、父子组件间的传值、sync修饰符与update响应式写法、vue的生命周期、uniapp的界面的交互反馈(uniapp的api比较多,所以只练习了一部分,更多的用法可以参考uniappapi)。动态设置TabBaruni.setTabBarItem(OBJECT)动态设置......
  • 项目场景:uniapp多个输入框弹出软键盘后无法滚动到底部
    项目场景:uniapp多个输入框弹出软键盘后无法滚动到底部|遮挡底部input在做uniapp的时候遇到多个输入框的情况,用官方demo做个演示adjustPan模式问题描述uniapp多个输入框无法拉到最下面原因分析:1.adjustPan模式下软键盘弹出时,webview窗体高度不变,但窗体上推,以保证输入框不被软......
  • uniapp h5+ 拍照、录音功能实现
    uniapph5+拍照、录音功能实现uniapp功能实现提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录uniapph5+拍照、录音功能实现前言一、拍照1.调取摄像头拍摄照片2.管理系统相册,支持从相册中选择图片或视频文件、保存图片或视频文件到相册等功能GalleryOptio......
  • uniapp 组件中使用页面的生命周期(vue2)
    用于直接在组件中使用onLoad,onBackPress等因为之前在写App的时候有许多弹窗,希望可以在有弹窗的时候先关闭弹窗,没有弹窗在执行返回事件,因此需要在页面onBackPress里面写很多判断,因此找了一些方法,写了可以直接在组件中调用页面生命周期的方法!使用mixinthis.$children去循环查询......
  • uniapp专题学习(四)
    前言在uniapp专题学习(三)中学习到的知识点有计算属性computed、计算属性computed和方法methods的区别、vue语法的自定义组件、pops的父子组件参数传递。native修饰符如果想在自定义组件执行原生的事件就需要用native来修饰假设我已经定义好了自定义组件,我想触发click事件,就需......
  • uniapp+vue3+ts 使用pinia报错
    "hasInjectionContext"isnotexportedby"node_modules/vue-demi/lib/index.mjs",importedby"node_modules/pinia/dist/pinia.mjs".11:36:19.397at../node_modules/pinia/dist/pinia.mjs:6:9 解决方法:把pinia降级 先删除pinianpmunipin......