0

I have a list of dynamic checkboxes using keyvalue pipe that returns objects rather than just array of selected IDs.

can anyone help pls, I need the form to submit just an array of selected user IDs.

https://stackblitz.com/edit/angular-ciaxgj

EDIT here's the log of a similar form with a multi-select (countries): console log

I need users (checkboxes) to return an array like countries (multi-select) as in the log above.

3
  • Just do console.log(Object.keys(this.userForm.value.users)); Commented Mar 20, 2019 at 10:55
  • stackoverflow.com/questions/54984196/…. tip, sometimes it's utils write in .html {{myform?.value|json}} to check the value of a form Commented Mar 20, 2019 at 11:33
  • Thanks, but that hasn't helped. I've updated the question. Commented Mar 20, 2019 at 12:55

2 Answers 2

0

Change your OnSubmit function to

onSubmit() {
    console.log(this.userForm.value);
    var usersObj = this.userForm.value.users;
    var selectedUserIds = [];
    for (var userId in usersObj) {
        if (usersObj.hasOwnProperty(userId)) {
          if(usersObj[userId])//If selected
          {
              selectedUserIds.push(userId);
          }

        }
    }
    console.log(selectedUserIds);
  }

Here is updated code

https://stackblitz.com/edit/angular-9fd17t

If you want to submit as a Form then add a hidden field to form

<input type="hidden" name="selectedUserIds" value=""/>

set value selectedUserIds from onSubmit and submit the form code.

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

2 Comments

Thanks for your answer, I was able to get this far previously, but the issue still remains that the form submits objects. I have tried to update the form with setValue, patchValue, but none seems to work. I need the form to submit just the array of IDs. Any ideas pls? Am I approaching this all wrong?
Change button type to type="submit", Form submit may not work in stackblitz.com
0

So, it happens that I have been overthinking the solution in thinking that like the multiselect, checkboxes would/could return a ready array of selected items.

The rather simple solution was from reading another of Eliseo's posts combined with Jaba Prince's answer:

onSubmit() {
    var usersObj = this.userForm.value.users;
    var selectedUserIds = [];
    for (var userId in usersObj) {
        if (usersObj.hasOwnProperty(userId)) {
          if(usersObj[userId])//If selected
          {
              selectedUserIds.push(userId);
          }

        }
    }

    let data = {
      users: selectedUserIds
    }

    console.log(data);

    // post data in service call

  }

Many thanks to you two!

1 Comment

You could upvote and like the answer if it helped you :)

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.