2

Here I have a JSON array

demoList = [ 
  { name:"asian003", model:"hyper20", class:"selina0127" },
  { name:"asian003", model:"hyper21", class:"selina0127" },
  { name:"norva", model:"hyper21", class:"gowri.14" },
  { name:"sparks007", model:"NNc22", class:"gowri.14" },
  { name:"asian003", model:"NNc22", class:"selina0127" }
]

This array showing in an HTML table using Angular. I want to add dropdowns separately such as name dropdown, model dropdown, class dropdown. When I select drop-down values I want to filter table records.

For example, if I select asian003 from name and selina0127 from class and do not select the value model dropdown, I want to get the below result into the table preview

demo1List = [
  {name:"asian003",model:"hyper20",class:"selina0127"},
  {name:"asian003",model:"hyper21",class:"selina0127"},
  {name:"asian003",model:"NNc22",class:"selina0127"}
]
1
  • pls consider this => For example, if I select asian003 from name and selina0127 from class and do not select the value model dropdown, I want to get the below result into the table preview Commented Aug 31, 2021 at 8:40

1 Answer 1

1

You can create and bind one property to each dropdown. Then on any dropdown change, you can call filter_demo_list() function:

const demoList = [ 
  { name:"asian003", model:"hyper20", class:"selina0127" },
  { name:"asian003", model:"hyper21", class:"selina0127" },
  { name:"norva", model:"hyper21", class:"gowri.14" },
  { name:"sparks007", model:"NNc22", class:"gowri.14" },
  { name:"asian003", model:"NNc22", class:"selina0127" }
]

public table_data;
public selected_name;
public selected_class;
public selected_model;

filter_demo_list() {
  this.table_data = demoList;
  if (this.selected_name) this.table_data.filter((item)=> item.name === this.selected_name);
  if (this.selected_class) this.table_data.filter((item)=> item.class  === this.selected_class);
  if (this.selected_model) this.table_data.filter((item)=> item.model === this.selected_model);
}
Sign up to request clarification or add additional context in comments.

3 Comments

pls consider this => For example, if I select asian003 from name and selina0127 from class and do not select the value model dropdown, I want to get the below result into the table preview
My code covers that logic. As you can see, if this.selected_model is not selected, the last if condition will not be executed and the filter by model won't be applied.
I made some modifications. The code is worked.

Your Answer

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.