0

I have an angular app that creates dynamic components like checkbox ,radio button ,textbox etc from server configuration . here i have to show the checkboxes with column settings .Lets say, you have 6 checkboxes and its number of columns are 3 , I have to show it as 2 rows 3 columns .But is showing in all new lines . Means 1 column 6 row

stackblitz link

checkbox.html

<div class="widgetcontainer form-group" [style.left.px]='position.X' [style.top.px]='position.Y'>

  <div *ngFor="let chk of checkboxList;let i = index" class="checkbox">

    <div *ngIf="(i+1)%3==0 else loading" style="color:red">
      <div [ngClass]="'display-block'">
        <label class="checkbox-inline">
          <input type="checkbox" id="checkbox-{{i}}" name="checkbox-{{i}}" [value]="chk" (change)="onSelectionChange($event)">{{chk}}
        </label>

      </div>
    </div>


    <div>
      <ng-template #loading>
        <div class='inlline-flex-cls'>
          <label class="checkbox-inline">
            <input type="checkbox" id="checkbox-{{i}}" name="checkbox-{{i}}" [value]="chk" (change)="onSelectionChange($event)">{{chk}}
          </label>

        </div>
      </ng-template>
    </div>



  </div>

</div>

app.css

.widgetcontainer{
    position: absolute;
}

.inlline-flex-cls{
    display: inline-flex;
}
.display-block{
    display: inline-block;
}

#maincontainer {
  position: relative;
}

app.component.html

<div id="maincontainer" #container></div>

app.component.ts

import { Component, ComponentFactoryResolver, ViewChild, ViewContainerRef } from '@angular/core';
import { DynamicComponent } from './dynamic/dynamic.component';
import { CheckboxComponent } from './dynamic/checkbox/checkbox.component';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  @ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;

  private _counter = 1;

  constructor(private componentFactoryResolver: ComponentFactoryResolver) { }

  add(): void {

    // create the component factory
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(DynamicComponent);

    // add the component to the view
    const componentRef = this.container.createComponent(componentFactory);

    // pass some data to the component
    componentRef.instance.index = this._counter++;
  }
  ngOnInit(){
    const componentFactory = this.componentFactoryResolver.resolveComponentFactory(CheckboxComponent);

    // add the component to the view
    const componentRef:any = this.container.createComponent(componentFactory);
     componentRef.instance.initialize();
  }
}
AhammadaliPK
  • 3,448
  • 4
  • 21
  • 39
  • at bottom level the html is not generated correctly . in simple term you need to generate a html table like structure where you can add columns on fly. you will have to spend time on angular and typescript on how to do it but here is the bottom line running demo you will be aiming for. http://jsfiddle.net/fmuW6/11/ – Sean Ch Jul 21 '20 at 05:07

1 Answers1

0

I am not an expert in angular, but I think issue is with the HTML that is getting generated. An extra element is getting generated before every '' each time, causing the checkbox to be created on next line.

You can see below simple HTML snippet for showing 6 check-boxes in 2 rows and 3 columns.

<form>
    <div>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 1</label>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 2</label>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 3 </label>
    </div>
    <div>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 4</label>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 5</label>
      <label class="checkbox-inline"><input type="checkbox" value="">Option 6 </label>
    </div>    
</form>

However, in your case, I think following html is generated through the angular code:

<form>
    <div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 1</label> 
      </div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 2</label> 
      </div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 3 
       </label></div>
      </div>
    <div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 4</label> 
     </div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 5</label> 
     </div>
      <div><label class="checkbox-inline"><input type="checkbox" value="">Option 6 
      </label></div>
    </div>    
</form>
sandeep23
  • 16
  • 4