我正在使用jQuery ui datepicker结合同位素插件和过滤器功能来按日期过滤掉div。要使此过滤器功能起作用,日期已转换为字符串并规定了所有零,例如20/08/2013
将显示为2082013
。我还使用beforeShowDay
功能突出显示日期选择器中的相关日期,这就是问题所在,在我的示例中,因为我使用此日期格式1782013 1882013 1982013
也是样式{ {1}}等(正如你在小提琴中看到的那样)
jsFiddle:http://jsfiddle.net/neal_fletcher/jPzK2/1/
jQuery的:
01/08/2013
正如您所看到的,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"]'
});
});
中的所有日期都在日期选择器中成功设置了风格,但由于日期格式,它也在8月1日至9日期间进行了样式设置。无论如何,过滤器功能和beforeShowDay功能都可以在没有这个问题的情况下工作吗?
&安培;是的,将日期格式更改为例如data-value
确实解决了21082013
样式问题,但打破了过滤功能,它们都需要工作。
任何建议都将不胜感激!
答案 0 :(得分:0)
您在beforeShowDay
功能
('[data-value*="' + target + '"]')
这就是8月1日的风格 - 因为它符合“2182013”的价值。 将其更改为单词选择器〜
('[data-value~="' + target + '"]')