Highlight certain dates on bootstrap-datepicker

后端 未结 5 508
耶瑟儿~
耶瑟儿~ 2020-12-18 23:06

I\'m using the bootstrap-datepicker to check the days where my website got sales and I want to display or highlight the dates there was at least one sale. I can pass the dat

相关标签:
5条回答
  • 2020-12-18 23:08

    There is simple way to set multiple dates in bootstrap calendar. There is a property multidate which can be used for the selection. find the working code below.

         $('.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)])
    

    Only one problem there the highlights are removed on click. and it take month as one less. if you want August dates then you have to use 7 not 8.

    0 讨论(0)
  • 2020-12-18 23:20

    Using datetimepicker events show, update and change the date need to be highlighted.

    The Blog post here explaining how it can achieve using 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";
        } 
     }
     }
    

    For more information see the blog

    Reference:

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

    0 讨论(0)
  • 2020-12-18 23:27
    $('#xxx').datepicker()
      .on('onRender', function(ev){
        if (ev.date.valueOf() == your date){
          return 'highlight';
        }
      });
    

    Might do the trick, although I am not sure.

    0 讨论(0)
  • 2020-12-18 23:28

    here is another example to highlight a range of dates:

    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
        }
      }
    });
    
    0 讨论(0)
  • 2020-12-18 23:28

    My solution of this problem using Knockout bindings and datepicker property "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">

    0 讨论(0)
提交回复
热议问题