0

I have a JSON data like I shown below,

[  
         {  
            "id":1,
            "applicationId":1,
            "permissions":"Edit"
         },
         {  
            "id":2,
            "applicationId":1,
            "permissions":"View"
         }
]

Now I need to bind the checkbox dynamically for the data "permissions" and there is a button in HTML to Submit and when I click on Submit button, I need all the applicationId's which are checked in an array.

How can I achieve this in Angular?

3

1 Answer 1

1

I guess that you need to have as many checkboxs as the length of the array.

You can iterate the list with an ngFor and keep the state of the selected items in a separate array.

  checked = [];

  data = [  
         {  
            "id":1,
            "applicationId":1,
            "permissions":"Edit"
         },
         {  
            "id":2,
            "applicationId":1,
            "permissions":"View"
         }
  ];

  checkItem(item) {
    const idx = this.checked.indexOf(item);
    idx >= 0 ? this.checked.splice(idx, 1) : this.checked.push(item);
  }

  submitData() {
    const applicationIds = this.checked.map(it => it.applicationId);
    console.log(applicationIds);
  }

and the template to be something like

<ul>
  <li *ngFor="let item of data">
    {{item.permissions}} <input type="checkbox" (change)="checkItem(item)">
  </li>
</ul>

<button type="button" (click)="submitData()">Submit</button>
Sign up to request clarification or add additional context in comments.

Comments

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.