0

When I am clicking the add button, the row cannot be added. How can I add a row in a table?

My HTML file looks like:

<div>
<div>
<table class="table table-bordered table-striped mb-0 mt-3">
  <thead>
    <tr>
      <th>Name</th>
      <th>Type</th>
      <th>Required</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <tr *ngFor="let field of fieldArray; let i = index">
      <td>
        <input type="text" [(ngModel)]="field.domainName"> 
</td>
      <td>
        <div class="optionDropdown" 
[(ngModel)]="field.domainSelect">
        <nb-select class="" placeholder="All" 
[(selected)]="selectedItem">
          <nb-option value="">Option empty</nb-option>
          <nb-option value="0">Integer</nb-option>
          <nb-option value="1">Float</nb-option>
          <nb-option value="2">String</nb-option>
          <nb-option value="3">Character</nb-option>
          <nb-option value="4">Boolean</nb-option>
        </nb-select>
      </div></td>
      <td>
        <div [(ngModel)]="field.domainRequired">
            <nb-checkbox (checkedChange)="toggle($event)"></nb- 
checkbox>
        </div>            
      </td>
      <td (click)="newRow()">
        <div [(ngModel)]="field.domainIcon">
            <i class="plusIcon nb-plus"></i>
        </div>            
      </td>
    </tr>
  </tbody>
</table>
</div>
</div>

My ts file looks like:

export class DomainStructuredDataComponent implements OnInit {

  constructor() { }
  checked = false;
  domain: string;
  selectedItem: any;
  domainSelect: any;
  domainIcon: boolean;
  fieldArray: any = [];
  newAttribute: any = {};
  ngOnInit() {
  }
  toggle(checked: boolean) {
    this.checked = checked;
  }
  newRow() {
    this.fieldArray.push('');
  }
}

I mentioned my code above. Let me know, how to fix it.

1 Answer 1

1

Your need to push an object instead of string something like

newRow() {
    this.fieldArray.push({
       domainName:'',
       domainSelect:'',
       domainRequired:'',
       domainIcon:''
    });
}

Note:- you can use your default values instead of '' in above object

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

4 Comments

also set fieldArray: any = []; to public
According to his ngFor add "domainIcon" too
Thanks for pointed out my mistakes. After changed the array push, the tbody not show.
@vishnupriya this is because you don't have any item in your array you can have add button outside of your row put it at some other place instead

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.