0

I have list of group items which i am getting dynamically below is sample data

groupItems = {
    "AllResult": [
        {
            "GrpId": 1,
            "GroupName": "DESKTOP",
        },
        {
            "GrpId": 2,
            "GroupName": "LAPTOPS",
        },
        {
            "GrpId": 3,
            "GroupName": "MOBILES",
        }
    ]
}

and i am displaying these in the form of buttons so when ever i click on this button the i need display the result of that concerned group suppose if i click the Group name Laptops the items of laptops has to be displayed by using the groupid below is the result of the Items JSON

itemsDetails = {
    "AllList": [
        {
            "ItemId":1
            "GrpId": 1,
            "ItemName": "DELL",
        },
        {
            "ItemId":1
            "GrpId": 1,
            "ItemName": "ACER",
        },
        {
            "ItemId":1
            "GrpId": 1,
            "ItemName": "LENOVO",
        },
        {
            "ItemId":1
            "GrpId": 2,
            "ItemName": "HP",
        },
        {
            "ItemId":1
            "GrpId": 2,
            "ItemName": "ASUS",
        },
        {
            "ItemId":1
            "GrpId": 3,
            "ItemName": "Motorolla",
        }        
    ]
}

.html

<div *ngFor="let data of groupItems" (click)=items(data.Grpid) >
 <p>{{data.GroupName}}</p>
</div>

.ts code

items(Grpid){


}

Now my question is how can i get data/list of items when ever i click on the particular group suppose if i click on the DESKTOPS then all the items related to desktop has to be displayed

Trying the solution:

trying the soultion

0

1 Answer 1

2

There is no need to add extra key for array, you can directly have array of objects for groupItems & itemsDetails as:

groupItems = [
    {
        "GrpId": 1,
        "GroupName": "DESKTOP",
    }
    ...
]

itemsDetails = [
    {
        "ItemId":1
        "GrpId": 1,
        "ItemName": "DELL",
    }
    ...       
]

HTML

<div *ngFor="let data of groupItems" (click)=getItems(data.Grpid) >
    <p>{{data.GroupName}}</p>
</div>

TS

getItems(Grpid){
    let filteredData = this.itemsDetails.filter(item => item.GrpId == Grpid);
    console.log(filteredData);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ok but by using solutions i tried when ever i am calling groupItems suppose if i call desktop items all the items which are listed in desktop has to return but with ur solutions whats happening is it is displaying the all the items of the other groups also irrespective of the group i clicked
please check the solution i got after applying ur solution
@Devpop My bad it would be item.GrpId == Grpid rather than item.GrpId = Grpid missed = so instead of comparison it was assignment. Updated the answer check now.
yeah i found out the same and got the solutions and how can i make this items data as asynchronous like suppose if i have list of 500 names

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.