微信小程序实现选择地区demo,记录最近访问的地区

news/2024/7/3 16:47:43

最终效果图:
在这里插入图片描述
在这里插入图片描述

index.wxml

<view class="container">
    <view class="search_contaniner">
      <view class="search_box">
          <input type="text" placeholder="省/市级/区县级" value="{{search_cont}}" bindinput="handleInput"/>
          <icon type="clear" size="32rpx" class="searhc_clear" wx:if="{{search_cont != ''}}" bindtap="handleClear"></icon>
      </view>
    </view>
</view>
<block wx:if="{{ search_cont == '' }}">
  <view class="now_location container">
        <view class="now_location_l">
          <text>当前:</text>
          <text>{{group_name}}</text>
        </view>
        <view class="now_location_r">
          <!-- <text class="iconfont icon-dingwei"></text>
          <text>重新定位</text> -->
        </view>
  </view>
  <view class="container search_view_content">
    <view class="search_view_item" wx:if="{{history_list.length > 0}}">
      <view class="search_view_title">最近访问</view>
      <view class="search_view_block_list">
        <block wx:for="{{history_list}}" wx:key="index">
          <view class="search_view_block" bindtap="checkGroupid" data-groupid="{{item.group_id}}" data-groupName="{{item.group_name}}">{{item.group_name}}</view>
        </block>
      </view>
    </view>
    <view class="search_view_item">
      <view class="search_view_title">省级</view>
      <view class="search_view_block_list">
        <view class="search_view_block" bindtap="checkGroupid" data-groupid="61" data-groupName="陕西省">陕西省</view>
      </view>
    </view>
    <view class="search_view_item">
      <view class="search_view_title">市级</view>
      <view class="search_view_block_list">
        <block wx:for="{{list}}" wx:key="index">
          <view class="search_view_block" bindtap="checkGroupid" data-groupid="{{item.group_id}}" data-groupName="{{item.group_name}}">{{item.group_name}}</view>
        </block>
      </view>
    </view>
    <block wx:for="{{list}}" wx:key="index">
      <view class="search_view_item" wx:if="{{item.children.length > 0}}">
        <view class="search_view_title">{{item.group_name}}</view>
        <view class="search_view_ul">
          <block wx:for="{{item.children}}" wx:key="index" wx:for-item="group">
            <view class="search_view_li" bindtap="checkGroupid" data-groupid="{{group.group_id}}" data-groupName="{{group.group_name}}">{{group.group_name}}</view>
          </block>
        </view>
      </view>
    </block>
  </view>
</block>
<block wx:else>
  <view class="container search_content">
    <block wx:if="{{search_list.length <= 0}}">
      <view class="search_empty">抱歉,未找到相关位置,可尝试修改后重试</view>
    </block>
    <block wx:else>
      <view class="search_ul">
        <block wx:for="{{search_list}}" wx:key="index">
          <view class="search_li">
            <view class="iconfont icon-dingwei1 search_li_l"></view>
            <view class="search_li_r" bindtap="checkGroupid" data-groupid="{{item.group_id}}" data-groupName="{{item.group_name1 + item.group_name}}">
              <view>
                <text class="setRed">{{item.group_name1}}</text>
                <text>{{item.group_name}}</text>
              </view>
              <text class="iconfont icon-xiangyoujiantou"></text>
            </view>
          </view>
        </block>
      </view>
    </block>
    

    
  </view>
</block>

index.js

