1

我正在尝试从服务中获取数据并使用Covalent在数据表中显示它们,唯一的问题我似乎不知道如何去做。最初是使用带有数据的 twitter 引导程序,它工作正常。我正在使用共价键进入材料设计 2,这是唯一被卡住的部分。

这是我的组件

getSpReceiptsList(){
  //console.log(this.webApiPathService.getWebApiPath('get-sp-receipts').path);
  this.getReceiptService.getReceipts( this.webApiPathService.getWebApiPath('get-sp-receipts').path, this.token)
  .subscribe(response => {
      if(response){
          this.spReceiptsList = this.getReceiptService.getReceiptsList();
          this.spReceiptsList.forEach((object)=>{
              let paymentDate = new Date(object.paymentDate);
              let year = paymentDate.getFullYear();
              let month = paymentDate.getMonth()+1;
              let dt = paymentDate.getDate();    
              let hours = paymentDate.getHours();
              let minutes = paymentDate.getMinutes();
              let seconds = paymentDate.getSeconds();
              object.paymentDate = dt+'-' + month + '-'+year+' '+hours+':' + minutes + ':'+seconds;
          });

      }else{
          this.snackBar.open('No receipts exist', '', {
                  duration: 2000,
          });
      }
  }, 
  errMsg => {
      this.snackBar.open(errMsg, '', {
          duration: 2000,
      });
      console.log(errMsg);
  });
}

  //Covalent
  data: any[] = [
    { approve: 'True', serviceName: 'Hotel', userName: 'gbrigens', amount: 1000, paymentDate: '12/08/2017' },
  ];
  columns: ITdDataTableColumn[] = [
    { name: 'approve', label: 'APPROVE', tooltip: 'Approve' },
    { name: 'serviceName', label: 'SERVICE NAME' },
    { name: 'userName', label: 'USER NAME' },
    { name: 'amount', label: 'AMOUNT' },
    { name: 'paymentDate', label: 'PAYMENT DATE' }
  ];

  filteredData: any[] = this.data;
  filteredTotal: number = this.data.length;

  searchTerm: string = '';
  fromRow: number = 1;
  currentPage: number = 1;
  pageSize: number = 5;
  sortBy: string = 'approve';
  selectedRows: any[] = [];
  sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Descending;

  sort(sortEvent: ITdDataTableSortChangeEvent): void {
    this.sortBy = sortEvent.name;
    this.sortOrder = sortEvent.order;
    this.filter();
  }

  search(searchTerm: string): void {
    this.searchTerm = searchTerm;
    this.filter();
  }

  page(pagingEvent: IPageChangeEvent): void {
    this.fromRow = pagingEvent.fromRow;
    this.currentPage = pagingEvent.page;
    this.pageSize = pagingEvent.pageSize;
    this.filter();
  }

  filter(): void {
    let newData: any[] = this.data;
    let excludedColumns: string[] = this.columns
    .filter((column: ITdDataTableColumn) => {
      return ((column.filter === undefined && column.hidden === true) ||
              (column.filter !== undefined && column.filter === false));
    }).map((column: ITdDataTableColumn) => {
      return column.name;
    });
    newData = this._dataTableService.filterData(newData, this.searchTerm, true, excludedColumns);
    this.filteredTotal = newData.length;
    newData = this._dataTableService.sortData(newData, this.sortBy, this.sortOrder);
    newData = this._dataTableService.pageData(newData, this.fromRow, this.currentPage * this.pageSize);
    this.filteredData = newData;
  }

显示表格的模板

<div layout="row" layout-align="start center" class="pad-left-sm pad-right-sm">
  <span *ngIf="!searchBox.searchVisible" class="push-left-sm">
    <span class="md-title">Receipts</span>
  </span>
  <span *ngIf="!searchBox.searchVisible" class="push-left-sm">
    <span *ngIf="(selectable && !selectedRows.length) || !selectable" class="md-title">Receipts</span>
    <span *ngIf="selectedRows.length && selectable" class="md-body-1">0 item(s) selected</span>
  </span>
  <td-search-box #searchBox backIcon="arrow_back" class="push-right-sm" placeholder="Search here" (searchDebounce)="search($event)" flex>
  </td-search-box>
</div>
<md-divider></md-divider>
<td-data-table
  #dataTable
  [data]="filteredData"
  [columns]="columns"
  [selectable]="selectable"
  [clickable]="clickable"
  [multiple]="multiple"
  [sortable]="true"
  [sortBy]="sortBy"
  [(ngModel)]="selectedRows"
  [sortOrder]="sortOrder"
  (sortChange)="sort($event)">
</td-data-table>
<div class="md-padding" *ngIf="!dataTable.hasData" layout="row" layout-align="center center">
  <h3>No results to display.</h3>
</div>
<td-paging-bar #pagingBar [pageSize]="pageSize" [total]="filteredTotal" (change)="page($event)">
  <span hide-xs>Rows per page:</span>
  <md-select [style.width.px]="50" [(ngModel)]="pageSize">
    <md-option *ngFor="let size of [5,10,15,20]" [value]="size">
      {{size}}
    </md-option>
  </md-select>
  {{pagingBar.range}} <span hide-xs>of {{pagingBar.total}}</span>
</td-paging-bar>

最后是我的服务

getReceipts(path: string, token: string): Observable<Boolean>{
      let headers = new Headers();
      headers.append('Content-Type', 'application/json');
      headers.append('x-access-token', token);
      headers.append('Accept', 'application/json');
      let requestoptions = new RequestOptions({
          headers: headers
      });


      let urlPath: string = this.authUrl + path;
      console.log(urlPath);
      return this.http
          .get(urlPath, requestoptions)
          .map((res: Response) => {  
              let test: Array<SpReceiptModel> = res.json();        
              this.spReceiptList = test;
              if(this.spReceiptList.length !== 0){
                  return true;
              }else{
                  return false;
              }
          })
          .catch((err) => this.handleError(err));
  }
4

1 回答 1

1

在您的组件中,您错过了调用 filter() 函数来更新数据表中的过滤数据,您可以像这样定义属性数据:

[数据]="过滤数据"

然后在您的组件中需要调用

this.filter()

在这个 forEach 的结尾

 this.spReceiptsList.forEach((object)=>{
          let paymentDate = new Date(object.paymentDate);
          let year = paymentDate.getFullYear();
          let month = paymentDate.getMonth()+1;
          let dt = paymentDate.getDate();    
          let hours = paymentDate.getHours();
          let minutes = paymentDate.getMinutes();
          let seconds = paymentDate.getSeconds();
          object.paymentDate = dt+'-' + month + '-'+year+' '+hours+':' + minutes + ':'+seconds;
      });
 this.filter();

好吧,您错过了更新该变量,或者您可以设置变量 filteredData 而不是调用 this.filter()

   this.filteredData = this.spReceiptsList;
于 2017-08-30T00:48:14.400 回答