角表行作为组件

时间:2019-07-06 14:06:15

标签: angular firebase components

我需要从firebase中读取数据并将其显示为组件,因为我需要显示与每列相对应的 thead 。我尝试了一些不同的html解决方案,但没有结果。

列出组件html:

        <div class="card">
          <div class="card-header card-header-info">
            <h4 class="card-title">Trailers abroad</h4>
            <p class="card-category">Until {{ todayDate }}</p>
          </div>
          <div class="card-body table-responsive">
            <table class="table table-hover">
              <thead>
                <tr>
                  <th>Make</th>
                  <th>Type</th>
                  <th>Number</th>
                  <th>Action</th>
                </tr>
              </thead>
              <tbody>
                <tr
                  app-trailer
                  *ngFor="let trailer of abroadTrailers"
                  [inputTrailer]="trailer"
                ></tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>

列出组件ts:

  export class TrailersListComponent implements OnInit {
  abroadTrailers: Trailer[];
  homeTrailers: Trailer[];
  todayDate;
  myDate;
  user;

  constructor(
    private router: Router,
    public readService: ReadService,
    public loginService: LoginService
  ) {}

  ngOnInit() {
    this.readService.getAbroadTrailers().subscribe(list => {
      this.abroadTrailers = this.readService.processTrailersData(list);
    });

    this.readService.getHomeTrailers().subscribe(list => {
      this.homeTrailers = this.readService.processTrailersData(list);
    });

    this.myDate = new Date();
    this.todayDate = this.dateToString(this.myDate);

    this.loginService.loggedUser.subscribe(currentUser => {
      if (currentUser !== undefined) {
        if (currentUser === null) {
          this.router.navigate(["/login"]);
        } else {
          this.user = currentUser;
        }
      }
    });
  }

  dateToString(date) {
    let month = (date.getMonth() + 1).toString();
    let day = date.getDate().toString();
    const year = date.getFullYear();
    month = month.length < 2 ? "0" + month : month;
    day = day.length < 2 ? "0" + day : day;
    return `${day}/${month}/${year}`;
  }
}

行组件html:

<td>{{ inputTrailer.make }}</td>
<td>{{ inputTrailer.type }}</td>
<td>{{ inputTrailer.number }}</td>
<td class="td-actions">
  <button
    (click)="onSelect()"
    mat-raised-button
    type="button"
    matTooltip="View"
    [matTooltipPosition]="'above'"
    class="btn btn-success btn-link btn-sm btn-just-icon"
  >
    <i class="material-icons">remove_red_eye</i>
  </button>
  <button
    (click)="editTrailer()"
    mat-raised-button
    type="button"
    matTooltip="Edit Task"
    [matTooltipPosition]="'above'"
    class="btn btn-primary btn-link btn-sm btn-just-icon"
  >
    <i class="material-icons">edit</i>
  </button>
  <button
    data-target="#delete_trailer"
    data-toggle="modal"
    mat-raised-button
    type="button"
    matTooltip="Remove"
    [matTooltipPosition]="'above'"
    class="btn btn-danger btn-link btn-sm btn-just-icon"
  >
    <i class="material-icons">close</i>
  </button>
  <div class="modal fade" id="delete_trailer" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">
            &times;
          </button>
        </div>
        <div class="modal-body">
          <p class="textModal">
            Are you sure you want to delete this trailer?
          </p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">
            Cancel
          </button>
          <button
            type="button"
            class="btn btn-danger ml-2"
            data-dismiss="modal"
            (click)="deleteTrailer()"
          >
            Delete
          </button>
        </div>
      </div>
    </div>
  </div>
</td>

行组件ts:

  @Component({
  selector: "[app-trailer]",
  templateUrl: "./trailer.component.html",
  styleUrls: ["./trailer.component.css"]
})
export class TrailerComponent implements OnInit {
  @Input() inputTrailer: Trailer;

  constructor(private router: Router, private deleteService: DeleteService) {}

  ngOnInit() {}

  onSelect() {
    this.router.navigate(["/trailer-preview/", this.inputTrailer.key]);
  }

  deleteTrailer() {
    this.deleteService.deleteTrailer(this.inputTrailer.key);
    this.router.navigate(["/trailers"]);
  }

  editTrailer() {
    this.router.navigate(["/trailer-edit/", this.inputTrailer.key]);
  }
}

这是现在的样子: current table

我需要显示与每个行列相对应的表头列。

这是检查的外观: enter image description here enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 列表组件html中的替换表结构如下
<table class="table table-hover">
    <thead>
      <tr>
        <th>Make</th>
        <th>Type</th>
        <th>Number</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
        <tr app-trailer *ngFor="let trailer of abroadTrailers" [inputTrailer]="trailer"></tr>
    </tbody>
</table>
  1. 在行组件html中删除<tr></tr>标签。

  2. 行组件中的
  3. ts对此进行了更改

@Component({
  selector: 'app-trailer',
  ....
})

对此

@Component({
  selector: '[app-trailer]',
  ....
})

这是一个简单的演示https://stackblitz.com/edit/angular-z45yab