【问题标题】:How to filter jquery data table by date column?如何按日期列过滤jquery数据表?
【发布时间】:2016-11-21 20:39:38
【问题描述】:

我有一个数据表,我试图根据一列中的日期进行过滤。我想根据日期为一年或更早的 lastModified 列过滤数据,但即使在某些硬编码日期上过滤数据也是一个好的开始。字符串格式的数据,所以我尝试使用新的 Date() 函数转换为日期。

var table = $('#database').DataTable( {
        fixedHeader: true,

        data: dataSet,
        columns: [
        { data: "processName" },
        { data: "processLob" },
        { data: "processOwner"},
        { data: "RiskReviewer"},
        { data: "lastModified"}]
        } );

        var filteredData = table
        .column( { data: "lastModified"} )
        .data()
        .filter( function ( value, index ) {
        return new Date(value) < 2015-10-10 ? true : false;
        } );

【问题讨论】:

    标签: javascript datatable datatables datatables-1.10


    【解决方案1】:

    您要做的第一件事是为您的日期列添加“columnDefs”对象并将其类型指定为“日期”。只要您遵循众所周知的格式,DateTables 就会内置日期解析。 ColumnType API Def

    如果这不能让您完全到达那里,那么您将需要在您刚刚创建的新 columnDef 对象上为您的数据列定义一个渲染函数。在那里,您可以检查渲染类型并为其他所有内容返回一个“不错”的显示值和原始数据值(理想情况下是 Date 类型的值)。 Render API Defintion

    还有一些一般性建议,不要试图与图书馆抗争。它实际上非常灵活,可以处理很多事情。所以尽可能使用内置的 API 函数。通常,当人们尝试使用 JQuery 手动操作表时,事情就会出错。在幕后,DataTables 插件维护了大量从未进入 DOM 的状态。基本上,如果 API 中有相应的函数,就使用它。

    编辑:即使他找到了另一个解决方案,也要为原始发帖者的问题添加答案。

    要记住的一点是,“过滤器”仅用于返回过滤后的数据集。它不会改变网格中的显示。相反,您将希望在“column()”API 项上使用“搜索”来过滤 DataTable 中显示的行。

    然而,这有一个小问题。 search 方法只接受常规值而不接受函数。因此,如果您想实现这一点,您可以提供一个自定义搜索功能,如下所示:

    // The $.fn.dataTable.ext.search array is shared amongst all DataTables and 
    //  all columns and search filters are evaluated in the order in which they 
    //  appear in 
    //  the array until a boolean value is returned.  
    $.fn.dataTable.ext.search.unshift(function(settings, data, dataIndex) {
         // Using a negative value to get the column wraps around to the end of 
         // the columns so "-1" will always be your last column.
         var dateColumn = $(this).column(-1);
    
         // We get the data index of the dateColumn and compare it to the index
         // for the column currently being searched.
         if(dateColumn.index() !== dataIndex) {'
            // Pretty sure this indicates to skip this search filter
            return null;
         }
    
         var columnSearchingBy = $(this).column(dataIndex);
    
         // Allows the data to be a string, milliseconds, UTC string format ..etc
         var columnCellData = new Date(data.lastModified);
         var valueToSearchBy = new Date(columnSearchingBy.search.value);
    
         // Ok this is one of the worst named methods in all of javascript.   
         // Doesn't actually return a meaningful time.  Instead it returns the a 
         // numeric value for the number of milliseconds since ~ 1970 I think.  
         //
         // Kind of like "ticks()" does in other languages except ticks are 
         // measured differently.  The search filter I am applying here is to 
         // only show dates in the DataTable that have a lastModified after or 
         // equal the column search.
         return (valueToSearchBy.getTime() >= columnCellData.getTime());
    });
    
    
    // So this should use our fancy new search function applied to our datetime
    // column.  This will filter the displayed values in the DataTable and from 
    // that just a small filter on the table to get all the data for the rows 
    // that satisfy the search filter.
    var filteredData = table
            .column({ data: "lastModified"})
            .search('2015-10-10')
            .draw();
    

    即使您找到了另一种方法来解决这个问题,也许上述内容稍后会帮助您。

    【讨论】:

    • 谢谢阿利斯特!我不知道这个功能,它让事情变得更容易!你知道如何根据这个日期过滤数据吗?
    • nvm 我能够使用日期范围选择器进行过滤。谢谢!
    • @Bilal 我用一个如何使用“搜索”来完成此操作的示例更新了我的答案,因为人们通常希望过滤显示。此外,一旦显示被过滤,从 DataTable 实例中获取过滤后的数据集也很简单。
    • 您也可以查看此插件的 DataTables。我自己不使用它,但它看起来很可靠,并且为搜索 DataTable 提供了一些额外的灵活性。 yadcf-showcase.appspot.com/server_side_source.html
    • 太棒了!谢谢! :)
    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多