当前位置: 首页>大数据>正文

微信小程序组件时间搜索

姹熸箹瑙勭煩锛佸厛涓婃晥鏋滃浘馃憞
微信小程序组件时间搜索,第1张
鏁堟灉鍥?/div>

鏄矖绯欎簡涓€浜涘椃涓嶈繃瀹炵幇鍔熻兘姣旇緝閲嶈鍢縹

棣栧厛锛屽湪鏍圭洰褰曚笅鏂板缓涓€涓枃浠跺す锛屼笓闂ㄦ斁缃粍浠跺唴瀹广€傝鏂囦欢澶逛笅鍐嶆柊寤烘枃浠跺す锛屾斁缃垜浠啓濂界殑鏃堕棿鎼滅储鐨勫唴瀹广€?/p>

微信小程序组件时间搜索,第2张
鏂囦欢浣嶇疆

缁勪欢鍐呭娑夊強鍒皃icker鐨勪娇鐢紝鐢ㄥ井淇″皬绋嬪簭鑷甫鐨勪篃鍙互锛寁ant缁勪欢搴撻噷涔熷彲浠ャ€備笉杩囦釜浜烘劅瑙夊皬绋嬪簭鑷甫鐨勬瘮杈冨ソ鐢紝鐩存帴鎿嶄綔灏辫锛屼笉鐢ㄦ搷浣滃儚vant缁勪欢搴撻噷閭d箞澶氱殑鏂规硶銆傘€傜偣鍑诲睍寮€picker锛岄€夋嫨鏃堕棿锛岀‘璁ゆ椂闂淬€傘€傚氨寰堢儲鍝堝搱鍝堝搱鍝堝搱

timeFilter.wxml鏂囦欢鍐呭锛氾紙startTime涓鸿捣濮嬫棩鏈燂紝endTime涓虹粨鏉熸棩鏈熴€俻icker鍐呯殑鎴嚦鏃ユ湡end榛樿涓哄綋鍓嶆棩鏈熴€傦級

<view class="search_round">
  <view class="time">

    <picker mode="date" value="{{startTime}}" start="2000-01-01" end="{{currentDate}}" bindchange="bindDateChange" data-msg="start">
      <view class="start">
        {{ startTime }}
      </view>
    </picker>

    <view class="line">鈥斺€?lt;/view>

    <picker mode="date" value="{{endTime}}" start="2000-01-01" end="{{currentDate}}" bindchange="bindDateChange" data-msg="end">
      <view class="end">
        {{ endTime }}
      </view>
    </picker>

  </view>

  <view class="search" bindtap="_handleFilter">
    <van-icon name="search" size="24" color="lightgrey" />
  </view>

</view>

timeFilter.wxss鏂囦欢鍐呭锛?/p>


.search_round{
  box-sizing: border-box;
  width: 100%;
  margin-bottom: 0;
  border-radius: 50px;
  background: #fff;
  font-size: 26rpx;

  display: flex;
  align-items: center;
  justify-content: space-between;

  padding: 4rpx;
  padding-left: 20rpx;
}

.search_round .time {
  color: #868383;
  flex: 7;

  display: flex;
  align-items: center;
  justify-content: space-around;
}
.search_round .search {
  flex: 1;
  margin-left: 20rpx;
  margin-top: 10rpx;
}

timeFilter.json鏂囦欢鍐呭锛?/p>

{
  "component": true,
  "usingComponents": {
    "van-icon": "@vant/weapp/icon/index"
  }
}

timeFilter.js鏂囦欢鍐呭锛?/p>

