当前位置: 首页 > 知识库问答 >
问题:

有没有办法使物料表的角度保持扩展?(带有tr和td,而不是mat-row标签)

卫君博
2023-03-14

我正在尝试设置带可扩展行的角材料表,这样在单击某一行后,它保持扩展,在单击其他行后它不会折叠,只有在单击自身时才会折叠-https://stackblitz.com/angular/ygdrrokyvkv?file=app%2ftable-expandable-rows-example.html

我一直在寻找解决办法,但发现只有老的方法,但没有材料表。我需要https://stackblitz.com/angular/ygdrrokyvkv?file=app%2ftable-expandable-rows-example.html这个文件可以展开多行

我找不到很好的解决方案,如何从expandedDetail生成一个数组并相应地显示它?

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
 <tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
     class="example-element-row"
     [class.example-expanded-row]="expandedElement === element"
     (click)="expandedElement = element">
 </tr>
 <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>

我希望具有可展开行的表保持展开状态,直到用户不再单击该行本身为止

共有1个答案

邴姚石
2023-03-14

界面中添加扩展键,单击后将其设置为true:

组件类中的更改:

import {Component} from '@angular/core';
import {animate, state, style, transition, trigger} from '@angular/animations';

/**
 * @title Table with expandable rows
 */
@Component({
  selector: 'table-expandable-rows-example',
  styleUrls: ['table-expandable-rows-example.css'],
  templateUrl: 'table-expandable-rows-example.html',
  animations: [
    trigger('detailExpand', [
      state('collapsed', style({height: '0px', minHeight: '0', display: 'none'})),
      state('expanded', style({height: '*'})),
      transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
    ]),
  ],
})
export class TableExpandableRowsExample {
  dataSource = ELEMENT_DATA;
  columnsToDisplay = ['name', 'weight', 'symbol', 'position'];
  expandedElement: PeriodicElement;
}

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

const ELEMENT_DATA: PeriodicElement[] = [
  {
    position: 1,
    name: 'Hydrogen',
    weight: 1.0079,
    symbol: 'H',
    expanded: false,
    description: `Hydrogen is a chemical element with symbol H and atomic number 1. With a standard
        atomic weight of 1.008, hydrogen is the lightest element on the periodic table.`
  }, {
    position: 2,
    name: 'Helium',
    weight: 4.0026,
    symbol: 'He',
    expanded: false,
    description: `Helium is a chemical element with symbol He and atomic number 2. It is a
        colorless, odorless, tasteless, non-toxic, inert, monatomic gas, the first in the noble gas
        group in the periodic table. Its boiling point is the lowest among all the elements.`
  }, {
    position: 3,
    name: 'Lithium',
    weight: 6.941,
    symbol: 'Li',
    expanded: false,
    description: `Lithium is a chemical element with symbol Li and atomic number 3. It is a soft,
        silvery-white alkali metal. Under standard conditions, it is the lightest metal and the
        lightest solid element.`
  }, {
    position: 4,
    name: 'Beryllium',
    weight: 9.0122,
    symbol: 'Be',
    expanded: false,
    description: `Beryllium is a chemical element with symbol Be and atomic number 4. It is a
        relatively rare element in the universe, usually occurring as a product of the spallation of
        larger atomic nuclei that have collided with cosmic rays.`
  }, {
    position: 5,
    name: 'Boron',
    weight: 10.811,
    symbol: 'B',
    expanded: false,
    description: `Boron is a chemical element with symbol B and atomic number 5. Produced entirely
        by cosmic ray spallation and supernovae and not by stellar nucleosynthesis, it is a
        low-abundance element in the Solar system and in the Earth's crust.`
  }, {
    position: 6,
    name: 'Carbon',
    weight: 12.0107,
    symbol: 'C',
    expanded: false,
    description: `Carbon is a chemical element with symbol C and atomic number 6. It is nonmetallic
        and tetravalent—making four electrons available to form covalent chemical bonds. It belongs
        to group 14 of the periodic table.`
  }, {
    position: 7,
    name: 'Nitrogen',
    weight: 14.0067,
    symbol: 'N',
    expanded: false,
    description: `Nitrogen is a chemical element with symbol N and atomic number 7. It was first
        discovered and isolated by Scottish physician Daniel Rutherford in 1772.`
  }, {
    position: 8,
    name: 'Oxygen',
    weight: 15.9994,
    symbol: 'O',
    expanded: false,
    description: `Oxygen is a chemical element with symbol O and atomic number 8. It is a member of
         the chalcogen group on the periodic table, a highly reactive nonmetal, and an oxidizing
         agent that readily forms oxides with most elements as well as with other compounds.`
  }, {
    position: 9,
    name: 'Fluorine',
    weight: 18.9984,
    symbol: 'F',
    expanded: false,
    description: `Fluorine is a chemical element with symbol F and atomic number 9. It is the
        lightest halogen and exists as a highly toxic pale yellow diatomic gas at standard
        conditions.`
  }, {
    position: 10,
    name: 'Neon',
    weight: 20.1797,
    symbol: 'Ne',
    expanded: false,
    description: `Neon is a chemical element with symbol Ne and atomic number 10. It is a noble gas.
        Neon is a colorless, odorless, inert monatomic gas under standard conditions, with about
        two-thirds the density of air.`
  },
];


