美文网首页技术栈
layui组件laydate设置默认值后选择BUG优化

layui组件laydate设置默认值后选择BUG优化

作者: 浪漫矢志不渝 | 来源:发表于2023-05-15 10:39 被阅读0次

在使用laydate时,使用范围选择器设置默认值后会出现再次选择不会重置选框选取样式,
后来分析源码laydate的choose方法时,发现只要加几行代码就可解决问题

// 在判断为日期范围选择器后,初始化结束日期,以便第一次选择,能够重置选中状态
if(t.endState == undefined){
          t.endState = true;
}

//在重置样式后,需要将结束状态设置为false,以便不进入上面判断
t.endState = false;// 设置为false 区别undefined,防止后面每次点击都重置

choose方法具体代码如下:

T.prototype.choose = function(e) {
        var t = this
            , n = t.config
            , a = n.dateTime
            , i = w(t.elem).find("td")
            , r = e.attr("lay-ymd").split("-")
            , l = function(e) {
          new Date;
          e && w.extend(a, r),
          n.range && (t.startDate ? w.extend(t.startDate, r) : t.startDate = w.extend({}, r, t.startTime),
              t.startYMD = r)
        };
        if (r = {
          year: 0 | r[0],
          month: (0 | r[1]) - 1,
          date: 0 | r[2]
        },
            !e.hasClass(s))
          if (n.range) {
            if (w.each(["startTime", "endTime"], function(e, n) {
              t[n] = t[n] || {
                hours: 0,
                minutes: 0,
                seconds: 0
              }
              // 初始化结束日期,以便第一次选择,能够重置选中状态
              if(t.endState == undefined){
                  t.endState = true;
              }
            }),
                t.endState)
              l(),
                  delete t.endState,
                  delete t.endDate,
                  t.startState = !0,
                  i.removeClass(o + " " + u),
                  e.addClass(o),
                  t.endState = false;// 设置为false 区别undefined,防止后面每次点击都重置
            else if (t.startState) {
                console.log(t.startState)
              if (e.addClass(o),
                  t.endDate ? w.extend(t.endDate, r) : t.endDate = w.extend({}, r, t.endTime),
              t.newDate(r).getTime() < t.newDate(t.startYMD).getTime()) {
                var d = w.extend({}, t.endDate, {
                  hours: t.startDate.hours,
                  minutes: t.startDate.minutes,
                  seconds: t.startDate.seconds
                });
                w.extend(t.endDate, t.startDate, {
                  hours: t.endDate.hours,
                  minutes: t.endDate.minutes,
                  seconds: t.endDate.seconds
                }),
                    t.startDate = d
              }
              n.showBottom || t.done(),
                  t.stampRange(),
                  t.endState = !0,
                  t.done(null, "change")
            } else
              e.addClass(o),
                  l(),
                  t.startState = !0;
            w(t.footer).find(g)[t.endDate ? "removeClass" : "addClass"](s)
          } else
            "static" === n.position ? (l(!0),
                t.calendar().done().done(null, "change")) : "date" === n.type ? (l(!0),
                t.setValue(t.parse()).remove().done()) : "datetime" === n.type && (l(!0),
                t.calendar().done(null, "change"))
      }

相关文章

网友评论

    本文标题:layui组件laydate设置默认值后选择BUG优化

    本文链接:https://www.haomeiwen.com/subject/joumsdtx.html