2016-03-03 63 views
22

我正在使用jQuery UI日期选择器,我想在日期旁边的日期单元格中显示其他文本。这是所期望的行为:在jQuery UI日期选择器旁边显示其他文本日期选择器

jQuery UI datepicker with custom text inside cells

遗憾的是,操纵使用.text().html()功能日期细胞打破了日期选择器功能。看下面的演示,并尝试使用datepicker。请注意,当您选择一个日期(我)的自定义文本消失(二)改变几个月破坏了日历和土地你“未定义NAN”一个月:

https://jsfiddle.net/salman/aLdx4L0y/

有没有解决方案?

+0

如果你不介意触发_addCustomInformation_功能很多,你可以这样做[这](https://jsfiddle.net/aLdx4L0y/2/) – Lends

回答

16

好了,你可以猴子补丁的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,我认为这是不太可能打破在今后的任何东西比猴子修补

+0

好方法! (我的补丁版本几乎没有打破日历小部件本身的机会,但是如果它们在内部改变某些东西,它当然可能会停止工作。不像这样,真的 - 它默默地认为'td里面总是会有'a' ',我敢打赌这是没有记录在任何地方;)) – Tomalak

+0

@Tomalak *实际上* - 是的,这是真的,jQuery UI并没有真正得到那么多的更新(*尤其是打破*),看起来,完整的交互带有触摸支持(2.0)的重写是永远的,今天我看到一个3岁的bug,它的版本是1.10〜,3年后稳定版本只有1.11〜! –

+0

的确,它在一些地区收集到的不仅仅是一点尘土。 – Tomalak

7

这段代码添加到您的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";}'); //&nbsp; 
     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); 
} 
} 

演示链接... http://jsfiddle.net/pratikgavas/e3uu9/131/

+0

如果你想文本只为选定的日期,那么你可以声明它在cellContents数组...例如var cellContents = {1:'20',15:'60',28:'$ 99.99'};其中1,15,28是日期,20,60,999.99是值 –

11

由于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>

+0

Monkey修补'_generateHTML'函数...为什么我不这么想? –

+1

这实际上是否完全工作?当我点击一个日期并点击右侧或左侧箭头时,它会显示“未定义的NaN”,日期会消失。如果我没有点击日期框,只需点击箭头,一切都很好,月份会发生变化。 – SacWebDeveloper

+0

是的,它的确如此。我在三个浏览器中测试过它。不知道它会在哪里显示“未定义的NaN”吗?请制作一个证明问题的jsFiddle。 – Tomalak

8

有基于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>

+2

非常好,这是一个美丽的解决方案。竖起大拇指。 – Tomalak

+0

想一想,这个方法取决于:a)你只想插入纯文本,从不附加功能(比如第二个链接),b)没有其他'.ui-datepicker td'会有标题。尽管如此,第二个可以通过使用'.ui-datepicker-calendar td [title]'来克服。第一个可能是限制性的。 – Tomalak