javascript - today - jquery ui widget js
在jQuery UI datepicker中顯示日期旁邊的其他文本 (3)
我正在使用jQuery UI datepicker,我想在日期旁邊的日期單元格中顯示其他文本。 這是期望的行為:
不幸的是,使用.text()
和.html()
函數操作日期單元會破壞日期選擇器功能。 請參閱以下演示並嘗試使用datepicker。 請注意,當您選擇日期時(i)自定義文本消失(ii)更改月份會破壞日曆並讓您登錄“未定義的NaN”月份:
https://jsfiddle.net/salman/aLdx4L0y/
有解決方案嗎?
好吧,你可以修補jQuery UI並冒險破壞新版本的代碼,或者你可以使用文檔化的回調將自定義data-*
屬性添加到datepicker並使用CSS偽元素顯示它們:
$(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);
}
}
由於jQuery UI數據採集器非常單一,因此很難乾淨地增強。
我會選擇猴子修補其內部函數之一,即_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>