角度材质数据表显示[对象对象]

时间:2018-02-12 21:47:20

标签: javascript angular express datatable angular-datatables

我正在尝试使用Express和Firebase DB实现数据表组件。

以下是我的服务请求数据的方式

withCredentials: true

console.log打印:

gettext() {
  return this.http.get<nomchamp[]>(this.url)
      .map(res => { console.log(res); return res });
}

我创建了一个模型

[{…}]
       0: {text: "testoooo"}
       length: 1__proto__: Array(0)

这是我的数据表组件

export interface nomchamp {
    text: String;
}

和html:

export class DatatableComponent implements OnInit {

  constructor(private dataService: AjouttextService) { }
  data = [];

  displayedColumns = ['text'];
  dataSource = new UserDataSource(this.dataService);
  ;

  applyFilter(filterValue: string) {
    filterValue = filterValue.trim(); // Remove whitespace
    filterValue = filterValue.toLowerCase(); // MatTableDataSource defaults to lowercase matches
  }



  ngOnInit() {
  }
}
export class UserDataSource extends DataSource<any> {
  constructor(private dataService: AjouttextService) {
    super();
  }
  connect(): Observable<nomchamp[]> {
    return this.dataService.gettext()
  }
  disconnect() { }
}

在我的快速发布路线中,我推送了一个数据对象,然后我在get route中调用它

<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" -->

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



    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
  </mat-table>
</div>

在我的数据表中,我得到了这个预览

router
  .route("/")
  .get(function (req, res, err) {

    // Get a database reference to our posts
    var db = admin.database();
    var ref = db.ref("/");

    // Attach an asynchronous callback to read the data at our posts reference
    ref.on("value", function (snapshot) {
      res.send(snapshot.val());

      console.log(snapshot.val());
    }, function (errorObject) {
      console.log("The read failed: " + errorObject.code);
    });
  });

router
  .route("/")
  .post(function (req, res, err) {

    // Get a database reference to our posts
    var db = admin.database();
    var ref = db.ref("/");

    // Attach an asynchronous callback to read the data at our posts reference
    ref.set(
      [{ text: "testoooo" }]
    );
  });

enter image description here

我找到了同样的问题here,但收到的数据格式与问题不一样,因为我没有数组 这是邮递员发送的内容

[object Object]

1 个答案:

答案 0 :(得分:1)

可以通过订阅将数据从http或任何其他返回observable的服务传递给Angular Material Table组件(或其他表示控件)。请参阅我的stackblitz示例:Presenting Tabular Data Sets Using Angular Material Table Component and Observable