角材料表可扩展表行未扩展?
我盯着这个眼睛已经太久了,我想不通.
I've been staring at this for too long, and I can't figure it out.
在这个stackoverflow中有一个很好的示例Angular Material表,其中带有可扩展行
There's a good example of an Angular Material table with an expandable row in this stackoverflow answer. I can plug the example into my app, and it works correctly.
我无法将示例应用于我的数据.我认为我的问题出在HTML中,因为表格已呈现,因此似乎已应用扩展行CSS(单击第一行时行描述消失了),但是扩展行却没有出现.
I'm failing to apply the example to my data. I think my problem is in the HTML, since the table renders, the expanded row CSS seems to get applied (the row delineation disappears on clicking the first row), but the expansion row does not appear.
有人看到这个问题吗?预先感谢.
Does anyone see the problem? Thanks in advance.
这是我所拥有的 StackBlitz .我已经从链接的SO answer 与我坏掉的例子并列
Here's a StackBlitz of what I have. I've put the working example from the linked SO answer in side by side with my broken example
./tweets-table/tweets-table.component.ts:
./tweets-table/tweets-table.component.ts:
import { Component, OnInit } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations';
import { Tweet } from '../tweet';
import { TweetService } from '../tweet.service';
import { TweetsDataSource } from '../tweets.datasource';
import { data } from '../mock-tweets';
@Component({
selector: 'app-tweets-table',
templateUrl: './tweets-table.component.html',
styleUrls: ['./tweets-table.component.css'],
animations: [
trigger('detailExpand', [
state('collapsed', style({ height: '0px', minHeight: '0', visibility: 'hidden' })),
state('expanded', style({ height: '*', visibility: 'visible' })),
transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
]),
],
})
export class TweetsTableComponent implements OnInit {
// Data
displayedColumns = [
'createdTime', 'tweetText', 'favoriteCount'
];
tweets: Tweet[];
expandedTweet: any;
dataSource: TweetsDataSource|null;
constructor(private tweetService: TweetService) {
}
ngOnInit() {
this.getTweets();
}
getTweets(): void {
// this.tweets = this.tweetService.getTweets(this.pageIndex, this.pageSize);
this.tweets = data;
this.dataSource = new TweetsDataSource(this.tweets);
}
isExpansionDetailRow = (i: number, row: Object) => {
row.hasOwnProperty('detailRow');
}
}
./tweets-table/tweets-table.component.html:
./tweets-table/tweets-table.component.html:
<div class="example-container mat-elevation-z8">
<mat-table #table [dataSource]="dataSource">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Time Column -->
<ng-container matColumnDef="createdTime">
<mat-header-cell *matHeaderCellDef> Tweet Time </mat-header-cell>
<mat-cell *matCellDef="let tweet"> {{tweet.created_time}} </mat-cell>
</ng-container>
<!-- Text Column -->
<ng-container matColumnDef="tweetText">
<mat-header-cell *matHeaderCellDef> So Texty </mat-header-cell>
<mat-cell *matCellDef="let tweet"> {{tweet.text}} </mat-cell>
</ng-container>
<!-- Favs Column -->
<ng-container matColumnDef="favoriteCount">
<mat-header-cell *matHeaderCellDef> Flava Favs </mat-header-cell>
<mat-cell *matCellDef="let tweet"> {{tweet.favorite_count}} </mat-cell>
</ng-container>
<!-- Expanded Content Column - The detail row is made up of this one column -->
<ng-container matColumnDef="expandedDetail">
<mat-cell *matCellDef="let detail">
Walk before you run. Here's more text: {{detail.tweet.text}}
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
matRipple
class="element-row"
[class.expanded]="expandedTweet == row"
(click)="expandedTweet = row"></mat-row>
<mat-row *matRowDef="let row; columns: ['expandedDetail']; when: isExpansionDetailRow"
[@detailExpand]="row.element == expandedTweet ? 'expanded' : 'collapsed'"
style="overflow: hidden">
</mat-row>
</mat-table>
</div>
我认为我的DataSource对象还可以.但是,我很难完全了解MatDataSource,所以我也将其包括在内.
I think my DataSource object is okay. However, I'm having a hard time getting a full understanding of MatDataSource, so I'll include that, too.
./tweets.datasource.ts:
import { Component, OnInit } from '@angular/core';
import { DataSource } from '@angular/cdk/collections';
import { Observable, of } from 'rxjs';
import { Tweet } from './tweet';
import { TweetService } from './tweet.service';
export class TweetsDataSource extends DataSource<Tweet> {
constructor(private tweets: Tweet[]) {
super();
}
connect(): Observable<Tweet[]> {
const rows = [];
this.tweets.forEach(element => rows.push(element, { detailRow: true, element }));
console.log('dataset rows:')
console.log(rows);
return of(this.tweets);
}
disconnect() { }
}
tweets.datasource.ts
在connect方法中,您返回错误的内容.它应该返回行,而不是 this.tweets .
In the connect method you are returning the wrong stuff. It should return rows instead of this.tweets.
connect(): Observable<Tweet[]> {
const rows = [];
this.tweets.forEach(tweet => rows.push(tweet, { detailRow: true, tweet }));
console.log('dataset rows:')
console.log(rows);
return of(rows);
}
tweets-table.component.ts
这也有一个错误,方法 isExpansionDetailRow 应该返回布尔值true或false(不执行任何操作).因此,添加收益.原始代码不需要返回,因为它只使用一行而不使用方括号.
This also has a error the method isExpansionDetailRow should return a boolean value true or false (Not do nothing). So add the return. The original code does not need the return because it just uses one line without the brackets.
如果箭头函数仅返回一行代码,则可以省略语句括号和return关键字.这告诉箭头函数返回该语句.参考=> https://codeburst.io/javascript-understand-arrow-function -syntax-ab4081bba85b
If an arrow function is simply returning a single line of code, you can omit the statement brackets and the return keyword. This tells the arrow function to return the statement. Reference => https://codeburst.io/javascript-understand-arrow-function-syntax-ab4081bba85b
isExpansionDetailRow = (i: number, row: Object) => {
return row.hasOwnProperty('detailRow');
}
如果您对执行此操作的另一种方法感兴趣,可以参考此问题.
If you are interested in another way of doing this you can refer to this issue.