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

news/2024/7/20 4:33:44 标签: 微信小程序, 小程序

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

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://www.niftyadmin.cn/n/435650.html

相关文章

索引常见问题

被问到SQL和索引优化问题&#xff0c;如何切入&#xff1f; 可以用 explain 进行分析 思考流程&#xff1a;找到哪些运行时间长浪费性能的sql&#xff0c;然后再用explain分析 慢查询日志 MySQL可以设置慢查询日志&#xff0c;当SQL执行的时间超过我们设定的时间&#xff0…

SOLIDWORKS Electrical如何绘制电线

SOLIDWORKS Electrical 是一套计算机辅助工程 (CAE) 设计工具&#xff0c;他可以帮助电气设计工程师减少创新的内在风险&#xff0c;并允许公司减少对物理原型的需求&#xff0c;从而在更短时间内以更低成本将产品推向市场。通过一组强大、直观的电气设计功能&#xff0c;设计人…

图的企业应用-A*算法自动寻路

引言 MC想必大家都玩过,但鸡哥利用A*自动寻路算法来找箱子 箱子里有鸡你太美唱片&#xff0c;和准备好的篮球 当然在这是游戏中找到的宝箱 还得原石x5等一大堆的养成物品 ???等等 ,原神 玩家露出鸡脚了吧! 不应该是 有鸡你太美唱片,还有一条鱼并且给梅里猫的名叫荔枝的? 这…

Java8 四大核心函数式接口 Function、Consumer、Supplier、Predicate使用示例

Function<T, R> T&#xff1a;入参类型&#xff0c;R&#xff1a;出参类型 调用方法&#xff1a;R apply(T t); 定义函数示例&#xff1a;Function<Integer, Integer> func p -> p * 10; // 输出入参的10倍 调用函数示例&#xff1a;func.apply(10); // 结…

Node.js:Buffer基础

文章目录 创建Buffer对象Buffer对象的常用方法1. Buffer.from(string[, encoding])2. Buffer.from(array)3. Buffer.alloc(size[, fill[, encoding]])4. buf.toString([encoding[, start[, end]]])5. buf.toJSON()6. buf.compare(otherBuffer)7. buf.copy(target[, targetStart…

Webpack 使用详解

Webpack 是一个现代 JavaScript 应用程序的静态模块打包器。本文将详细介绍如何使用 Webpack&#xff0c;以及提供代码示例。为了保持篇幅&#xff0c;我们将简要介绍 Webpack 的核心概念和功能。 一、核心概念 入口&#xff08;entry&#xff09;&#xff1a;应用程序的起点…

php swoole 请求tcp服务的两种方式

第一种方式 &#xff08;可以实现在代码中调用&#xff09; $task_connection stream_socket_client(tcp://127.0.0.1:8721,$errno,$errstr);$task_data array(route > demo/test,);fwrite($task_connection, json_encode($task_data));if(!empty($errno) || !empty($e…

1.8C++流提取运算符重载

C流提取运算符重载 在 C中&#xff0c;流提取运算符&#xff08;>>&#xff09;是用于从流中提取数据的运算符。 C中的流提取运算符可以被重载&#xff0c;使得程序员可以自定义输入对象的方式&#xff0c;更方便地输入自定义的数据类型&#xff0c;也可以使得输入更加…