我正在开发一个Angular 2应用程序,我需要在列表中更改列的顺序,单击标题(作为数据表工作),我从Angularfire 2文档示例中获取一个想法,这里是我的代码:(grupos.component.ts)
import { Component, OnInit } from '@angular/core';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import { Subject } from 'rxjs/Subject';
import {AF} from "../providers/af";
import { Router } from "@angular/router";
{ EditGpoComponent } from '../edit-gpo/edit-gpo.component';
@Component({
selector: 'app-grupos',
templateUrl: './grupos.component.html',
styleUrls: ['./grupos.component.css']
})
export class GruposComponent implements OnInit {
public newMessage: string;
eventos: FirebaseListObservable<any[]>;
sizeSubject: Subject <any>;
constructor( af: AngularFire, private router: Router ) {
this.sizeSubject = new Subject();
this.eventos = af.database.list('/eventos', {
query: {
orderByChild: this.sizeSubject
}
});
}
filterBy(size: string) {
this.sizeSubject.next(size);
}
editaGrupo(keyGrupo){
this.router.navigate(['/add-gpo']);
}
ngOnInit() {
}
}
以下是我的Grupos.components.html的代码:
<div class="container">
<br><br><br>
<div class="row">
<a href="#" class="btn btn-info" routerLink="/add-gpo">+Add New Event</a>
<br><br>
<table class="table table-striped">
<thead>
<tr>
<th><button (click)="filterBy('evento')">EVENT</button></th>
<th><button (click)="filterBy('arrival')">ARRIVAL</button></th>
<th><button (click)="filterBy('departure')">DEPARTURE</button></th>
<th><button (click)="filterBy('hotel')">VENUE</button></th>
<th><button (click)="filterBy('pax')">PAX</button></th>
<th>FUNCTIONS</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let evento of eventos | async ">
<td>{{evento.evento}}</td>
<td>{{evento.arrival}}</td>
<td>{{evento.departure}}</td>
<td>{{evento.hotel}}</td>
<td>{{evento.pax}}</td>
<td style="font-size: 1.2em">
<a href="#" [routerLink]="['/editGpo']" [queryParams]="{eveId: evento.$key}"><span class="glyphicon glyphicon-edit" aria-hidden="true" title="edit event"></span></a>
<a href="#"><span class="glyphicon glyphicon-user" aria-hidden="true" title="attendees"></span></a>
<a href="#"><span class="glyphicon glyphicon-bullhorn" aria-hidden="true" title="speakers"></span></a>
<a href="#"><span class="glyphicon glyphicon-trash glyphicon " aria-hidden="true" title="delete event"></span></a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
正如你看到它的工作非常酷,每次我点击列标题按钮,我的表都会得到该列的顺序。但我一直在寻找如何给&#34; sizeSubject&#34; (主题),我喜欢在&#34; evento&#34;上分配值,因此当我显示时,表格按照evento排序。现在只需向我显示按钮标题和空白表格,如果我点击任何按钮,它会按顺序显示表格。
任何提示或评论都将不胜感激!
答案 0 :(得分:36)
如果你想要一个具有&#39;初始值&#39;的主题,一个更好的RxJS类就是使用BehaviorSubject。
使用BehaviorSubject,您可以将初始值传递给其构造函数。
答案 1 :(得分:0)
除了使用 BehaviorSubject,您还可以使用普通的 Subject 并使用 Rxjs 运算符 startWith
来设置初始值。