jQuery Datatables过滤器

3
我有一个使用日历图像的datatables日期过滤器,效果很好。但是当我清除日期时,它不会显示所有日期。
如何制作一个按钮来显示所有日期并清除日期筛选器?
希望能得到任何关于此问题的帮助/建议,提前感谢您的回复。
// The plugin function for adding a new filtering routine
$.fn.dataTableExt.afnFiltering.push(
        function(oSettings, aData, iDataIndex){
            var dateStart = parseDateValue($("#dateStart").val());
            // aData represents the table structure as an array of columns, so the script access the date value 
            // in the first column of the table via aData[0]
            var evalDate= parseDateValue(aData[3]);

            if (evalDate == dateStart ) {
                return true;
            }
            else {
                return false;
            }

        });

// Function for converting a mm/dd/yyyy date value into a numeric string for comparison (example 08/12/2010 becomes 20100812
function parseDateValue(rawDate) {
    var dateArray= rawDate.split("/");
    var parsedDate= dateArray[1] + dateArray[0] + dateArray[3];
    return parsedDate;
}



$(function() {
    // Implements the dataTables plugin on the HTML table
    var $oTable= $("#example").dataTable( {
        "iDisplayLength": 20,
        "oLanguage": {
            "sLengthMenu": 'Show <select><option value="25">25</option><option value="50">50</option><option value="100">100</option><option value="200">200</option></select>'
        },
        "bJQueryUI": true,
        "aoColumns": [
                null,
                null,
                null,
                { "sType": "date" }
        ]                   
        }); 


    // The dataTables plugin creates the filtering and pagination controls for the table dynamically, so these 
    // lines will clone the date range controls currently hidden in the baseDateControl div and append them to 
    // the feedbackTable_filter block created by dataTables
    $dateControls= $("#baseDateControl").children("div").clone();
    $("#feedbackTable_filter").prepend($dateControls);

    // Implements the jQuery UI Datepicker widget on the date controls
    $('.datepicker').datepicker(
        {dateFormat: 'DD, d MM, yy', showOn: 'button', buttonImage: '../images/calendar.jpg', buttonImageOnly: true}
    );      

    // Create event listeners that will filter the table whenever the user types in either date range box or
    // changes the value of either box using the Datepicker pop-up calendar
    $("#dateStart").keyup ( function() { $oTable.fnDraw(); } );
    $("#dateStart").change( function() { $oTable.fnDraw(); } );

});
1个回答

4

好的,您尝试过以下方法吗:

$.fn.dataTableExt.afnFiltering.push(
        function(oSettings, aData, iDataIndex){
            var dateStart = parseDateValue($("#dateStart").val());
            //if filter is empty return everything
            if(dateStart === ''){
                return true;
            }
            // aData represents the table structure as an array of columns, so the script access the date value 
            // in the first column of the table via aData[0]
            var evalDate= parseDateValue(aData[3]);

            if (evalDate == dateStart ) {
                return true;
            }
            else {
                return false;
            }

        });

如果这不起作用,你能在jsfiddle上发布一个示例吗?
编辑-好的,问题出在parseDateValue()上,它期望使用/创建的日期。由于你想要完全匹配,所以可以省略parseDateValue()
// The plugin function for adding a new filtering routine
$.fn.dataTableExt.afnFiltering.push(
    function(oSettings, aData, iDataIndex){
        var dateStart = $("#dateStart").val();
        //if filter is empty return everything
        if(dateStart === ''){
            return true;
        }
        // aData represents the table structure as an array of columns, so the script access the date value
        // in the first column of the table via aData[0]
        var evalDate= aData[3];

        if (evalDate == dateStart ) {
            return true;
        }
        else {
            return false;
        }

    });

fiddle here http://jsfiddle.net/eMZtV/1/


嗨,感谢回复。我之前尝试过那个方法,但没有成功。这是 jsfiddle 链接:http://jsfiddle.net/eMZtV/ - Codded
非常出色,运行得很好,谢谢。当我在搜索框中输入内容时,如何清除日期?另外,我可以有一个图像来显示所有内容,这将清除日期框。 - Codded
谢谢,虽然它不能在清除输入时更新数据表。 - Codded
所有的工作都非常出色。非常感谢您的时间。 - Codded
这个答案对我帮助很大!谢谢@Nicola Peluchetti - CIRCLE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接