const app = new getApp();
Page({
  data: {
    group_name: app.globalData.group_info.group_name, // 当前所选区划名
    search_cont: '', // 搜索关键词
    list: [], // 所有市区及地区列表
    history_list: [], // 最近访问 最多保存3个
    search_list: [], // 搜索后显示的列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  	// 获取是否存有历史数据
    const history_data = wx.getStorageSync('history_data');
    if(history_data){
      this.setData({
        history_list: JSON.parse(history_data)
      })
    }
    // 获取所有区划
    app.$http.get('get_list_groupAll', {}).then(res => {
      this.setData({
        list: res.content
      })
    }).catch(err => {
      console.log(err)
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      group_name: app.globalData.group_info.group_name
    })
  },
  // 点击选择地区
  checkGroupid(e){
    console.log(e)
    const group_id = e.target.dataset.groupid;
    const group_name = e.target.dataset.groupname;
    app.globalData.group_info = {
      group_id: group_id,
      group_name: group_name
    };
    // 点击选择后将值记录至最近访问数组
    // const 
    this.setHistoryData({group_id: group_id, group_name: group_name});
    // 选择完毕后返回首页
    wx.navigateBack({
      delta: 1,
    })
  },
  // 搜索框触发输入事件
  handleInput(e){
    this.searchGroup(e.detail.value);
    this.setData({
      search_cont: e.detail.value
    })
  },
  // 搜索时触发,查询与搜索内容匹配的所有区划
  searchGroup(keys){
    const list = this.data.list;
    const new_list = [];
    list.forEach(item1 => {
      if(keys == item1.group_name.slice(0, keys.length)){
        new_list.push({group_id: item1.group_id, group_name: item1.group_name});
        if(item1.children.length > 0){
          item1.children.forEach(item2 => {
            if(keys == item2.group_name.slice(0, keys.length)){
              new_list.push({group_id: item2.group_id, group_name: item2.group_name});
            }
          })
        }
      }
    })
    this.setData({
      search_list: this.setColorList(keys, new_list)
    })
    console.log(this.data.search_list)
  },
  // 点击清空搜索
  handleClear(){
    this.setData({
      search_cont: '',
      search_list: []
    })
  },
  // 为搜索列表设置和关键词相等的字符设置颜色
  setColorList(keys, list){
    if(keys == '' || list.length < 1){
      return [];
    }
    var new_list = [];
    list.forEach((item, index) => {
      let val = {};
      val.group_id = item.group_id;
      val.group_name1 = item.group_name.slice(0, keys.length);
      val.group_name = item.group_name.replace(val.group_name1, '');
      new_list[index] = val;
    })
    return new_list;
  },
  // 设置历史数据
  setHistoryData(data={}){
    let history_list = this.data.history_list;
    if(JSON.stringify(data) != '{}'){
      history_list.unshift(data);
    }
    // 去重
    let uniqueArr = Array.from(new Set(history_list.map(JSON.stringify))).map(JSON.parse);
    // 检测最近访问数量是否超过3条,如超过则删除最后一条
    if(uniqueArr.length > 3){
      uniqueArr.splice(3, 1)
    }
    // console.log('unique', uniqueArr);
    this.setData({
      history_list: uniqueArr
    })
    wx.setStorageSync('history_data', JSON.stringify(uniqueArr))
  }

})

index.wxss

