我正在使用jQuery UI日期选择器,我想在日期旁边的日期单元格中显示其他文本。这是所期望的行为:在jQuery UI日期选择器旁边显示其他文本日期选择器
遗憾的是,操纵使用.text()
和.html()
功能日期细胞打破了日期选择器功能。看下面的演示,并尝试使用datepicker。请注意,当您选择一个日期(我)的自定义文本消失(二)改变几个月破坏了日历和土地你“未定义NAN”一个月:
https://jsfiddle.net/salman/aLdx4L0y/
有没有解决方案?
我正在使用jQuery UI日期选择器,我想在日期旁边的日期单元格中显示其他文本。这是所期望的行为:在jQuery UI日期选择器旁边显示其他文本日期选择器
遗憾的是,操纵使用.text()
和.html()
功能日期细胞打破了日期选择器功能。看下面的演示,并尝试使用datepicker。请注意,当您选择一个日期(我)的自定义文本消失(二)改变几个月破坏了日历和土地你“未定义NAN”一个月:
https://jsfiddle.net/salman/aLdx4L0y/
有没有解决方案?
好了,你可以猴子补丁的jQuery UI和风险打破在新版本中,也可以使用记录的回调来定制data-*
属性添加到日期选择器,并使用CSS pseudo elements显示它们的代码:
$(function() {
$("#datepicker").datepicker({
beforeShow: addCustomInformation,
//---^----------- if closed by default (when you're using <input>)
beforeShowDay: function(date) {
return [true, date.getDay() === 5 || date.getDay() === 6 ? "weekend" : "weekday"];
},
onChangeMonthYear: addCustomInformation,
onSelect: addCustomInformation
});
addCustomInformation(); // if open by default (when you're using <div>)
});
function addCustomInformation() {
setTimeout(function() {
$(".ui-datepicker-calendar td").filter(function() {
var date = $(this).text();
return /\d/.test(date);
}).find("a").attr('data-custom', 110); // Add custom data here
}, 0)
}
.ui-datepicker .weekend .ui-state-default {
background: #FEA;
}
.ui-datepicker-calendar td a[data-custom] {
position: relative;
padding-bottom: 10px;
}
.ui-datepicker-calendar td a[data-custom]::after {
/*STYLE THE CUSTOME DATA HERE*/
content: '$' attr(data-custom);
display: block;
font-size: small;
}
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
<link href="//code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="//code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<input id="datepicker">
这里有一个更新的JSFiddle
附注:我不知道它的功能是在您的演示打破了,但由于该解决方案是使用记录的回调和CSS,我认为这是不太可能打破在今后的任何东西比猴子修补
这段代码添加到您的JS ......
$('#DatePicker').datepicker({
changeMonth: true,
changeYear: true,
minDate: 0,
onSelect: function (date, dp) {
updateDatePickerCells();
},
onChangeMonthYear: function(month, year, dp) {
updateDatePickerCells();
},
beforeShow: function(elem, dp) {
updateDatePickerCells();
}});
updateDatePickerCells();
function updateDatePickerCells(dp) {
setTimeout(function() {
var cellContents = {1: '20', 15: '60', 28: '$99.99'};
$('.ui-datepicker td > *').each(function (idx, elem) {
var value = '$125';//cellContents[idx + 1] || 0;
var className = 'datepicker-content-' + CryptoJS.MD5(value).toString();
if(value == 0)
addCSSRule('.ui-datepicker td a.' + className + ':after {content: "\\a0";}'); //
else
addCSSRule('.ui-datepicker td a.' + className + ':after {content: "' + value + '";}');
$(this).addClass(className);
});
}, 0);
}
var dynamicCSSRules = [];
function addCSSRule(rule) {
if ($.inArray(rule, dynamicCSSRules) == -1) {
$('head').append('<style>' + rule + '</style>');
dynamicCSSRules.push(rule);
}
}
如果你想文本只为选定的日期,那么你可以声明它在cellContents数组...例如var cellContents = {1:'20',15:'60',28:'$ 99.99'};其中1,15,28是日期,20,60,999.99是值 –
由于jQuery UI的datapicker很整体,很难清洁增强。
我会去猴子修补其内部功能之一,即_generateHTML
。
$.datepicker._generateHTML = (function() {
var realGenerateHtml = $.datepicker._generateHTML;
return function (instance) {
var html = realGenerateHtml.apply(this, arguments), $temp;
if (instance.input.is(".datepicker-price")) {
$temp = $("<table></table>").append(html);
$temp.find(".ui-datepicker-calendar td a").each(function() {
var yy = $(this).parent().data("year"),
mm = $(this).parent().data("month"),
dd = +$(this).text();
$(this).append("<br><small class='price'>$100</small>");
});
html = $temp[0].innerHTML;
}
return html;
};
})();
$(function() {
$("#datepicker").datepicker();
});
.ui-datepicker .weekend .ui-state-default {
background: #FEA;
}
.price {
color: blue;
}
<link href="https://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>
<div id="datepicker" class="datepicker-price"></div>
Monkey修补'_generateHTML'函数...为什么我不这么想? –
这实际上是否完全工作?当我点击一个日期并点击右侧或左侧箭头时,它会显示“未定义的NaN”,日期会消失。如果我没有点击日期框,只需点击箭头,一切都很好,月份会发生变化。 – SacWebDeveloper
是的,它的确如此。我在三个浏览器中测试过它。不知道它会在哪里显示“未定义的NaN”吗?请制作一个证明问题的jsFiddle。 – Tomalak
有基于the accepted answer一个更简单的解决方案:
的beforeShowDay
功能允许我们设置每个日期“标题”属性。我们可以使用CSS伪元素来显示属性。
$(function() {
var dayrates = [100, 150, 150, 150, 150, 250, 250];
$("#datepicker").datepicker({
beforeShowDay: function(date) {
var selectable = true;
var classname = "";
var title = "\u20AC" + dayrates[date.getDay()];
return [selectable, classname, title];
}
});
});
@import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css");
.ui-datepicker td span,
.ui-datepicker td a {
padding-bottom: 1em;
}
.ui-datepicker td[title]::after {
content: attr(title);
display: block;
position: relative;
font-size: .8em;
height: 1.25em;
margin-top: -1.25em;
text-align: right;
padding-right: .25em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<div id="datepicker"></div>
如果你不介意触发_addCustomInformation_功能很多,你可以这样做[这](https://jsfiddle.net/aLdx4L0y/2/) – Lends