在jquery datepicker上突出显示多个日期

时间:2015-01-07 16:23:35

标签: javascript jquery arrays jquery-ui-datepicker

如果我尝试通过提供静态数组来突出显示jquery datepicker上的日期。它工作得非常好。如果我在循环内生成数组然后提供它。我没有办法让日期突出显示。代码如下。

  // var tempDates=["2015/01/07","2015/01/14","2015/01/21"];
                var tempDates = []; tempDates=datesToHighlight;


                line.datepicker({disabled:false, defaultDate: dateObj, dateFormat: 'yy/mm/dd',
                    beforeShowDay: function (highlightMe) {
                        var dateString = jQuery.datepicker.formatDate('yy/mm/dd', highlightMe);
                        var highlightIndex = $.inArray(dateString.toString(), tempDates);
                        if(highlightIndex>-1) {
                            return [true, 'highlight', tips[highlightIndex]];
                        }
                        var aIndex = $.inArray(dateString, selected);
                        return [aIndex == -1]
                    }
                });

这是循环生成日期的格式。

 2015/01/07
 2015/01/14
 2015/01/21
 2015/01/28
 2015/02/04
 2015/02/11
 2015/02/18

我用来生成数组的循环。

for (var k = 0; k < selectedArrayLength; k++) {
                    if (new Date(selected[k]).getMonth() == monthIndex[ind%12]) {

                        var dateFormat = new Date(selected.splice(k, 1));
                        var curr_date = addZ(dateFormat.getDate());
                        var curr_month = dateFormat.getMonth();
                        curr_month++;
                        var curr_year = dateFormat.getFullYear();
                        var newFormat = (curr_year + "/" + addZ(curr_month) + "/" + curr_date);
                        thisMonthDates.push(newFormat);
                        //thisMonthDates.push(selected.splice(k, 1));
                        k--; // since we removed an element we need to decrement k
                    }
                }


  for(var eachDt=0; eachDt<thisMonthDates.length; eachDt++) {
                    console.log(thisMonthDates[eachDt]);
                    var datesToHighlight = new Array();
                    datesToHighlight.push(new Date(thisMonthDates[eachDt]));

                }

代码记录。

Year Difference: 2 end year: 2015 start year: 2015
78 Difference of days.
 12 : Selected Array Length 
 calendar0
 4
 2015/01/07
 2015/01/14
 2015/01/21
 2015/01/28
 4
 ["2015/01/07", "2015/01/14", "2015/01/21", "2015/01/28"]
 calendar1
 4
 2015/02/04
 2015/02/11
 2015/02/18
 2015/02/25
 4
 ["2015/02/04", "2015/02/11", "2015/02/18", "2015/02/25"]
 calendar2
 4
 2015/03/04
 2015/03/11
 2015/03/18
 2015/03/25
 4
 ["2015/03/04", "2015/03/11", "2015/03/18", "2015/03/25"]
 SELECTED ARRAY IS EMPTY.
 0,1,2 - 2015,2015,2015

1 个答案:

答案 0 :(得分:0)

尝试更新for循环以创建datesToHighlight。您不需要为每个thisMonthDates项创建日期对象,并且您每天都在重新创建一个新数组,因此我将datesToHighlight初始化移到了循环之外。

var datesToHighlight = [];
for(var eachDt=0; eachDt<thisMonthDates.length; eachDt++) {
    console.log(thisMonthDates[eachDt]);
    datesToHighlight.push(thisMonthDates[eachDt]);
}