Jquery Datatables Date Range Filter

后端 未结 3 1350
心在旅途
心在旅途 2021-01-15 16:42

The code below is working perfectly but I need to add a new functionality to allow the user to filter on a range of records based on their start date and end date, the user

3条回答
  •  走了就别回头了
    2021-01-15 17:33

    You should be able to put that code anywhere within your javascript after you build the table.

    You just have to change these lines like so:

        $.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
      var iFini = document.getElementById('datepickerStart').value; 
      var iFfin = document.getElementById('datepickerEnd').value; 
      var iStartDateCol = 4; 
      var iEndDateCol = 5; 
    
        iFini=iFini.substring(6,10) + iFini.substring(3,5)+ iFini.substring(0,2);
        iFfin=iFfin.substring(6,10) + iFfin.substring(3,5)+ iFfin.substring(0,2);
    
        var datofini=aData[iStartDateCol].substring(6,10) + aData[iStartDateCol].substring(3,5)+ aData[iStartDateCol].substring(0,2);
        var datoffin=aData[iEndDateCol].substring(6,10) + aData[iEndDateCol].substring(3,5)+ aData[iEndDateCol].substring(0,2);
    
        if ( iFini === "" && iFfin === "" )
        {
            return true;
        }
        else if ( iFini <= datofini && iFfin === "")
        {
            return true;
        }
        else if ( iFfin >= datoffin && iFini === "")
        {
            return true;
        }
        else if (iFini <= datofini && iFfin >= datoffin)
        {
            return true;
        }
        return false;
    }
    );
    

提交回复
热议问题