1

I am working on filtering a table in vue js. I have done filtering the table with name & date. But now I don't understand how to add another filter with them.

Here is a codepen link codepen

my computed property is like this---

filterItem() {
 let filterClient = this.selectedClient;
 let startDate = this.localizeDate(this.startDate);
 let endDate = this.localizeDate(this.endDate);
 let filterBranch = this.selectedBranch;
 const itemsByClient = filterClient
 ? this.salesReport.filter((item) => item.client_name === filterClient) && item.business_branch=== filterBranch)
 : this.salesReport;
 return itemsByClient.filter((item) => {
 const itemDate = new Date(item.date);
 if (startDate && endDate) {
 return startDate <= itemDate && itemDate <= endDate;
 }
 if (startDate && !endDate) {
 return startDate <= itemDate;
 }
 if (!startDate && endDate) {
 return itemDate <= endDate;
 }
 return true;
 });
},

It works when I give both name & business branch but I want to filter the data with or without name.

For example, If I select a client then the table shows the client. Now what I want is, when I select a branch(the other fields remains empty) then the table shows the rows associated with the selected branch

asked Nov 21, 2022 at 8:14
0

1 Answer 1

3

Just set up your filter functions and chain them.

filterItem() {
 let filterClient = this.selectedClient;
 let startDate = this.localizeDate(this.startDate);
 let endDate = this.localizeDate(this.endDate);
 let filterBranch = this.selectedBranch;
 const myClientFilterFunction = (item) => filterClient
 ? item.client_name === filterClient
 : true;
 const myBranchFilterFunction = (item) => filterBranch
 ? item.business_branch=== filterBranch
 : true;
 const myDateFilterFunction = (item) => {
 const itemDate = new Date(item.date);
 if (startDate && endDate) {
 return startDate <= itemDate && itemDate <= endDate;
 }
 if (startDate && !endDate) {
 return startDate <= itemDate;
 }
 if (!startDate && endDate) {
 return itemDate <= endDate;
 }
 return true;
 };
 return this.salesReport
 .filter(myClientFilterFunction)
 .filter(myBranchFilterFunction)
 .filter(myDateFilterFunction);
}

To achieve a more complex logic, like filtering only for branches or for clients, just add the filter functions programatically, e.g.

 let report = this.salesReport;
 if(this.selectedClient) {
 report = report.filter(myClientFilterFunction);
 }
 if(this.selectedBranch) {
 report = report.filter(myBranchFilterFunction);
 }
 report = report.filter(myDateFilterFunction);
 return report;
answered Nov 21, 2022 at 8:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.