0

我将 jQuery ui datepicker 与 isotope 插件和过滤器功能结合使用,以按日期过滤掉 div。为了使此过滤器功能起作用,日期已转换为字符串并删除了所有零,例如20/08/2013将读取为2082013. 我还使用该beforeShowDay功能突出显示日期选择器中的相关日期,这就是问题所在,在下面的示例中,因为我使用的日期格式1782013 1882013 1982013也是样式01/08/2013等(如您在小提琴中看到的)。
jsFiddle: http : //jsfiddle.net/neal_fletcher/jPzK2/1/

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

$(function () {
    var blocks = $('#block-wrap .blocks')
    $('#datepicker').datepicker({
        inline: true,
        //nextText: '→',
        //prevText: '←',
        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 = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();
            var contains = blocks.filter('[data-value*="' + target + '"]').length > 0;
            return [true, contains ? 'special' : '', '']
        }
    });
});

$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日期选择器中的所有日期都已成功设置样式,但由于日期格式的原因,它也设置了 8 月 1 日至 9 日的样式。无论如何,过滤器功能和 beforeShowDay 功能都可以在没有这个问题的情况下工作吗?
是的,将日期格式更改为 eg21082013确实可以解决beforeShowDay样式问题,但会破坏过滤器功能,它们都需要工作。
任何建议将不胜感激!

4

1 回答 1

0

您在beforeShowDay函数中使用 * 选择器

('[data-value*="' + target + '"]')

这就是 8 月 1 日的样式 - 因为它匹配“2182013”​​值。改成单词选择器~

('[data-value~="' + target + '"]')
于 2013-08-21T12:14:33.410 回答