为了更准确地说,我需要在Jquery 上显示一些日期上的自定义文本,以便为某些日期提供一个特定的价格。我的想法是使用beforeShowDay在这些日期的标题中附加特定的价格,然后在beforeShow中检查每个td并将标题的文本放入单元格中。示例:
var m, d, y, checkDate, specificPrice = '';
var specificPrices = {"2013-3-8":"300 EUR", "2013-2-26":"263 EUR"}
$('#my-datepicker').datepicker({
beforeShowDay: function checkAvailable(date) {
m = date.getMonth();
d = date.getDate();
y = date.getFullYear();
checkDate = y + '-' + (m+1) + '-' + d;
if(specificPrices[checkDate]){
specificPrice = specificPrices[checkDate];
}else{
specificPrice = '';
}
return [true, "", specificPrice];
},
beforeShow: function(elem, inst){
$('table.ui-datepicker-calendar tbody td', inst.dpDiv).each(function(){
var calendarPrice = $(this).attr('title');
if(calendarPrice != undefined){
$(this).find('a').append('<span class="calendar-price">' + calendarPrice + '<span>');
}
});
}
});这将在第一个日历呈现上呈现价格(在月/年更改之前),并且仅用于内联日历。
我需要价格显示在非内联日历和任何月/年的变化以及。。
我尝试了一些其他的变体,并尝试使用onChangeMonthYear,但到目前为止没有成功。
谢谢您的关注,欢迎您的意见。
发布于 2014-04-02 20:25:04
我遇到了同样的情况,并认为我会发布我的解决方案的内联数据,但也应该工作一个弹出数据报警器。
首先,您不能修改由datepicker插件创建的表单元格的内容。这样做将破坏其核心功能,因为它读取单元格内容以生成选定的日期字符串。因此,必须使用纯css添加内容。
创建数据报警器
$('#DatePicker').datepicker({
changeMonth: true,
changeYear: true,
minDate: 0,
//The calendar is recreated OnSelect for inline calendar
onSelect: function (date, dp) {
updateDatePickerCells();
},
onChangeMonthYear: function(month, year, dp) {
updateDatePickerCells();
},
beforeShow: function(elem, dp) { //This is for non-inline datepicker
updateDatePickerCells();
}
});
updateDatePickerCells();创建插入单元格内容的方法
function updateDatePickerCells(dp) {
/* Wait until current callstack is finished so the datepicker
is fully rendered before attempting to modify contents */
setTimeout(function () {
//Fill this with the data you want to insert (I use and AJAX request). Key is day of month
//NOTE* watch out for CSS special characters in the value
var cellContents = {1: '20', 15: '60', 28: '100'};
//Select disabled days (span) for proper indexing but apply the rule only to enabled days(a)
$('.ui-datepicker td > *').each(function (idx, elem) {
var value = cellContents[idx + 1] || 0;
/* dynamically create a css rule to add the contents with the :after
selector so we don't break the datepicker functionality */
var className = 'datepicker-content-' + value;
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);
}添加一个方法来动态创建新的CSS规则,同时跟踪已经创建的规则。
var dynamicCSSRules = [];
function addCSSRule(rule) {
if ($.inArray(rule, dynamicCSSRules) == -1) {
$('head').append('<style>' + rule + '</style>');
dynamicCSSRules.push(rule);
}
}最后,一些新单元格内容的默认css
.ui-datepicker td a:after
{
content: "";
display: block;
text-align: center;
color: Blue;
font-size: small;
font-weight: bold;
} 这适用于基本内容,但是如果您想喜欢'$20.95‘之类的东西(包含CSS特殊字符),可以使用内容的md5散列来创建className变量。
编辑 JSFiddle,从@yuga的JSFiddle中修改,为更复杂的内容包括唯一类名的MD5哈希。
发布于 2016-09-22 21:48:10
基于@PrestonS的答案和这个职位,我有一个很好的简单解决方案,不需要向头添加<style>标记。
我的解决方案更新Preston的如下内容:
Modified**updateDatePickerCells** 函数:
function updateDatePickerCells(dp) {
/* Wait until current callstack is finished so the datepicker
is fully rendered before attempting to modify contents */
setTimeout(function () {
//Fill this with the data you want to insert (I use and AJAX request). Key is day of month
//NOTE* watch out for CSS special characters in the value
var cellContents = {1: '20', 15: '60', 28: '100'};
//Select disabled days (span) for proper indexing but apply the rule only to enabled days(a)
$('.ui-datepicker td > *').each(function (idx, elem) {
var value = cellContents[idx + 1] || 0;
/***** MAGIC! *****/
$(this).attr('data-content', value);
// and that's it! (oh, so easy)
});
}, 0);
}使用此解决方案,您根本不需要addCSSRule函数。
修改的css:
/* to target all date cells */
.ui-datepicker td > *:after {
content: attr(data-content); /***** MAGIC! *****/
/* add your other styles here */
}
/* to target only allowed date cells */
.ui-datepicker td > a:after {
}
/* to target only disabled date cells */
.ui-datepicker td > span:after {
}用数据报头对象初始化
如果您需要updateDatePickerCells函数中的datepicker对象,下面是在初始化时获得它的一种方法。(基于这个职位)
var calendarElem = $('#DatePicker').datepicker({
changeMonth: true,
changeYear: true,
minDate: 0,
//The calendar is recreated OnSelect for inline calendar
onSelect: function (date, dp) {
updateDatePickerCells( dp );
},
onChangeMonthYear: function(month, year, dp) {
updateDatePickerCells( dp );
},
beforeShow: function(elem, dp) { //This is for non-inline datepicker
updateDatePickerCells( dp );
}
});
var datepicker = $.datepicker._getInst(calendarElem[0]);
updateDatePickerCells(datepicker);https://stackoverflow.com/questions/14959709
复制相似问题