【问题标题】:How to add a new column to the table, based on checkbox selection in angular 7如何根据角度 7 中的复选框选择向表中添加新列
【发布时间】:2019-09-03 17:54:07
【问题描述】:

我有一个包含一些行的表。表数据来自循环。这里我需要根据复选框的选择添加新列。假设我检查了产品,一个新的表标题将创建为产品以及下面的空白行表标题,再次如果我取消选中创建的列将被删除。新列应在添加按钮之前创建。这是下面的代码 https://stackblitz.com/edit/angular-table-focus

app.component.html

    <div class="container">
  <h2>Basic Table</h2>
  <p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p> 
<div><button (click)="enable()">Edit</button> <input type="checkbox" value="product">Product&nbsp;<input type="checkbox" value="market">market</div>  
  <table class="table border">
    <thead>
    <tr>
    <th>Items</th>
    <th>Business</th>
    <th></th>
    </tr>
    </thead>
    <tbody>
      <tr *ngFor="let row of groups;let i = index" (click)="setClickedRow(i)"  [class.active]="i == selectedRow">
       <td> <input #focus [disabled]='toggleButton' type="text" value="{{row.name}}"> </td>
       <td> <input [disabled]='toggleButton' type="text" value="{{row.items}}"> </td>
       <td> <button (click)="addRow(i)">Add</button></td>
      </tr>
    </tbody>
  </table>
</div>

app.component.ts

import { Component,ViewChild, ElementRef } from '@angular/core';

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

@ViewChild('focus') input: ElementRef;
      public toggleButton: boolean = true;
  ngOnInit() {

  }
  groups=[
     {
       "name": "pencils",
       "items": "red pencil"
     },
     {
       "name": "rubbers",
       "items": "big rubber"
     },
     {
       "name": "rubbers1",
       "items": "big rubber1"
     },
  ];
  addRow(index): void {
    var currentElement = this.groups[index];
    this.groups.splice(index, 0, currentElement);
 }
 enable(){
    this.toggleButton = false
    setTimeout(()=>{ // this will make the execution after the above boolean has changed
    this.input.nativeElement.focus();
    this.selectedRow = 0;
  },0); 
    }
  setClickedRow(index) {
    this.selectedRow = index;
  }
}

【问题讨论】:

  • 根据您的组属性在&lt;th&gt; 上创建一个 ng-for。当用户点击复选框时,在组上添加属性。
  • 我试过了,但无法正确推送新列
  • @UIAPPDEVELOPER - 现在您有 ItemsBusiness 作为列。那么,您要在选中时添加ProductMarket 吗?添加后,这些列的表行值是多少?
  • 应该是空的
  • 是的,应该是空的

标签: html angular


【解决方案1】:

查看我的 StackBlitz 进行现场演示:https://stackblitz.com/edit/angular-table-focus-77xrnn

要动态添加或删除columns,我们必须首先从组件中获取值,而不是在模板中硬编码它们。

columns = ["Items", "Business"];

组件:

每当切换列复选框时,我们都会监听change 事件并相应地添加或删除列。

onColumnStatusChange(column, isChecked) {
  if (isChecked) {
    // Add column to the table.

    this.columns.push(column);

    this.groups.forEach(value => {
      value[column] = "";
    });
  } else {
    // Delete column from the table.

    this.columns.splice(this.columns.indexOf(column), 1);

    this.groups.forEach(value => {
      delete value[column];
    });
  }
}

模板:

在模板中,列和表格行项目必须动态获取。

<input type="checkbox" value="product" (change)="onColumnStatusChange($event.target.value, $event.target.checked)">Product&nbsp;
<input type="checkbox" value="market" (change)="onColumnStatusChange($event.target.value, $event.target.checked)">market
  ...
  ...
<tr>
  <ng-container *ngFor="let column of columns; let i = index">
     <th>{{ column }}</th>
  </ng-container>
</tr>
  ...
  ...
<tr *ngFor="let row of groups;let i = index" (click)="setClickedRow(i)"  [class.active]="i == selectedRow">
   <ng-container *ngFor="let item of objectKeys(row); let j = index">
      <td><input #focus [disabled]='toggleButton' type="text" value="{{row[item]}}"></td>
   </ng-container> 
   <td><button (click)="addRow(i)">Add</button></td>
</tr>

【讨论】:

  • 我已在我的项目中添加了此代码,但无法获取表数据 onload 其显示错误“无法将未定义或 null 转换为对象”...有什么解决方案吗?
  • @UIAPPDEVELOPER - 我不确定是什么导致了该错误。确保您的 groupscolumns 值不是 nullundefined。另外,看看这个answer。如果仍未解决,请在 StackBlitz 中重现相同的错误,我会看看。
  • 我得到了 column/thead 值,但是 tbody 有问题 ,我不明白你是如何获取项目和业务数据的?
  • StackBlitz 所示,确保在组件中添加objectKeys = Object.keys;。第二个*ngFor 嵌套在第一个*ngFor 中,并从第一个*ngFor 获取row 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多