I have a large table something like this below. I want to filter the table by Office and Month. I was thinking to do this I would some how use .indexOf() twice. I tried to do this in different ways and did not figure it out.
Office | Name | Month | Sales |
---|---|---|---|
1a | Abe D | Jan | 10 |
1a | Abe D | Feb | 13 |
1a | Abe D | Mar | 11 |
1a | Abe D | Apr | 12 |
1a | Jon R | Jan | 9 |
1a | Jon R | Feb | 12 |
1a | Jon R | Mar | 9 |
1a | Jon R | Apr | 13 |
2b | Eve C | Jan | 13 |
2b | Eve C | Feb | 14 |
2b | Eve C | Mar | 13 |
2b | Eve C | Apr | 15 |
This is what I tried.
let office = "1a";
let mth = "Jan";
$("#Table2 tbody tr").filter(function() {
let step1 = $(this).toggle($(this).text().indexOf(office) > -1);
$(step1).toggle($(step1).text().indexOf(mth) > -1);
});
The code does not yield what I want. Below is what I want the output to be.
Office | Name | Month | Sales |
---|---|---|---|
1a | Abe D | Jan | 10 |
1a | Jon R | Jan | 9 |
thank you
2
Answers
Here’s how I would do it.
@JosephWebber already provided a good solution based on jQuery. Here is another one in Vanilla JavaScript.