/* pages/search/index.wxss */
page{
  background-color: #eee;
}
.search_box{
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  padding: 0 30rpx;
  border-radius: 50rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.search_box input{
  font-size: 28rpx;
  /* margin-left: 30rpx; */
}

.now_location{
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  border-radius: 15rpx;
}
.now_location text{
  font-size: 28rpx;
  color: #333;
}
.now_location_r .iconfont{
  margin-right: 10rpx;
}

.search_view_content{
  background-color: #fff;
  margin-top: 20rpx;
}
.search_view_item{
  margin-top: 30rpx;
}
.search_view_item:first-child{
  margin-top: 0;
}
.search_view_title{
  font-size: 28rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
}
.search_view_block_list{
  display: grid;
  grid-template-columns: repeat(4, 23%);
  grid-gap: 20rpx;
}
.search_view_block{
  padding: 20rpx 0;
  text-align: center;
  font-size: 28rpx;
  border: 1px solid #ccc;
  border-radius: 15rpx;
  /* width: 23%; */
}

/* .search_view_ul{} */
.search_view_li{
  padding: 20rpx 0;
  border-bottom: 1px solid #eee;
  font-size: 28rpx;
}
.search_view_li:first-child{
  padding-top: 0;
}

.search_content{
  min-height: 85vh;
  background-color: #fff;
}
.search_empty{
  text-align: center;
  font-size: 28rpx;
  color: #333;
}
.search_li{
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search_li_l{
  margin-right: 16rpx;
}
.search_li_r{
  flex: 2;
  padding: 20rpx 0;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.setRed{
  color: red;
}

http://lihuaxi.xjx100.cn/news/1247060.html

相关文章

1.5 掌握Scala内建控制结构(一)

一、条件表达式 &#xff08;一&#xff09;语法格式 if (条件) 值1 else 值2 &#xff08;二&#xff09;执行情况 条件为真&#xff0c;结果是值1&#xff1b;条件为假&#xff0c;结果是值2。如果if和else的返回结果同为某种类型&#xff0c;那么条件表达式结果也是那种…

10 个杀手级的Python自动化脚本,解放你的双手

重复性任务总是耗时且无聊&#xff0c;想一想你想要一张一张地裁剪 100 张照片或 Fetch API、纠正拼写和语法等工作&#xff0c;所有这些任务都很耗时&#xff0c;为什么不自动化它们呢&#xff1f;在今天的文章中&#xff0c;我将与你分享 10 个 Python [自动化脚本]。 所以&a…

网络编程与自动化(python)

20.1 网络编程与自动化概述 传统网络运维困境大家在日常的网络运维中是否遇到过如下问题: 设备升级:现网有数千台网络设备,你需要周期性、批量性地对设备进行升级。配置审计:企业年度需要对设备进行配置审计。例如要求所有设备开启sTelnet功能,以太网交换机配置生成树安全…

【mmcls】mmdet中使用mmcls的网络及预训练模型

mmcls现在叫mmpretrain&#xff0c;以前叫mmclassification&#xff0c;这里为了统一称为mmcls。在基于MM框架的下游任务&#xff0c;例如检测(mmdetection)中可以使用mmcls中的backbone进行特征提取&#xff0c;但这就需要知道网络的参数以及输出特征的维度。本文简单介绍了在…

Spring Boot进阶(48):SpringBoot之集成WebSocket及使用说明 | 超级详细,建议收藏

1. 前言&#x1f525; 对于很多小伙伴来说&#xff0c;项目现在都普遍前后端分离&#xff0c;各干各的事儿&#xff0c;在前后端进行服务调用才会有大面积的碰头&#xff0c;后端接口数据格式发生变更要通知前端&#xff0c;相反&#xff0c;前端有变化要告诉后端&#xff0c;这…

Matter实战系列-----4.matter设备的OTA

一、生成OTA固件升级压缩包 1.1 修改OTA固件版本号为3 1.2 编译生成 MatterLightOverThread_V3.ota 将编译得到的MatterLightOverThread.s37固件名称改成MatterLightOverThread_V3.s37&#xff0c;复制到以下Windows文件夹 C:\SiliconLabs\SimplicityStudio\v5\developer\ad…

IT行业里有个词叫“技术债务”

关于技术债务的文章&#xff0c;尽管实践中会堆积技术债&#xff0c;但这个概念并不在我们的工作中频繁出现。这篇文章就系统性讲讲技术债&#xff0c;让大家避免知其然&#xff0c;不知其所以然。 一、技术债是什么 技术负债&#xff08;英语&#xff1a;Technical debt&…

windows编译ffmpeg,并开启png的编解码器

废话不多说了&#xff0c;先上下载链接 ffmpeg官方网站&#xff1a;http://ffmpeg.org/download.html ffmpeg源码下载链接:https://ffmpeg.org/releases/ffmpeg-3.4.13.tar.gz 如果需要其他版本&#xff0c;修改版本号即可&#xff0c;适用于3.4全系列&#xff0c;如https:/…