数据表两个日期范围在两列中过滤

时间:2016-12-10 11:27:11

标签: php jquery filter datatables date-range

我有一个包含两个日期列的表我可以在一列中使用日期范围,这是aData [3]

$.fn.dataTableExt.afnFiltering.push(
    function(oSettings, aData, iDataIndex){
        var dateStart = parseDateValue($("#fromDate").val());
        var dateEnd = parseDateValue($("#toDate").val());
        var evalDate= parseDateValue(aData[3]);
        if ( evalDate >= dateStart && evalDate <= dateEnd) {
            return true;
        }
        else {
                return false;
        }

 });

 function parseDateValue(rawDate) {
     var dateArray= rawDate.split("-");
     var parsedDate= dateArray[2] + dateArray[1] + dateArray[0];
     return parsedDate;
 }

var table = $('#data').DataTable({
  "lengthMenu": [[25, 50, -1], [25, 50, "All"]],
  "order": [[ 0, "desc" ]],
  "paging" : true,
  "scrollX": true,
  dom: 'Bfrtip',
  buttons: [
    'excel', 'print'
  ]
});

 $("#fromDate").keyup ( function() { table.draw(); } );
 $("#fromDate").change( function() { table.draw(); } );
 $("#toDate").keyup ( function() { table.draw(); } );
 $("#toDate").change( function() { table.draw(); } );

现在我想添加另一个日期列,它将是aData [5],我想为它添加另一个日期范围过滤器。如何使用其他日期选择器(#Dast2和#toDate2)在aData [5]中搜索,使用两个数据范围过滤器过滤表格?

1 个答案:

答案 0 :(得分:0)

我创建了单选按钮来选择要过滤的列

<td><input type="radio" name="filterdate" value="birth_date"> birth date </td>
<td><input type="radio" name="filterdate" value="register_date"> register date </td>

$("input[name=filterdate]").change(function () {
    var d = $("input[name=filterdate]:checked").val();

    if(d == 'birth_date'){
        $.fn.dataTableExt.afnFiltering.push(
            function(oSettings, aData, iDataIndex){
                var dateStart = parseDateValue($("#Bdate_From").val());
                var dateEnd = parseDateValue($("#Bdate_To").val());
                var evalDate= parseDateValue(aData[3]);
                if ( evalDate >= dateStart && evalDate <= dateEnd) {
                    return true;
                }
                else {
                    return false;
                }
            }
        );
    }
    else{
        $.fn.dataTableExt.afnFiltering.push(
            function(oSettings, aData, iDataIndex){
                var dateStart = parseDateValue($("#regdate_From").val());
                var dateEnd = parseDateValue($("#regdate_To").val());
                var evalDate= parseDateValue(aData[8]);
                if ( evalDate >= dateStart && evalDate <= dateEnd) {
                    return true;
                }
                else {
                    return false;
                }
            }
        );
    }
});

function parseDateValue(rawDate) {
    var dateArray= rawDate.split("-");
    var parsedDate= dateArray[2] + dateArray[1] + dateArray[0];
    return parsedDate;
}

var table = $('#data').DataTable({
    "lengthMenu": [[25, 50, -1], [25, 50, "All"]],
    "order": [[ 0, "desc" ]],
    "paging" : true,
    "scrollX": true,
    dom: 'Bfrtip',
    buttons: [
        'excel', 'print'
    ],
    responsive: true
});