gpt4 book ai didi

javascript - DataTables 标题中的 jQuery UI Datepicker 没有产生?

转载 作者:行者123 更新时间:2023-11-29 19:46:02 25 4
gpt4 key购买 nike

我的数据表标题中有日期选择器。我需要两件事。

第一,在表格外工作的完全相同的日期选择器将不会在标题内工作(或包括日历图标)。我也尝试过通过 DOM 实现这些,但仍然没有用,而且这种方式看起来不那么困惑。

第二,这些日期选择器应该充当表格的过滤器。因此,从 10 月 2 日到 10 月 4 日应该隐藏该括号之外的所有日期。有任何想法吗?提前致谢。

http://jsfiddle.net/Z85QC/12/

jQuery

$(".datepick").datepicker({
showOn: "both",
buttonImage: "http://www.effinghamparkdistrict.org/graphics/calendar_icon.png"
});

$('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aLengthMenu": [
[5, 10, 15, 20, -1],
[5, 10, 15, 20, "All"]
],
"iDisplayLength": 10
});

$("<div class='nBreak padR'><label>Date Filter:</label>&nbsp;&nbsp;From <input type='text' class='datepick' />&nbsp;To <input type='text' class='datepick' /></div>").prependTo('div.dataTables_filter');

最佳答案

var oTable = $('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aLengthMenu": [
[5, 10, 15, 20, -1],
[5, 10, 15, 20, "All"]
],
"iDisplayLength": 10
});

//added id's to text inputs to aid in attaching event listeners on date selects
$("<div class='nBreak padR'><label>Date Filter:</label>&nbsp;&nbsp;From <input id='min' type='text' class='datepick' />&nbsp;To <input id='max' type='text' class='datepick' /></div>").prependTo('div.dataTables_filter');

//datepicker initialization move to AFTER creation of the datatable so plugin could do it's thing
$(".datepick").datepicker({
showOn: "both",
buttonImage: "http://www.effinghamparkdistrict.org/graphics/calendar_icon.png"
});

//datatable documentation to push custom filtering functions
$.fn.dataTableExt.afnFiltering.push(
function(oSettings, aData, iDataIndex){
var iMin = document.getElementById('min').value;
var iMax = document.getElementById('max').value;

if(iMin == null || iMin == "") {
return true;
}
if(iMax == null || iMax == "") {
return true;
}

var minDate = new Date(iMin);
var maxDate = new Date(iMax);

var date = new Date(aData[1]); //column index 1 contains dates

if( minDate <= date && date <= maxDate){
return true;
}
return false;
}
);

//Added events for listening to datepicker selects that will trigger the table to redraw and run the custom filtering
$('#min').datepicker("option","onSelect",function(dateString){
oTable.fnDraw();
});

$('#max').datepicker("option","onSelect",function(dateString){
oTable.fnDraw();
});

这是更改(在代码中注释),基本上您需要:

  • 在网格创建之后移动日期选择器初始化,这样它的标题中的日期选择器被创建。
  • 数据网格中的日期选择器被赋予了 id,所以我们可以很容易地将听众附加到他们身上。
  • 数据表的文档允许用户定义自定义使用 afnFiltering.push 调用过滤规则( http://datatables.net/release-datatables/examples/plug-ins/range_filtering.html )
  • 最后,我们将 onselect 监听器附加到数据网格datepickers 以便重新绘制表格并运行自定义过滤功能。

所有这些都在这里演示:http://jsfiddle.net/RP6Wn/

关于javascript - DataTables 标题中的 jQuery UI Datepicker 没有产生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19666590/

25 4 0
文章推荐: android - 在构建签名的 apk ExternalSystemException : String index out of range: -97 时出错
文章推荐: javascript - 未捕获的类型错误 : Object # has no method "leanModal"