gpt4 book ai didi

Angular Material 2 数据表连接到 AngularFire2 或 Firebase 服务?

转载 作者:太空狗 更新时间:2023-10-29 17:12:43 26 4
gpt4 key购买 nike

我希望它只是即插即用 :-) 我已经为此折腾了几个小时,但我的小实验都没有成功。 md数据表是新的,所以网上几乎没有什么神学知识。找到一种将 Firebase 连接到表格的好方法将是一个好的开始。有什么想法吗?

目前我有这个设置。我的代码在没有带有标准 Angular 设置和代码的表的情况下运行良好,使用 ngFor 并从模板创建列表。因此代码使用 AngularFire 2 从 Firebase 传送数据。尝试新的 md 数据表是问题所在。

首先,模板不会呈现。我知道我已经正确设置了 NgModule,所以我怀疑数据源没有连接并产生了这个错误。这是 Chrome 控制台中的错误。

Template parse errors:
Can't bind to 'dataSource' since it isn't a known property of 'md-table'.
1. If 'md-table' is an Angular component and it has 'dataSource' input, then verify that it is part of this module.

除了我更改了模板绑定(bind)之外,我的 members-search.component.html 看起来与官方文档相同:

<md-table #table [dataSource]="dataSource">

<ng-container cdkColumnDef="memberName">
<md-header-cell *cdkHeaderCellDef> Name </md-header-cell>
<md-cell *cdkCellDef="let row"> {{member.firstName}} {{ member?.lastName }} </md-cell>
</ng-container>

members-search.component.ts 具有以下相关部分:

import { DataSource } from '@angular/cdk';

