简体   繁体   中英

Angular Material datatable show [object Object]

I'm trying to implement datatable component using Express and Firebase DB.

Here's how my service request data

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

console.log prints :

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

I have created a model

export interface nomchamp {
    text: String;
}

Here's my datatable component

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() { }
}

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

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

In my express post route I push an object of data then I call it in the get route

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" }]
    );
  });

In my datatable I get this preview

[object Object]

在此处输入图片说明

I have found the same question here but the format of received data is not the same as in question because I don't have an array Here's what postman send

[
      {
          "text": "testoooo"
      }
]

It is possible to assign/pass data from http, or any other service that returns an observable, to the Angular Material Table Component (or other presentation control) by subscribing. See my stackblitz example: Presenting Tabular Data Sets Using Angular Material Table Component and Observable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM