2

I have a form with User roles displayed as multiple checkboxes:

<div *ngFor="let role of roles">
            <label for="role_{{role.id}}">
              <input type="checkbox" ngModel name="roles" id="role_{{role.id}}" value="{{role.id}}"> {{role.name}} &nbsp;&nbsp;
            </label>
          </div>

the roles object loaded from server looks like this which have all the roles that displayed on the form:

{id: 1, name: "HQ", description: "A Employee User", created_at: "2017-10-07 10:43:17",…}
1
:
{id: 2, name: "admin", description: "A Manager User", created_at: "2017-10-07 10:43:17",…}
2
:
{id: 3, name: "caretaker", description: "", created_at: null, updated_at: null}

now i want to set multiple check boxes using form.setValue, my user object loaded from server looks like this: "roles" in the user object are the roles that are assigned to the user and needs to be checked on the form

{
"id":13,
"name":"Wasif Khalil",
"email":"[email protected]",
"created_at":"2017-10-07 10:43:17",
"updated_at":"2017-10-09 07:45:34",
"api_token":"LKVCGPGnXZ3LyiCnyiTAg8XTpck6xWlVkeoMBgtoYZWoAOy4b5epNqMz7KG7",
"roles":[
     {"id":2,"name":"admin","description":"A Manager User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"2","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17"}
     },
     {"id":1,"name":"HQ","description":"A Employee User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"1","created_at":null,"updated_at":null}
     }
   ]
}

after loading user object form server im setting values like this:

this.form.setValue({
                        name: user.name,
                        email: user.email,
                        password:"",
                        confirm_password:"",
                        roles: [1] //here im not sure how to set roles
                    });

can someone help me check the checkboxes with the loaded user roles object. Thanks in advance

EDIT: Sorry for not explaining it well, i have edited my question to explain the question again: the roles on user object are the roles that are assigned to user and the roles object is the list of all roles to display in form, look at the image below:

enter image description here

8
  • 1
    I don't recognise the syntax, is ngModel not supposed to be ngModel="yourvalue"? Commented Oct 9, 2017 at 12:36
  • Why dont you just overwrite the value rather than using the setValue? Commented Oct 9, 2017 at 12:38
  • yeah, ngModel="YourArrayVariable" in your case Commented Oct 9, 2017 at 12:38
  • actually [(ngModel)]="yourArrayVariable" Commented Oct 9, 2017 at 12:39
  • I suggest that you switch to use reactive forms for what you want to do. You will want to use patchValue, not setValue, becuase setValue is used when you want to change all the values, not just a subset. Commented Oct 9, 2017 at 12:43

1 Answer 1

1

You don't have to use reactive forms to make it done.

HTML

  <input ...[checked]="check(user.roles,role.id)" ...>

Typescript:

check(value1, value2){
  return (value1.filter(item => item.id == value2)).length
}

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

awesome thanks (Y) it worked, i just had to make one change, the "users" object you defined is a sinle user object so i dont need to add another ngfor on the view, i just renamed "users" to "user" and removed users ngfor
one question, i cannot add validation if i dont add ngModel and when i add validations it checks all checkboxes, any solution for that?
Glad it worked! :) The update should add the missing role to the user by adding a new line and unchecking should remove it, I guess ? Is there an order of roles to respect?

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.