By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Add multiple range filters on Jquery Data table
JavaScript

Javascript – Add multiple range filters on Jquery Data table

Admin
Last updated: 2023/12/20 at 4:36 PM
Admin
Share
5 Min Read
Add multiple range filters on Jquery Data table

Problem:

I have a scenario where I want to apply multiple range filters on a jquery data table. Like, first filter is for filtering between certain range of days for which I have dropdown containing certain days values and another one will search between certain selected dates from the date picker. I’m using $.fn.dataTable.ext.search.push(function(settings, data, dataIndex)) to push the range of values for searching in table. These both filters are working fine together until I clear any one of it. I am using $.fn.dataTable.ext.search.pop() to pop the applied filters but it is all the search results and not just the specific one.
How can i solve this issue?
Here is my code:

Contents
Problem:Solution:
  1. For filtering between days range:
var filterTableByRange = (searchUptoDays, columnIndex) => {

    if(searchUptoDays) {
        $.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {
            var min = 0;
            var max = parseInt(searchUptoDays);
            var totalTransDays = parseInt(data[columnIndex]) || 0; // use data for the age column

            if ( ( isNaN( min ) && isNaN( max ) ) ||
               ( isNaN( min ) && totalTransDays <= max ) ||
               ( min <= totalTransDays   && isNaN( max ) ) ||
               ( min <= totalTransDays   && totalTransDays <= max ) )
            {
              return true;
            }
            return false;
        });

        homePageDataTable.draw();

    } else {

        var dateFrom = $('#' + portlet_namespace + 'dateFrom').val();
        var dateTo = $('#' + portlet_namespace + 'dateTo').val();

        if(dateFrom && dateTo) {
            filterTablesWithinDates();
        }

        $.fn.dataTable.ext.search.pop();
        homePageDataTable.draw();

    }

};
  1. For filtering between Dates:
var filterTablesWithinDates = () => {

    var dateFrom = $('#' + portlet_namespace + 'dateFrom').val();
    var dateTo = $('#' + portlet_namespace + 'dateTo').val();

    if(dateFrom === '' || dateTo === '') {
        $.fn.dataTable.ext.search.pop();

        var filterByDays = $('#' + portlet_namespace + 'filter_by_days').val();

        if(filterByDays) {
            filterTableByRange(filterByDays, 10);
        }

        homePageDataTable.draw();

        return;
    }

    var min = new Date(dateFrom);
    var max = new Date(dateTo);

    if(min > max) {
        alert('Date From must be smaller than Date To.');
        return;
    }

    $.fn.dataTable.ext.search.push(function( settings, data, dataIndex ) {

        let date = new Date(data[3]); // use data for the age column

        if (min <= date && date <= max) {
            return true;
        }
        return false;

    });

    homePageDataTable.draw();

};

Solution:

In order to manage multiple filters, you need to keep track of which filters are currently active and pop only the specific filter that needs to be removed. Here is an example code for doing this:

// Define an array to store the filter functions
var filterFunctions = [];

var filterTableByRange = (searchUptoDays, columnIndex) => {
    // Remove any existing filter functions for this filter
    filterFunctions = filterFunctions.filter(fn => fn !== filterByRange);

    if (searchUptoDays) {
        filterFunctions.push(filterByRange(searchUptoDays, columnIndex));
    }

    applyFilters();
};

// Function to create the filter function for days range
var filterByRange = (searchUptoDays, columnIndex) => {
    return function(settings, data, dataIndex) {
        var min = 0;
        var max = parseInt(searchUptoDays);
        var totalTransDays = parseInt(data[columnIndex]) || 0;

        if ((isNaN(min) && isNaN(max)) ||
            (isNaN(min) && totalTransDays <= max) ||
            (min <= totalTransDays && isNaN(max)) ||
            (min <= totalTransDays && totalTransDays <= max)) {
            return true;
        }
        return false;
    };
};

var filterTablesWithinDates = () => {
    // Remove any existing filter functions for this filter
    filterFunctions = filterFunctions.filter(fn => fn !== filterWithinDates);

    var dateFrom = $('#' + portlet_namespace + 'dateFrom').val();
    var dateTo = $('#' + portlet_namespace + 'dateTo').val();

    if (dateFrom && dateTo) {
        filterFunctions.push(filterWithinDates(dateFrom, dateTo));
    }

    applyFilters();
};

var filterWithinDates = (dateFrom, dateTo) => {
    return function(settings, data, dataIndex) {
        let date = new Date(data[3]);

        if (min <= date && date <= max) {
            return true;
        }
        return false;
    };
};

var applyFilters = () => {
    // Remove any previously added search functions
    $.fn.dataTable.ext.search.pop();

    // Push all active filter functions into the DataTable
    filterFunctions.forEach(fn => {
        $.fn.dataTable.ext.search.push(fn);
    });

    // Draw the table
    homePageDataTable.draw();
};

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Is dynamic import useful in node.js? Javascript – Is dynamic import useful in node.js?
Next Article The pokédex that I can't continue [closed] Javascript – The pokédex that I can’t continue [closed]
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?