【问题标题】:Mat-table with headers in two rows, using Angular 8使用Angular 8的两行标题的Mat-table
【发布时间】:2020-02-19 13:39:31
【问题描述】:

我正在使用 mat-table 在表格中显示信息。我有这样的事情:

material table

使用此 HTML:

  <div class="container">
   <div class="mat-elevation-z8">
    <table #dataTable mat-table [dataSource]="dataSource">
      <ng-container class="ng-hide" matColumnDef="$key">
        <th class="ng-hide" mat-header-cell *matHeaderCellDef>ID del Paciente</th>
        <td class="ng-hide" mat-cell *matCellDef="let element"> {{element.$key}} </td>
      </ng-container>

      <ng-container matColumnDef="nombre_paciente">
        <th mat-header-cell *matHeaderCellDef> Nombre </th>
        <td mat-cell *matCellDef="let element"> {{element.nombre_paciente}}</td>
      </ng-container>

      <ng-container matColumnDef="apellido_paciente">
        <th mat-header-cell *matHeaderCellDef> Apellido </th>
        <td mat-cell *matCellDef="let element"> {{element.apellido_paciente}} </td>
      </ng-container>

      <ng-container matColumnDef="dni">
        <th mat-header-cell *matHeaderCellDef> DNI </th>
        <td mat-cell *matCellDef="let element"> {{element.dni}} </td>
      </ng-container>

      <ng-container matColumnDef="fecha_nacimiento">
        <th mat-header-cell *matHeaderCellDef> Fecha de Nacimiento </th>
        <td mat-cell *matCellDef="let element"> {{element.fecha_nacimiento | date: "dd/MM/yyyy" }</td>
      </ng-container>

      <ng-container matColumnDef="obra_social">
        <th mat-header-cell *matHeaderCellDef> Obra Social </th>
        <td mat-cell *matCellDef="let element"> {{element.obra_social}} </td>
      </ng-container>

      <ng-container matColumnDef="numero_os">
        <th mat-header-cell *matHeaderCellDef> Número de Obra Social </th>
        <td mat-cell *matCellDef="let element"> {{element.numero_os}} </td>
      </ng-container>

      <ng-container matColumnDef="telefono">
        <th mat-header-cell *matHeaderCellDef> Número de teléfono </th>
        <td mat-cell *matCellDef="let element"> {{element.telefono}} </td>
      </ng-container>

      <ng-container matColumnDef="telefono_contacto">
        <th mat-header-cell *matHeaderCellDef> Teléfono de Contacto </th>
        <td mat-cell *matCellDef="let element"> {{element.telefono_contacto}} </td>
      </ng-container>

      <ng-container matColumnDef="accion">
        <th mat-header-cell *matHeaderCellDef></th>
        <td mat-cell *matCellDef="let element; let i = index;">
          <button mat-raised-button color="primary" class="push-right"
            [routerLink]="['/PacienteEdita/', element.$key]">Editar</button>
          <button mat-raised-button color="warn" (click)="deletePaciente(i, element)">Eliminar</button>
        </td>
      </ng-container>

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

    </table>
  </div>
</div>

还有这个 TS:

import { Component, OnInit, ViewChild } from '@angular/core';
import { Paciente } from './../../shared/paciente';
import { MatTableDataSource, MatTable } from '@angular/material';
import { PacienteService } from './../../shared/paciente.service';
import { ActivatedRoute } from '@angular/router';


@Component({
  selector: 'app-pacientes-detalle',
  templateUrl: './pacientes-detalle.component.html',
  styleUrls: ['./pacientes-detalle.component.css']
})
export class PacientesDetalleComponent implements OnInit {
  id: any;
  element: Paciente;
  dataSource: MatTableDataSource<Paciente[]>;
  displayedColumns: any[] = [
    '$key',
    'nombre_paciente',
    'apellido_paciente',
    'dni',
    'fecha_nacimiento',
    'obra_social',
    'numero_os',
    'telefono',
    'telefono_contacto',
    'accion'
  ];
  @ViewChild('dataTable',{ static:false }) dataTable: MatTable<any>;
  constructor(
    private actRoute: ActivatedRoute, 
    private pacienteApi: PacienteService
    ) { 
  }

  ngOnInit() {
    this.id = this.actRoute.snapshot.paramMap.get('id');
    this.pacienteApi.GetPaciente(this.id).valueChanges().subscribe(data => {
      /* Data table */
      this.element = data;
      this.element.$key = this.id;
      this.dataSource = new MatTableDataSource<Paciente[]>([data]);
    })
  }
}

我正在尝试使用 mat-table 和 angular 8 得到类似这样的东西:

table without material

问题是我无法让它与displayedColumns 一起使用。我尝试使用两个不同的显示列,但对我不起作用。

有什么建议吗?

【问题讨论】:

    标签: angular angular-material angular8 mat-table


    【解决方案1】:

    也许这会有所帮助。

    <ng-container matColumnDef="nombre_paciente">
        <td mat-cell *matCellDef="let element"> 
           <div class="inner-col-header">Nombre<div>
           <div class="inner-col-data">{{element.nombre_paciente}}<div>
           <div class="inner-col-header">Obra Social<div>
           <div class="inner-col-data">{{element.obra_social}}<div>
        </td>
    </ng-container>
    

    您可以使用类 inner-col-data 和 inner-col-header 来格式化您的数据,并删除未使用的列(在本例中为 Obra Social)

    【讨论】:

    • 好主意,但它不适合我。它仅显示第一个数据(在本例中为 Nombre),但未显示第二个标题和数据(Obra Social)。控制台上显示的错误:ERROR 错误:缺少页眉、页脚和行的定义;无法确定应呈现哪些列。
    • 好的。解决了先前缺少定义的错误。现在,它只显示第一行标题和数据,而不显示第二行。控制台上没有显示错误
    【解决方案2】:

    感谢您的回答和帮助。最后我通过创建一个公用表并向其添加材质 CSS 解决了这个问题!

    HTML

     <div class="container">
      <div class="mat-elevation-z8">
        <table class="mat-table">
          <tr class="mat-header-row">
            <th class="mat-header-cell">Nombre</th>
            <th class="mat-header-cell">Apellido</th>
            <th class="mat-header-cell">DNI</th>
            <th class="mat-header-cell">Fecha de Nacimiento</th>
          </tr>
          <tr class="mat-row">
            <td class="mat-cell">{{element.nombre_paciente}}</td>
            <td class="mat-cell">{{element.apellido_paciente}}</td>
            <td class="mat-cell">{{element.dni}}</td>
            <td class="mat-cell">{{element.fecha_nacimiento | date: "dd/MM/yyyy"}}</td>
          </tr>
          <tr class="mat-header-row">
            <th class="mat-header-cell">Obra Social</th>
            <th class="mat-header-cell">Número de Obra Social</th>
            <th class="mat-header-cell">Número de Teléfono</th>
            <th class="mat-header-cell">Número de Contácto</th>
          </tr>
          <tr class="mat-row">
            <td class="mat-cell">{{element.obra_social}}</td>
            <td class="mat-cell">{{element.numero_os}}</td>
            <td class="mat-cell">{{element.telefono}}</td>
            <td class="mat-cell">{{element.telefono_contacto}}</td>
          </tr>
          <tr>
            <td colspan="4" style="width: 100%;">
            <div>
              <button mat-button matTooltip="Ver la ficha del paciente" (click)="fichaDialog()">Ficha</button>
              <button mat-button matTooltip="Ver observaciones del paciente" (click)="observacionesDialog()">Observaciones</button>
              <button mat-button matTooltip="Ver tratamientos del paciente"(click)="tratamientosDialog()">Tratamientos</button>
              <button mat-button matTooltip="Ver turnos del paciente" (click)="turnosDialog()">Turnos</button>
            </div>
          </td>
          </tr>
          <tr style="justify-content: center;">
            <td colspan="4" style="width: 100%;">
            <div class="footer">
              <button mat-raised-button  color="primary" class="push-right" style="width: 83.11px;"
                [routerLink]="['/PacienteEdita/',id]">Editar</button>
              <button mat-raised-button  color="warn" (click)="deletePaciente(id, element)">Eliminar</button>
            </div>
          </td>
          </tr>
        </table>
      </div>
    </div>
    

    CSS

    table.mat-table {
      border-spacing: 0;
      width: 100%;
    }
    
    tr.mat-header-row {
      height: 56px;
    }
    
    tr.mat-row, tr.mat-footer-row {
      height: 48px;
      text-align: center;
    }
    
    th.mat-header-cell {
      text-align: center;
      width: 25%;
    }
    
    th.mat-header-cell, td.mat-cell, td.mat-footer-cell {
      padding: 0;
      border-bottom-width: 1px;
      border-bottom-style: solid;
    }
    
    th.mat-header-cell:first-child, td.mat-cell:first-child, td.mat-footer-cell:first-child {
      padding-left: 24px;
      width: 25%;
    }
    
    th.mat-header-cell:last-child, td.mat-cell:last-child, td.mat-footer-cell:last-child {
      padding-right: 24px;
    }
    
    .footer{    
      align-items: flex-end;
      padding: 8px;
      padding-top: 12px;
    }
    .buttonsTr{
      width: 100% !important;
    }
    .buttonsDiv{
      width: 100% !important;
    }
    

    我希望这对任何人都有用!

    【讨论】:

    • 您能否在答案中添加一个解决方案示例?我正在尝试做类似的事情,这将是一个很好的帮助。我相信它也会帮助其他人。
    • @osiris 我更新了答案。希望有用!
    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2021-07-07
    • 2022-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多