@Injectable()
export class MembersAdminService {

private members$: FirebaseListObservable<Member[]>;
private dataSource: DataSource<any>;

constructor(
private af: AngularFireDatabase,
@Inject(FirebaseApp) fb) {
this.members$ = af.list('Members');
}

我将这些数据表函数放到了 members-search.service.ts 中的工作代码中,并在 connect() 中使用了我一直在该服务的其他地方使用的相同代码。

// md table dataSource functions.
public connect(): FirebaseListObservable<any> {
return this.af.list('Members', {
query: {
orderByChild: 'lastName'
}
});
// return this._exampleDatabase.dataChange;
}

public disconnect() {}

数据表文档和 plunker 在组件中创建数据源和数据库,但如果我已经拥有 Firebase,那么其中大部分似乎都不是必需的。我正在学习所有这些,所以我远不是任何方面的专家,也许我遗漏了一些东西。

如果您之前没有接触过这个新设置,那么这里是文档。 md 表建立在 cdk 表之上,为 cdk 表提供样式。

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

https://material.angular.io/guide/cdk-table

最佳答案

我添加了在使用 MD 数据表的 MD 分页器时连接到 Firebase 的代码。 Paginator 使服务中的代码更加复杂。大多数代码都在它所属的服务中。享受吧!

成员-admin.service.ts

import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { FirebaseApp } from 'angularfire2';
import { Inject, Injectable } from '@angular/core';

import { MemberModel } from './member-admin.model';
import { SuccessService } from '../../../shared/success.service';

// Data Table imports.
import { MdPaginator } from '@angular/material';
import { DataSource } from '@angular/cdk';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/observable/merge';
import 'rxjs/add/observable/combineLatest';
import 'rxjs/add/operator/map';


@Injectable()
export class MembersAdminService {

private membersData$: FirebaseListObservable<MemberModel[]>;

constructor(
public af: AngularFireDatabase,
private successService: SuccessService,

// For Create and Update functions.
@Inject(FirebaseApp) fb) {
this.membersData$ = af.list('Members');
}

// ... CRUD stuff not relevant to the MD Table ...


// *** MD DATA TABLE SERVICES. ***


@Injectable()
export class MemberDatabase {

/* Stream that emits whenever the data has been modified. */
public dataChange: BehaviorSubject<MemberModel[]> = new BehaviorSubject<MemberModel[]>([]);
get data(): MemberModel[] {
return this.dataChange.value; }

// Connection to remote db.
private database = this.memberAdminService.af.list('Members', {
query: {
orderByChild: 'lastName'
}
});
public getMembers(): FirebaseListObservable<MemberModel[]> {
return this.database;
}


constructor(private memberAdminService: MembersAdminService) {
this.getMembers()
.subscribe(data => this.dataChange.next(data));
}
}


@Injectable()
export class MembersAdminSource extends DataSource<MemberModel> {


constructor(
private memberDatabase: MemberDatabase,
private paginator: MdPaginator) {
super();
}


/** Connect function called by the table to retrieve one stream containing the data to render. */
connect(): Observable<MemberModel[]> {

const displayDataChanges = [
this.memberDatabase.dataChange,
this.paginator.page,
];

return Observable
.merge(...displayDataChanges) // Convert object to array with spread syntax.
.map(() => {
const dataSlice = this.memberDatabase.data.slice(); // Data removed from viewed page.

// Get the page's slice per pageSize setting.
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;

const dataLength = this.paginator.length; // This is for the counter on the DOM.

return dataSlice.splice(startIndex, this.paginator.pageSize);
});
}
disconnect() {}
}

所有成员.component.ts

ngOnInit 和类属性中进行了一些重构。

import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { Subject } from 'rxjs/Subject';

// For MD Data Table.
import { MdPaginator } from '@angular/material';
import { MembersAdminService, MembersAdminSource, MemberDatabase } from './member-admin.service';

import { ConfirmService } from '../../../shared/confirm.service';
import { MemberModel } from './member-admin.model';


@Component({
selector: 'app-all-members',
templateUrl: './all-members.component.html'
})


export class AllMembersComponent implements OnInit {

membersData: MemberModel[];
private result: boolean;
allMembers: MemberModel[];

// For search
startAt = new Subject();
endAt = new Subject();
lastKeypress: 0;

// For MD data table.

// private memberDatabase = new MemberDatabase(); // Requires a param but? Moved to constructor.
private dataSource: MembersAdminSource | null;
private displayedColumns = [
'firstName',
'lastName',
'mainSkillTitle',
'mainSkills',
'delete',
'key'
];

@ViewChild(MdPaginator)
paginator: MdPaginator;

public dataLength: any; // For member counter on DOM.

constructor(
private membersAdminService: MembersAdminService,
private memberDatabase: MemberDatabase,
private router: Router,
private confirmService: ConfirmService
) {}

ngOnInit() {

this.memberDatabase.getMembers()
.subscribe(members => {
this.dataSource = new MembersAdminSource(this.memberDatabase, this.paginator);
this.dataLength = members;
});
}

所有成员.component.html

请注意,我在行中有用于删除和编辑的按钮,它们工作正常。诀窍是您需要隐藏列中的数据库 key 。

<md-table #table [dataSource]="dataSource">

<!-- First Name Column -->
<ng-container cdkColumnDef="firstName">
<md-header-cell *cdkHeaderCellDef> First Name </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.firstName}} </md-cell>
</ng-container>

<!-- Las Name Column -->
<ng-container cdkColumnDef="lastName">
<md-header-cell *cdkHeaderCellDef> Last Name </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.lastName}} </md-cell>
</ng-container>

<!-- Title Column -->
<ng-container cdkColumnDef="mainSkillTitle">
<md-header-cell *cdkHeaderCellDef> Title </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.mainSkillTitle}} </md-cell>
</ng-container>

<!-- Main Skills Column -->
<ng-container cdkColumnDef="mainSkills">
<md-header-cell *cdkHeaderCellDef> Main Skills </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.mainSkills}} </md-cell>
</ng-container>

<!-- Delete Buttons Column -->
<ng-container cdkColumnDef="delete">
<md-header-cell *cdkHeaderCellDef> Delete / Edit </md-header-cell>
<md-cell *cdkCellDef="let row">
<button (click)="deleteMember(row.$key)">Delete</button>
<button (click)="goToDetailPage(row.$key)">Edit</button>
</md-cell>
</ng-container>

<!-- Database key Column -->

<ng-container cdkColumnDef="key">
<md-header-cell *cdkHeaderCellDef class="hiddenField"> Key </md-header-cell>
<md-cell *cdkCellDef="let row" class="hiddenField"> {{row.$key}} </md-cell>
</ng-container>


<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>


</md-table>
<md-paginator #paginator
[length]="dataLength?.length"
[pageIndex]="0"
[pageSize]="5"
[pageSizeOptions]="[5, 10, 25, 100]">
</md-paginator>

关于Angular Material 2 数据表连接到 AngularFire2 或 Firebase 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45336706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com