/**  Copyright 2018 Google Inc. All Rights Reserved.
    Use of this source code is governed by an MIT-style license that
    can be found in the LICENSE file at http://angular.io/license */

模板中的更改:

<table mat-table
       [dataSource]="dataSource" multiTemplateDataRows
       class="mat-elevation-z8">
  <ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
    <th mat-header-cell *matHeaderCellDef> {{column}} </th>
    <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
  </ng-container>

  <!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
  <ng-container matColumnDef="expandedDetail">
    <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
      <div class="example-element-detail"
           [@detailExpand]="element.expanded ? 'expanded' : 'collapsed'">
        <div class="example-element-diagram">
          <div class="example-element-position"> {{element.position}} </div>
          <div class="example-element-symbol"> {{element.symbol}} </div>
          <div class="example-element-name"> {{element.name}} </div>
          <div class="example-element-weight"> {{element.weight}} </div>
        </div>
        <div class="example-element-description">
          {{element.description}}
          <span class="example-element-description-attribution"> -- Wikipedia </span>
        </div>
      </div>
    </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
  <tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
      class="example-element-row"
      [class.example-expanded-row]="element.expanded"
      (click)="element.expanded = true">
  </tr>
  <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>


<!-- Copyright 2018 Google Inc. All Rights Reserved.
    Use of this source code is governed by an MIT-style license that
    can be found in the LICENSE file at http://angular.io/license -->

这里有一个工作示例StackBlitz供您参考。

 类似资料:
  • 在Dart(2.15.0)中,我试图通过定义toJson方法将jsonEncode与枚举一起使用。它不起作用。 根据文档,将查找方法。 当直接在枚举上调用时,扩展可以工作,但是不知何故jsonEncode没有找到toJSON。 Dart如何获取枚举的“值”https://dart.dev/guides/language/extension-methods 知道这是一个错误还是预期的行为吗? 否则,

  • 问题内容: 假设我有以下情况: 有没有一种方法可以保证实现的任何类也必须扩展?我不想创建一个抽象类,因为我希望能够以类似的方式混合其他一些接口。 例如: 问题答案: Java接口无法扩展类,这很有意义,因为类包含无法在接口内指定的实现细节。 解决此问题的正确方法是通过将接口也完全从实现中分离出来。所述等可以扩展接口以迫使程序员来实现相应的方法。如果要在所有实例之间共享代码,则可以将(可能是抽象的)

  • 下面是一个示例,其中只发生扩展,但我希望行折叠时,您单击它。 https://stackblitz.com/edit/Angular-Material-expandable-table-rows?file=app%2ftable%2ftable.component.ts 我希望我的桌子是这样的: 请帮帮忙。

  • 我们正在为机器学习应用程序使用python中的h5py处理大型(1.2TB)未压缩、未分块的hdf5文件,这需要我们重复处理完整的数据集,以随机顺序单独加载约15MB的切片。我们正在使用具有192 GB RAM的linux(Ubuntu 18.04)机器。我们注意到该程序正在慢慢填充缓存。当缓存的总大小达到与整机RAM相当的大小时(前0位的可用内存几乎为0,但有大量“可用”内存)交换会减慢所有其他

  • 问题内容: 我想在AJAX和常规HTTP调用之间共享一个模板,唯一的区别是一个模板需要与base.html html一起提供,而另一个则不需要。 任何想法? 问题答案: 使用变量。 然后在您的视图中将其设置为“ base.html”,或者将其设置为一个新的“ ajax.html”文件,该文件仅提供该阻止,而没有其他内容。