【问题标题】:How to show edit icon for few rows on mat table如何在垫表上显示几行的编辑图标
【发布时间】:2020-07-26 20:12:07
【问题描述】:

我是 Angular 的新手。我在 mat 表上添加了一列用于编辑字段。但它显示在每一列。我只想让几行可编辑。我怎样才能实现它?

看起来像这样

我只想显示第 2 行和第 3 行的编辑图标。但它显示每一行。我试过 *ngIf 但无法编写它的 TS 代码。

下面我附上了我尝试过的代码

details.component.html

<div class="reviews-style">
<div>


    <table mat-table [dataSource]="dataSource" class="mat-elevation-z1">
        <ng-container matColumnDef="key">
        <td mat-cell *matCellDef="let element" class="item-name"> {{element.key}} </td>
        </ng-container>

        <ng-container matColumnDef="value" >
            <th mat-header-cell *matHeaderCellDef> No. </th>
            <td mat-cell *matCellDef="let element" > 
              <span *ngIf="!element.editable; else editPlace">
              {{element.value}} 
              </span>
              <ng-template #editPlace>
                <input [(ngModel)]="element.value" (keyup.enter)="element.editable = false">
              </ng-template>
            </td>
          </ng-container>

          <ng-container matColumnDef="actions">
            <mat-header-cell *matHeaderCellDef>Actions </mat-header-cell>
            <mat-cell *matCellDef="let element">                
              <button mat-icon-button matTooltip="Click to Edit" (click)="edit(element)" class="iconbutton" color="primary">
                  <mat-icon aria-label="Edit">edit</mat-icon>
                </button>            
            </mat-cell>
          </ng-container>


        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
</div><br><br>
<button mat-stroked-button class="b1" (click)="update()" formtarget="_blank">Update</button>

details.component.ts

interface Data {
  key: string;
  value: string;
  editable: boolean;
}

const namesEnum: {[key:string]: string} = {
  'vid' : 'Vendor Id',
  'bn' : 'Vendor Name',
  'cate' : 'Category',
  'jdate' : 'Joining Date',
  'pcount' : 'Total No of Packages',
  'ocount' : 'Total No of Outlets',
  'scount' : 'Total No of Services',
}


@Component({
  selector: 'app-vendordetails',
  templateUrl: './vendordetails.component.html',
  styleUrls: ['./vendordetails.component.css']
})
export class VendordetailsComponent implements OnInit {

  constructor(private activatedRoute:ActivatedRoute,private apiService:ApiService) { }

  dataSource: Data[] = [];
  displayedColumns: string[] = ['key', 'value','actions'];
  vid:any;
  result:any;
  result1:any;
  col:any;

  ngOnInit() 
  {
    this.vid=this.activatedRoute.snapshot.paramMap.get('vid');

    this.activatedRoute.queryParamMap.subscribe((queryParams:Params)=>{
      let vid=this.vid;

     this.col='vdetail'
      this.apiService.getVendorDetailsById(vid,this.col)
      .subscribe(data=>{
        this.result = data[0];

        const newdata: Data[] = [];
        for (const prop in this.result) {
        newdata.push({
        key: namesEnum[prop],
        value: this.result[prop],
        editable: false
        })        
        }     
      this.dataSource = newdata;
      });
    });
  }

  edit(e: any) {
    e.editable = !e.editable;
  }

}

我正在以这种格式在“数据源”中获取数据

(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {key: "Vendor Id", value: "1", editable: false}
1: {key: "Vendor Name", value: "Free Charge", editable: false}
2: {key: "Category", value: "Beauty & Salon, Fitness, Food", editable: false}
3: {key: "Joining Date", value: "01-05-2019 11:30:25", editable: false}
4: {key: "Total No of Packages", value: "21", editable: false}
5: {key: "Total No of Outlets", value: "9", editable: false}
6: {key: "Total No of Services", value: "53", editable: false}
length: 7

【问题讨论】:

  • 你能在 stackblitz 上重现这个进行演示吗?
  • @GaurangDhorda 我不知道如何创建 stackblitz :(
  • 好的.. 究竟是什么错误?并给出你用来绑定 mat-table 的正确数据结构。
  • @GaurangDhorda 没有收到任何错误,因为无法理解在 TS 文件中写入的内容以在 *ngIf 中实现。我用“dataSource”的数据结构更新了我的问题
  • 在您的数据中,editable 的每个字段都设置为false,那么您如何使用 *ngIf 显示它?对于需要显示编辑图标的列,您需要可编辑为 true。

标签: angular typescript angular-material


【解决方案1】:

我想我现在明白你的问题了。 这样的函数不能写吗?

showEditAction(key: string) { return key == 'Vendor Id' || key == 'Vendor Name' } 

并将其与*ngIf 一起用于按钮,例如*ngIf="showEditAction(element.key)"

【讨论】:

  • 添加了它,但现在没有图标显示,因为 *ngIf 是假的。我们如何才能只对几行实现它?
  • 几行是什么意思?你可以做一件事。在您的 component.ts 文件中编写一个函数,该函数根据您的特定条件返回 true 或 false 并将其用于 *ngIf 语句
  • 这意味着我不想让所有行都可编辑。我只想让“供应商名称”和“类别”可编辑
  • 你的意思是任何一行的一部分?
  • 我的表是键值格式。我想编辑值部分,为此我添加了编辑图标,它的编辑很好,但我不想让所有行都可编辑,你可以看到上面的图片
猜你喜欢
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2014-06-23
  • 2023-04-01
  • 2020-11-23
相关资源
最近更新 更多