收藏
回答

如何保留上次选中的选项及自定义的数据?

在workinghours页面选中选项,保存后返回上一页,当再次进入该页面时,如图:工作时间选项如果是选中其他选项,可以正常高亮显示上次选中的选项,如果为自定义的时间,就显示不出来,并且自定义选项没有高亮 ,这是什么问题?如何修改?图2为工作时间选项为午班,图3为工作时间自定义选项自定义的时间

<!--pages/relase/hiretwo/hiretwo.wxml-->
<view class="input-group" wx:if="{{page2Data.expectationName === '临/兼职'}}">
  <view class="title">临/兼职时间</view>
  <view class="input-content">
     <view wx:if="{{!WorkingHours}}" class="address {{!WorkingHours ? 'address-red' : ''}}" bind:tap="onWorkingHours">请选择工作时间</view>
        <view class="workingHour" wx:if="{{WorkingHours}}" bind:tap="onWorkingHours">
           <text>工期:{{WorkingHours.duration}};</text>
                 <text>工作时间段:{{WorkingHours.timeslot}};</text>
                 <text>每周工作天数:{{WorkingHours.workingdays}};</text>
                 <text>工作时间:{{WorkingHours.timeshours}}</text>
       </view>
       <image class="thumbnail" src="../../../images/icon-right.png" mode="" />
    </view>
</view>
// pages/relase/hiretwo/hiretwo.js
onWorkingHours(){
   wx.navigateTo({
      url: '../workinghours/workinghours'
   })
},
<!--pages/workinghours/workinghours.wxml-->
<view class="timeslot-group">
   <view class="title">工作时间</view>
   <view class="list">
      <view class="text {{item === timeshours ? 'selected' : ''}}" name="timeshours" bindtap="onTimeshours" wx:for="{{timeshourss}}" value="{{timeshours}}" wx:key="item" data-index="{{index}}" data-value="{{item}}">{{item}}</view>
      <view wx:if="{{timeshourss.length%3 == 2}}" style="width:31%;"></view>
   </view>
   <view class="add-time" wx:if="{{showAddTime}}">
      <view class="add-time-group" wx:for="{{selectedTimes}}" wx:key="index" bindtap="onDelete" data-index="{{index}}">
        <view class="time-content" name="timeshours" value="{{timeshours}}">{{item}}</view>
          <view class="time-x">x</view>
        </view>
        <view class="add-time-slot" bindtap="onAddtime" data-index="{{index}}">+添加时间段</view>
    </view>
</view>
// pages/workinghours/workinghours.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    timeslots:['工作日', '周末节假日', '全周轮班', '按单安排时间', '不限时间'],
    workingdayss:['5天以上', '1-2天', '2-3天', '3-4天','4-5天', '无要求'],
    timeshourss:['早班', '午班', '晚班', '不限时间','自定义'],
    duration:'',
    timeslot:'',
    workingdays:'',
    timeshours:'',
    showAddTime:false,
    showPicker:false,
    hours:[],
    minutes:[],
    selectedTimes: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initTimeData();
    const WorkingHours = app.globalData.WorkingHours || {};
    console.log(WorkingHours)
    this.setData({
      duration: WorkingHours.duration || "",
      timeslot: WorkingHours.timeslot || "",
      workingdays: WorkingHours.workingdays || "",
      timeshours: WorkingHours.timeshours || "",
    });
  },
  /**
   * 工期
   */
  onDuration(e){
    const value = e.detail.value
    this.setData({
      duration:value
    })
  },
  /**
   * 工作时间段
   */
  onTimeslot(e){
    const value = e.currentTarget.dataset.value
    this.setData({
      timeslot:value,
    })
  }
  /**
   * 每周工作天数
   */
  onWorkingdays(e){
    const value = e.currentTarget.dataset.value
    this.setData({
      workingdays:value
    })
  },
  /**
   * 工作时间
   */
  onTimeshours(e){
    const value = e.currentTarget.dataset.value;
    const timesIndex = e.currentTarget.dataset.index;
    if (timesIndex === 4) {
      this.setData({
        showAddTime: true,
      });
    } else {
      this.setData({
        showAddTime: false,
      });
    }
    this.setData({
      timeshours:value,
      timesIndex:timesIndex
    })
  },
  /**
   * 初始化时间
   */
  initTimeData(){
    const hours = [];
    for(let i=0;i<=23;i++){
      hours.push(i.toString().padStart(2, '0'));
    }
    const minutes = [];
    for(let i=0;i<=59;i++){
      minutes.push(i.toString().padStart(2, '0'));
    }
    this.setData({hours,minutes});
  },
  /**
   * 弹窗
   */
  showPicker(e){
    this.setData({
      isPickerVisible:false
    })
  },
  /**
   * 初始化选中时间
   */
  onTimeChange(e){
    console.log(e)
    const value = e.detail.value;
    console.log(value)
    const selectedHour = this.data.hours[value[0]];
    const selectedMinute = this.data.minutes[value[1]];
    const selectedHour1 = this.data.hours[value[3]];
    const selectedMinute1 = this.data.minutes[value[4]];
    this.setData({
      selectedTime:`${selectedHour}:${selectedMinute}-${selectedHour1}:${selectedMinute1}`
    })
  },
  /**
   * 删除时间
   */
  onDelete(e){
    const index = e.currentTarget.dataset.index;
    const selectedTimes = this.data.selectedTimes.filter((_, i) => i !== index);
    this.setData({ selectedTimes });
  },
  /**
   * 添加时间
   */
  onAddtime(e){
    this.setData({
      showPicker:true
    })
  },
  /**
    * 弹窗取消按钮
    */
  closeModal(e) {
       this.setData({
	   showPicker: false
       })
   },
   /**
     * 弹窗确定按钮
     */
  async confirm(e) {
   const that = this;
   const selectedTime = that.data.selectedTime;
   that.setData({
      selectedTimes:[...that.data.selectedTimes, selectedTime],
     showPicker: false
   })
  },
  /**
	 * 保存
	 */
  submitForm(){
		const WorkingHour = {
      duration:this.data.duration,
      timeslot:this.data.timeslot,
      workingdays:this.data.workingdays, 
      timeshours: this.data.timesIndex === 4 ? this.data.selectedTimes.join(', ') : this.data.timeshours,
    }
    app.globalData.WorkingHours = WorkingHour;
		this.setData({
			WorkingHour: app.globalData.WorkingHours
    })
		wx.showToast({
			title: '保存成功',
			icon: 'none'
		})
		setTimeout(() => wx.navigateBack(), 1500)
  }
})
回答关注问题邀请回答
收藏

1 个回答

  • 智能回答 智能回答 本次回答由AI生成
    星期四 17:59
    有用
登录 后发表内容