【问题标题】:Radio Button within a table with Material Design带有 Material Design 的表格中的单选按钮
【发布时间】:2018-11-08 04:27:08
【问题描述】:

我实际上是在尝试在 Material Design 和 Angular 6 的表格组件中插入一个单选按钮组。

所以在 Material design 官方网站的示例中,有一个带有复选框的表格,但我想要对表格的每个元素都使用单选按钮。

我尝试使用 *ngFor 指令执行此操作,但我无法访问 ELEMENT_DATA 数组,即使它是全局常量。

import {Component} from '@angular/core';
import {MatTableDataSource} from '@angular/material';
import {SelectionModel} from '@angular/cdk/collections';

@Component({
  selector: 'table-selection-example',
  styleUrls: ['table-selection-example.css'],
  templateUrl: 'table-selection-example.html',
})
export class TableSelectionExample {
  displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
  dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
  selection = new SelectionModel<PeriodicElement>(true, []);

  isAllSelected() {
    const numSelected = this.selection.selected.length;
    const numRows = this.dataSource.data.length;
    return numSelected === numRows;
  }

  masterToggle() {
    this.isAllSelected() ?
        this.selection.clear() :
        this.dataSource.data.forEach(row => this.selection.select(row));
  }
}

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
    <table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Checkbox Column -->
  <ng-container matColumnDef="select">
    <th mat-header-cell *matHeaderCellDef>
      <mat-checkbox (change)="$event ? masterToggle() : null"
                    [checked]="selection.hasValue() && isAllSelected()"
                    [indeterminate]="selection.hasValue() && !isAllSelected()">
      </mat-checkbox>
    </th>
    <td mat-cell *matCellDef="let row">
      <mat-checkbox (click)="$event.stopPropagation()"
                    (change)="$event ? selection.toggle(row) : null"
                    [checked]="selection.isSelected(row)">
      </mat-checkbox>
    </td>
  </ng-container>

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef> Name </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <!-- Weight Column -->
  <ng-container matColumnDef="weight">
    <th mat-header-cell *matHeaderCellDef> Weight </th>
    <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
  </ng-container>

  <!-- Symbol Column -->
  <ng-container matColumnDef="symbol">
    <th mat-header-cell *matHeaderCellDef> Symbol </th>
    <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"
      (click)="selection.toggle(row)">
  </tr>
</table>

因此,我想使用此代码删除复选框并将其替换为单选按钮。

我尝试的是:

<mat-radio-group>
   <mat-radio-button *ngIf="let i of ELEMENT_DATA" value = i></mat-radio-button>
</mat-radio-button>

就像我之前所说的,我无法访问我的数组 ELEMENT_DATA。我也尝试用 dataSource.length 更改它,但没有任何效果。

感谢您的帮助。

