10

Please go through the link(https://ng-select.github.io/ng-select#/multiselect-checkbox) to know ng-select multi select checkbox.

I am trying to use the ng-select "Group selects children" in my angular 6 application.

I am having problem using ng-select "Group selects children" with Reactive Forms instead of template driven forms.

I have tired it as

<ng-select
          [items]="userGroupsList"
          [multiple]="true"
          bindLabel="name"
          groupBy="gender"
          [selectableGroup]="true"
          [selectableGroupAsModel]="false"
          [closeOnSelect]="false"
          bindValue="id"
          formControlName="userGroups" placeholder="Select a user group">
            <ng-template ng-optgroup-tmp let-item="item" let-item$="item$" let-index="index">
                <input id="item-{{index}}" type="checkbox" [(ngModel)]="" formControlName="userGroupParent"/> {{item.gender | uppercase}}
            </ng-template>
            <ng-template ng-option-tmp let-item="item" let-item$="item$" let-index="index">
                <input id="item-{{index}}" type="checkbox" [(ngModel)]="" formControlName="userGroupChild"/> {{item.name}}
            </ng-template>
        </ng-select>

I have used the same data of the multiselect-checkbox-- [items]="userGroupsList"

https://github.com/ng-select/ng-select/blob/master/demo/app/shared/data.service.ts -- getMockPeople() has the data

So here can i use the [(ngModel)] as well as formControlName on the input how can i child elements are selected when the parent is select as in the example

Please help....!

Tanjore Raj
  • 135
  • 1
  • 1
  • 7
  • Can you provide some example of what you already tried to do ? This component seem to work like a normal component – xrobert35 Sep 19 '18 at 18:14
  • i have edited the question.. xrobert35. Please help..! – Tanjore Raj Sep 22 '18 at 15:17
  • It dont really have sens to use formControlName in the group or option template here. Why do you want to do that ? – xrobert35 Sep 22 '18 at 15:40
  • By the way it's look like you don't really have understand how the template work but **let-item$="item$"*** in an option value or a group value that you must in your input with [ngModel] like in the exemple **[ngModel]="item$.selected"**. So if your question is to know if you can use formControlName for you "input" for me it's **no** – xrobert35 Sep 22 '18 at 15:48
  • Thanks for the reply, But my question is that if i can't use the formControlName then which directive would help me to achieve this kind of functionality. – Tanjore Raj Sep 23 '18 at 05:47
  • What kind of functionnality ? I understand that you want make the component work like in the example but in a different way (which doesn't work). So for me you just have to follow the example and use ngModel – xrobert35 Sep 23 '18 at 10:14
  • I want to use Multiselect Checbox with reactive form "fileControlName" when i use the [ngModel]="item$.selected" i get the below error: ** ngModel cannot be used to register form controls with a parent formGroup directive. Try using formGroup's partner directive "formControlName" instead. ** – Tanjore Raj Sep 23 '18 at 11:46
  • Ah it make things a clearer. You can update your question with this. – xrobert35 Sep 23 '18 at 11:57

3 Answers3

14

To make this work with formGroup : keep the formControlName on the ng-select who will be bind to your formGroup.

The problem is those input in the template. For me the best solution is to keep using ngModel like in the example. But you must make angular understand that is as nothing to do with the fromGroup so you can add the option standalone on it.

<input id="item-{{index}}" type="checkbox" [(ngModel)]="item$.selected" [ngModelOptions]="{ standalone : true }" />
xrobert35
  • 2,488
  • 1
  • 16
  • 15
11

There is an other way to do that without the ngModel :

<input id="item-{{index}}" type="checkbox" [checked]="item$.selected" /> 
Sa Hagin
  • 502
  • 1
  • 7
  • 18
0

My dropdown item was not getting selected on clicking the item in the dropdown, it was only getting checked on clicking on the checkbox. This worked for me thanks