【问题标题】:Highlight certain dates on bootstrap-datepicker在 bootstrap-datepicker 上突出显示某些日期
【发布时间】:2014-04-26 05:22:25
【问题描述】:

我正在使用 bootstrap-datepicker 检查我的网站销售的日期,并且我想显示或突出显示至少有一次销售的日期。我可以在 JSON 上传递日期,然后将它们放入 Javascript 的数组中,但我不知道如何让 datepicker 获取日期并突出显示它们。

bootstrap-datepicker 有没有办法实现这一点?

【问题讨论】:

    标签: javascript bootstrap-datepicker


    【解决方案1】:

    这是另一个突出显示日期范围的示例:

    var inRange=false;
    $('#elementPicker').datepicker({
      beforeShowDay: function(date) {
        dateFormat = date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
    
        if (dateFormat == '2014-01-01') {
          inRange = true; //to highlight a range of dates
          return {classes: 'highlight', tooltip: 'Title'};
        }
        if (dateFormat == '2014-01-05') {
          if (inRange) inRange = false;  //to stop the highlight of dates ranges
          return {classes: 'highlight', tooltip: 'Title'};
        }
        if (inRange) {
          return {classes: 'highlight-range'}; //create a custom class in css with back color you want
        }
      }
    });
    

    【讨论】:

      【解决方案2】:
      $('#xxx').datepicker()
        .on('onRender', function(ev){
          if (ev.date.valueOf() == your date){
            return 'highlight';
          }
        });
      

      可能会成功,虽然我不确定。

      【讨论】:

      【解决方案3】:

      有一种简单的方法可以在引导日历中设置多个日期。 有一个属性 multidate 可用于选择。 在下面找到工作代码。

           $('.inline_date').datepicker({
              multidate: true,
              todayHighlight: true,
              minDate: 0,
          });
      
       $('.inline_date').datepicker('setDates', [new Date(2015, 7, 5), new Date(2015, 7, 8), new Date(2015, 7, 7)])
      

      只有一个问题,高亮在点击时会被删除。并且需要一个月的时间。如果你想要 8 月的日期,那么你必须使用 7 而不是 8。

      【讨论】:

        【解决方案4】:

        我使用 Knockout 绑定和 datepicker 属性“beforeShowDay”解决此问题:

        function MainFilters() {
            var self = this;
            self.notAppliedDates = ko.observableArray([]);
            self.customDates = ko.observableArray(["14.06.2015", "20.06.2015", "26.06.2015", "10.06.2015", "29.06.2015"]);
        }
        
        ko.bindingHandlers.multiDatepicker = {
            init: function (element, valueAccessor, allBindings) {
                var customDates = allBindings.get("customDates");
        
                valueAccessor()();
                $(element).datepicker(
                    {
                        multidate: true,
                        language: "ru",
                        orientation: "top",
                        beforeShowDay: function (date) {
                            var d = ConvertDateTostring(date, "dd.mm.yyyy");
        
                            if ($.inArray(d, customDates()) != -1) {
                                return {
                                    classes: 'activeClass'
                                };
                            }
                            return;
                        }
                    }
                )
               .bind(
        			'changeDate',
        			function (e) {
        			    var res = e.dates;
        			    var value = [];
        			    res.sort(function (a, b) { return a - b });
        			    for (var i in res) {
        			        value.push(res[i].asString());
        			    }
        
        			    valueAccessor()(value);
        			}
        		);
            },
            update: function (element, valueAccessor, allBindings, bindingContext) {
        
            }
        };
        
        function ConvertDateTostring(date, format) {
            if (!date) {
                return "";
            }
            if (format) {
                return dateFormat(date, format);
            }
            return dateFormat(date, "dd.mm.yy");
        }
         .activeClass{
            background: #32cd32; 
          }
        <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css" rel="stylesheet"/>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
        <input type="text" id="dates" data-bind="customDates: customDates, multiDatepicker: notAppliedDates, value: selectedDatesString">

        【讨论】:

          【解决方案5】:

          使用 datetimepicker 事件 show、update 和 change 需要突出显示日期。

          博文here 解释了如何使用 Bootstrap Datetimepicker 来实现。

          $('#datetimepicker').on('dp.show', function(e){
            highlight()
          })
          $('#datetimepicker').on('dp.update', function(e){
           highlight()
          })
          $('#datetimepicker').on('dp.change', function(e){
              highlight()
          })
          
          function highlight(){
           var dateToHilight = ["04/19/2019","04/20/2019","04/30/2019"];
           var array = $("#datetimepicker").find(".day").toArray();
           for(var i=0;i -1) {
                 array[i].style.color="#090";
                 array[i].style.fontWeight = "bold";
              } 
           }
           }
          

          有关更多信息,请参阅博客

          参考:

          https://sourcecodezoneseven.blogspot.com/2019/04/here-is-code-function-hilight-var.html

          【讨论】:

            【解决方案6】:

            我在上面尝试了 Matias 的答案,但出现了故障。经过一些故障排除并稍微修改了 Matias 的答案,我设法创建了一个可行的解决方案。

            如果您将以下代码粘贴到一个空白的 .html 文件中,那么您应该会看到一个带有日期小部件的输入框。

            开始日期(2021 年 8 月 8 日)、结束日期(2021 年 8 月 26 日)和其间的日期应该有自己独特的配色方案。

            <script
              src="https://code.jquery.com/jquery-3.6.0.js"
              integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
              crossorigin="anonymous"></script>
            
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
            
            
            <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js" integrity="sha256-VBLiveTKyUZMEzJd6z2mhfxIqz3ZATCuVMawPZGzIfA=" crossorigin="anonymous"></script>
            
            
              <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/js/bootstrap-datetimepicker.min.js"></script>
            <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/css/bootstrap-datetimepicker.min.css" rel="stylesheet"></link>
            
            <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
            
            
              <link rel="stylesheet" href="/resources/demos/style.css">
              <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
              <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
            
            
            
            
            
            
            <style>
            .StartDate{background-color:  green;}
            .InBetweenDates{background-color:  darkred;}
            .EndDate{background-color:  blue;}
            </style>
            
            
            
               
            <p>Date: <input type="text" readonly="" id="elementPicker"></p>
                
            <script>
            
            var inRange=false;
            $('#elementPicker').datepicker({
               dateFormat: "yy-mm-dd",
                    changeMonth: true,
                    changeYear: true,
                    minDate: new Date("01/01/2016"),
            
            
            
              beforeShowDay: function(date){
                console.log("date")
                console.log(date)
                dateFormat = date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
            
                console.log("dateFormat")
                console.log(dateFormat)
            
              // 7 means August (0 means January)
              // We are checking to see if the date is 8th August 2021
            
              // Matias had  if (dateFormat == '2014-01-01') but after looking at Console.Log,
              // I removed the leading 0 from the day and month figure. 
            
            
            
                if (dateFormat == '2021-7-8') {
                  console.log("(dateFormat == '2021-7-8')")
                  inRange = true; //to highlight a range of dates
                  
                  // If the day is 8th August 2021 (remember 7 means August not July) 
                  // then we add the CSS class StartDate to the day (look at the top of the code for the StartDate CSS class styling).
                  
                   return [true, 'StartDate', 'tooltipText'];
                }
              
            
            
                if (dateFormat == '2021-7-26') {
                    console.log("(dateFormat == '2021-7-26')")
                
                  inRange = false;  //to stop the highlight of dates ranges
                  
                  // If the day is 26th August 2021 (remember 7 means August not July) 
                  // then we add the CSS class EndDate to the day (look at the top of the code for EndDate CSS class styling).
            
                   return [true, 'EndDate', 'tooltipText'];
                }
            
            
                if (inRange) {
                
            
                  // If the day is between 8th August 2021 - 26th August 2021 then we add the CSS class InBetweenDates to the day (look at the top of the code for the StartDate CSS class styling).
                  
            
                  return [true, 'InBetweenDates', 'tooltipText'];
                 
              } else {
            
            
                // I think this essentially means select the day, do nothing with it and then move on. 
                // Adding this in prevented the error message 'Uncaught TypeError: Cannot read property '0' of undefined jquery datepicker'.
                 return [true];
                
                }
            
              }
            
            });
            
            
            
            </script>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-12-07
              • 1970-01-01
              • 2012-12-11
              • 2019-06-23
              相关资源
              最近更新 更多