【问题讨论】:

    标签: angular radio-button material-design


    【解决方案1】:

    您可以将 mat-radio-group 添加到 mat-cell 并使用 ngModel:

    <ng-container matColumnDef="radio">
      <mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell>
      <mat-cell *matCellDef="let row">
        <mat-radio-group [(ngModel)]="selectedPerson">
          <mat-radio-button [value]="row"></mat-radio-button>
        </mat-radio-group>
      </mat-cell>
    </ng-container>
    

    【讨论】:

      【解决方案2】:

      这可能为时已晚,但我想我会插话。我只是想实现同样的行为,我发现使用 CKD 中的SelectionModel 基本上可以让我在视觉上复制这种行为,即使不是在更好的方法。

      https://material.angular.io/components/table/overview#selection

      它支持多选或单选模式,您可以轻松地将点击逻辑放在mat-row 上,以及任何视觉样式(甚至单选按钮),以告知用户当前选择的状态。

      【讨论】:

      • 更正我上面的评论,我将它用于复选框并且效果很好。对于单选按钮,我使用 ngModel 使用以下解决方案。这不那么复杂。
      【解决方案3】:

      您只需在表格的 td 内添加即可。但我不知道如何知道选择了哪个单选按钮。

       <table mat-table [dataSource]="dataSource" matSortDisableClear>
      
                                      <ng-container matColumnDef="select">
                        <th mat-header-cell *matHeaderCellDef>
                          <mat-radio-button disabled>
                          </mat-radio-button>
                        </th>
                        <td mat-cell *matCellDef="let row">
                          <mat-radio-button (click)="$event.stopPropagation()"
                                            (change)="$event ? selection3.toggle(row) : null"
                                            [checked]="selection3.isSelected(row)"
                                            ></mat-radio-button>
                        </td>
                      </ng-container>
                      <tr mat-header-row *matHeaderRowDef="datadisplayedColumns"></tr>
      
                      <tr mat-row *matRowDef="let row; columns: datadisplayedColumns;"></tr>
       </table>
      

      打字稿

        selection3 = new SelectionModel<Instruments>(false, []);
      
      

      如果你在行部分工作,你总是可以像我一样使用 let row 当你想在整个行上放置一个 routerLink 时也很方便

      【讨论】:

      • 我希望这对某人有所帮助,因为最初的问题正好是 1 岁
      【解决方案4】:

      你所做的每一件事都是正确的。但是,你为什么要这样做 *ngIf = "let i of Element_Data":

          <mat-radio-group>
              <mat-radio-button *ngIf="let i of ELEMENT_DATA" value = i></mat-radio-button>
          </mat-radio-button>.
      

      您是否正在尝试使用 *ngFor?你不能在 *ngIf 中做 'let i of Element_Data'

      【讨论】:

        【解决方案5】:

        ELEMENT_DATA 必须是类的一部分。

        试试这个:

        import {Component} from '@angular/core';
        import {MatTableDataSource} from '@angular/material';
        import {SelectionModel} from '@angular/cdk/collections';
        
        @Component({
          selector: 'table-selection-example',
          styleUrls: ['table-selection-example.css'],
          templateUrl: 'table-selection-example.html',
        })
        export class TableSelectionExample {
          displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
          dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);
          selection = new SelectionModel<PeriodicElement>(true, []);
        
          isAllSelected() {
            const numSelected = this.selection.selected.length;
            const numRows = this.dataSource.data.length;
            return numSelected === numRows;
          }
        
          masterToggle() {
            this.isAllSelected() ?
                this.selection.clear() :
                this.dataSource.data.forEach(row => this.selection.select(row));
          }
        
        const ELEMENT_DATA: PeriodicElement[] = [
          {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
          {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
          {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
          {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
          {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
          {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
          {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
          {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
          {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
          {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
        ];
        }
        
        export interface PeriodicElement {
          name: string;
          position: number;
          weight: number;
          symbol: string;
        }
        

        【讨论】:

          【解决方案6】:

          ELEMENT_DATA 应该是核心组件的属性,而不是常量。否则你无法在 html 部分访问它。

          【讨论】:

          • 哦,好吧,那你知道怎么做吗?用其他方法?
          • 将此添加到您的 TableSelectionExample elementData = ELEMENT_DATA,然后在您的 html 中访问它 *ngFor="let i of ELEMENT_DATA" 我看到您使用了错误的语法,当您尝试访问数组时,将 *ngIf 替换为 *ngFor。
          • 哦,是的,对不起,实际上这是我的错,我正在使用 *ngFor 指令。我试过你说的,但现在我的表里什么都没有,我有这个错误:ERROR TypeError: Cannot read property 'template' of undefined
          • 啊,现在我明白你想要达到的目的了。您想在一行中有一个单选按钮来选择一个特定的行吗?
          • 是的,这就是我想要的,我想为表格的每个元素创建一个单选按钮。所以现在我正在这样做: Option 我添加 elem = ELEMENT_DATA;在我的组件类中但是我在一行中有 10 个按钮,而不仅仅是我的表格的每个元素一个。
          【解决方案7】:

          尝试复制下面的代码,对我有用

                          <th mat-header-cell *matHeaderCellDef> No. </th>
                          <td mat-cell *matCellDef="let row">    
                                  <mat-radio-button [value]="row" (click)="radiobuttonClick(row)" color="primary"></mat-radio-button>
                          </td>
                      </mat-radio-group>
          

          【讨论】:

            猜你喜欢
            • 2015-09-30
            • 2019-10-19
            • 2019-12-08
            • 2019-11-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-08-24
            相关资源
            最近更新 更多