// pages/common/timeFilter.js
Component({
  lifetimes: {
    // 缁勪欢鐨勭敓鍛藉懆鏈熷嚱鏁般€傝繘鍏ラ〉闈㈠嵆鑾峰彇褰撳墠鏃ユ湡
    attached: function () {
      this.getTime();
    }
  },
  /**
   * 缁勪欢鐨勫睘鎬у垪琛細鍦ㄧ埗缁勪欢涓彲鑳戒細杩涜鎿嶄綔鐨勫彉閲忚缃湪properties閲?
   */
  properties: {
    startTime: {
      type: String,
      value: ""
    },
    endTime: {
      type: String,
      value: ""
    },
  },

  /**
   * 缁勪欢鐨勫垵濮嬫暟鎹細鐖剁粍浠朵腑涓嶄細鍙戠敓鏀瑰彉鐨勫彉閲忚缃湪data閲?
   */
  data: {
    timeType: "",
    currentDate: ""
  },

  /**
   * 缁勪欢鐨勬柟娉曞垪琛?
   */
  methods: {
    getTime() {
      // 鑾峰彇褰撳墠鏃ユ湡
      let date = new Date();
      // 鏍煎紡鍖栧綋鍓嶆棩鏈?
      let year = date.getFullYear();
      let agoMonth = date.getMonth();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      // 琛ラ浂鍎?
      agoMonth = agoMonth <= 9 `0${agoMonth}` : agoMonth;
      month = month <= 9 `0${month}` : month;
      day = day <= 9 `0${day}` : day;

      let endTime = `${year}-${month}-${day}`;
      let startTime = `${year}-${agoMonth}-${day}`;
      // 璧嬪€?
      this.setData({
        endTime,
        startTime,
        currentDate: endTime
      })
    },
    bindDateChange(e) {
      // 鍙栧€?
      let value = e.detail.value;
      // 涓や釜鏃ユ湡鐢ㄤ簡鍚屼竴涓柟娉曪紝鎵€浠ュ彟澶栬缃竴涓弬鏁版潵鍋氬尯鍒?
      let timeType = e.currentTarget.dataset.msg;
      // 璧峰鏃ユ湡璧嬪€?
      if (timeType == 'start') {
        this.setData({
          startTime: value
        })
      }
      // 缁撴潫鏃ユ湡璧嬪€?
      if (timeType == "end") {
        this.setData({
          endTime: value
        })
      }
    },
    // 鐐瑰嚮鎼滅储鍥捐〃杩涜鐨勬搷浣?
    _handleFilter() {
      let startTime = new Date(this.data.startTime).getTime();
      let endTime = new Date(this.data.endTime).getTime();
      // 璧峰鏃ユ湡涓嶈兘瓒呰繃缁撴潫鏃ユ湡鐨勬嫤鎴?
      if (startTime > endTime) {
        wx.showToast({
          title: "璇烽€夋嫨姝g‘鏃堕棿鍖洪棿",
          icon: "none",
          duration: 2000
        })
        return;
      }
      // 杩欎釜鎴戣彍楦¤涓嶆竻妤氥€傘€傘€傘€傚畼鏂规枃妗i噷鏈夎В閲娿€傘€傝繕鍙互杩涜鏇村璁剧疆銆傘€傚凹钀岃嚜宸卞幓鐪嬪搱鍝堝搱鍝?
      this.triggerEvent("handleFilter");
    }
  }
})

缁勪欢鍐呭灏辨槸杩欎簺馃憜锛屼互涓嬫槸鐖剁粍浠朵腑鐨勮缃€?br> 鐖剁粍浠?json閰嶇疆锛氣啌

{
  "usingComponents": {
    "time-filter": "../../../components/timeFilter/timeFilter"
  },
  "navigationBarBackgroundColor": "#ffffff",
  "navigationBarTextStyle": "black",
  "navigationBarTitleText": "鏃堕棿妫€绱?,
  "backgroundColor": "#eeeeee",
  "backgroundTextStyle": "light"
}

鐖剁粍浠?wxml锛氾紙鏈€濂藉湪寮曞叆鐨則imeFilter澶栭潰鍐嶅涓鍣紝灏嗘潵鍦ㄧ埗缁勪欢涓柟渚胯缃牱寮忥級

  <view class="time-container">
    <time-filter id="filter" startTime="{{startTime}}" endTime="{{endTime}}" bind:handleFilter="_handleFilter"></time-filter>
  </view>

鐖剁粍浠朵腑鐨勬牱寮忓氨涓嶄笂浜嗭紝鍙湪time-container璁剧疆浜嗕竴涓搴︼紝鍜屽眳涓瓇

鐖剁粍浠?js涓渶瑕佽繘琛屼笁涓搷浣溿€?br> 1.锛堥渶瑕佷负璧峰鏃ユ湡鍜岀粨鏉熸棩鏈熸彁鍓嶈祴鍊肩殑璇濇墠杩涜杩欎竴姝ワ級鍦╠ata涓缃彉閲忥紝涓庡瓙缁勪欢鍙岃姳鎷彿鍐呯殑鍚嶇О涓€鑷?/p>

Page({
  data {
    startTime: "",
    endTime:""
  }
})

2.鐢熷懡鍛ㄦ湡鍑芥暟onShow涓?鈫擄紙杩欓噷鐨刬d闇€瑕佸拰涓婇潰鐨刬d瀵瑰簲锛?/p>

  onShow() {
    this.timeFilter = this.selectComponent('#filter');
  },

3.瀛愮粍浠跺唴鐨勬柟娉曚娇鐢ㄢ啌

  _handleFilter() {
  // 杩欓噷锛屽疄涓嶇浉鐬掞紝璇ュ共鍢涘氨骞插槢浜嗗搱鍝堝搱闇€瑕佷紶鍊肩殑浼犲€硷紝璋冩帴鍙g殑璋冩帴鍙?
  // 闇€瑕佸彇鍊肩殑璇硶
    let start = this.timeFilter.data.startTime;
    let end = this.timeFilter.data.endTime;
    console.log(start,"start");
    console.log(end,"end");
  },

tada~涓€涓椂闂存绱㈢殑缁勪欢灏卞畬鎴愬暒


https://www.xamrdz.com/bigdata/7gd1994314.html

相关文章: