首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery DataTables yadcf大于-等于(日期)

jQuery DataTables yadcf大于-等于(日期)
EN

Stack Overflow用户
提问于 2016-03-28 22:08:20
回答 1查看 1.4K关注 0票数 0

我正在使用jQuery YADCF插件过滤出一列格式化为mm的日期。插件的工作原理非常出色,只是它只显示了日期上的精确匹配。如果确切的排在桌子上的话,它会显示给我2016年03年/06/2016,但我真的希望它能显示2016年06月03/06和之后的所有日期。

我一直在尝试用DataTables手动完成这个任务,而且没有一个真正的工作或文档良好的jQuery DataTables版本,可以进行“大于等于”的日期搜索。

是否有人用YADCF (或DataTables单人)成功地做到了这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-04 20:34:50

这里我的用例是YADCF的一个边缘案例,所以我重新使用了来自http://codepen.io/markmichon/pen/tmeGD的一些代码来使我的过滤器工作。这次我没有用YADCF,但很高兴知道它在外面。大吼大吼丹尼尔,插件作者,对我的回应!!

代码语言:javascript
复制
// converts date strings to a Date object, then normalized into a YYYYMMMDD format (ex: 20131220). Makes comparing dates easier. ex: 20131220 > 20121220
var normalizeDate = function(dateString) {
    var date = new Date(dateString);
    var normalized = date.getFullYear() + '' + (("0" + (date.getMonth() + 1)).slice(-2)) + '' + ("0" + date.getDate()).slice(-2);
    return normalized;
}

var filterByDate = function(column, vesselDate) {
// Custom filter syntax requires pushing the new filter to the global filter array
    $.fn.dataTableExt.afnFiltering.push(
        function( oSettings, aData, iDataIndex ) {
            var rowDate = (aData[column]).replace(/<(?:.|\n)*?>/gm, '').match(/[0-9][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]/), // take my mm-dd-yyyy – timestamp format wrapped in hTML and strip it down to the mm-dd-yyy only
          start = normalizeDate(vesselDate); // use a normalized date (see code below)
          rowDate = normalizeDate(rowDate);
      // If our date from the row is between the start and end
      if (start <= rowDate) {
        return true;
      } else if (rowDate >= start && start !== ''){
        return true;
      } else {
        return false;
      }
    }
    );
};

$('#dateStart').on('change keyup', function(){
    dateStart = $(this).val();
    filterByDate(3, dateStart); //passing the column value to the function
    $vesselSchedule.draw(); //updating the table
});

$('#dateEnd').on('change keyup', function(){
    dateEnd = $(this).val();
    filterByDate(4, dateEnd);
    $vesselSchedule.draw();
});

https://www.nwseaportalliance.com/operations/vessels#/是一个活的例子。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36272076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档