0

我正在使用 jQuery datepicker 结合同位素插件过滤器功能来过滤掉依赖于日期的 div。每个.blocksdiv 都有通过data-value允许您过滤 div 来分配的日期。例如,第一个实例.blocks有 3 个日期1782013 1882013 1982013。我希望单独设置这些日期的样式,即data-value属性中日期的每个实例,我希望在其中分配一个单独的类,#datepicker以便它们可以设置不同的样式。
jsfiddle 演示:http: //jsfiddle.net/neal_fletcher/9WnYY/
HTML:

<div id="datepicker"></div>
<div id="block-wrap">
    <div class="blocks" data-value="1782013 1882013 1982013">17/08 — 19/08</div>
    <div class="blocks" data-value="2182013 2282013 2382013">21/08 — 23/08</div>
    <div class="blocks" data-value="2582013 2682013 2782013 2882013 2982013">25/08 — 29/08</div>    
</div>

jQuery:

var $container = $('#block-wrap');
var $blocks = $("div.blocks", "#block-wrap");

$(function () {
    $('#datepicker').datepicker({
        inline: true,
        //nextText: '&rarr;',
        //prevText: '&larr;',
        showOtherMonths: true,
        //dateFormat: 'dd MM yy',
        dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
        //showOn: "button",
        //buttonImage: "img/calendar-blue.png",
        //buttonImageOnly: true,
        onSelect: function (dateText, inst) {
            var date = new Date(dateText);
            var dateValue = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();

            $container.isotope({
                filter: '[data-value~="' + dateValue + '"]'
            });
        }
    });
});

$container.imagesLoaded(function () {
    $container.isotope({
        itemSelector: '.blocks',
        animationEngine: 'css',
        masonry: {
            columnWidth: 5
        }
    });
});

var prepareData = function (howLong) {
    var mode = howLong,
        date = new Date(),
        days = 0,
        d = date.getDate(),
        m = date.getMonth(),
        y = date.getFullYear(),
        duration = [],
        durationReady = [];

    if (mode === "week") {
        days = 7;
    } else if (mode === "month") {
        days = 30;
    }

    for (i = 0; i < days; i++) {
        // for each day create date objects in an array
        duration[i] = new Date(y, m, d + i);

        // convert objects into strings
        // fe. 25th of May becomes '2552013'
        durationReady[i] = duration[i].getDate().toString() + (duration[i].getMonth() + 1).toString() + duration[i].getFullYear().toString();

        // 1. select all items which contain given date
        var $applyMarkers = $('.blocks[data-value~="' + durationReady[i] + '"]')
            .each(function (index, element) {
            var thisElem = $(element),
                thisElemAttr = thisElem.attr('data-value');
            // 2. for each item which does not contain a marker yet, apply one    
            if (thisElemAttr.indexOf(mode.substring(0, 1)) === -1) {
                thisElem.attr('data-value', (thisElemAttr += ' ' + mode));
            }
        });
    }
};

prepareData("week");
prepareData("month");

$("#today").on("click", function () {
    var date = new Date();
    var dateValue = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();

    $('#datepicker').datepicker('setDate', date);

    $container.isotope({
        filter: '[data-value~="' + dateValue + '"]'
    });
});


$("#week").on("click", function () {
    $container.isotope({
        filter: '[data-value ~="week"]'
    });
});

$("#month").on("click", function () {
    $container.isotope({
        filter: '[data-value ~="month"]'
    });
});

因此是否可以收集存储在data-value属性中的所有日期,将它们存储为变量并通过beforeshowday函数传递它们以分别设置它们的样式?如果那是可能的?任何建议将不胜感激!

4

1 回答 1

0

您可以查看showBeforeDay选项

$(function () {
    var blocks = $('#block-wrap .blocks')
    $('#datepicker').datepicker({
        inline: true,
        //nextText: '&rarr;',
        //prevText: '&larr;',
        showOtherMonths: true,
        //dateFormat: 'dd MM yy',
        dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
        //showOn: "button",
        //buttonImage: "img/calendar-blue.png",
        //buttonImageOnly: true,
        onSelect: function (dateText, inst) {
            var date = new Date(dateText);
            var dateValue = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();

            $container.isotope({
                filter: '[data-value~="' + dateValue + '"]'
            });
        },
        beforeShowDay: function(date){
            var target = $.datepicker.formatDate( 'ddmmyy', date);
            var contains = blocks.filter('[data-value*="' + target + '"]').length > 0;
            return [true, contains ? 'special' : '', '']
        }
    });
});

演示:小提琴

注意:正如我在评论中所说,我对data-value格式进行了轻微更改,日期格式更改为ddmmyyyy

于 2013-08-20T03:21:41.087 回答