html - "How to correctly display a table with "@媒体打印?

标签 html css angular html-table media-queries

我正在努力正确地从 html 打印表格。当我触发打印按钮时,预览不尊重网络浏览器中的实际显示,它只是一堆文本,一个独特的段落。我做了一些研究,发现了有关@media print 的信息,但我无法处理它。

这是我的表格html代码

<a id="top"></a>
<div class="row ">

    <div class="col l12">

        <!--<h4><b>Listes des participants aux séances d'orientation</b></h4>-->

    <form materialize>
         <div class="input-field col l3">
            <i class="material-icons prefix">account_circle</i>
            <input id="icon_prefix" type="text" class="validate" name="something" [(ngModel)]='nomSearch'>
            <label for="icon_prefix">Nom</label>
         </div>
    </form>

    <form materialize>
         <div class="input-field col l3">
            <i class="material-icons prefix">domain</i>
            <input id="icon_prefix" type="text" class="validate" name="something" [(ngModel)]='partiSearch'>
            <label for="icon_prefix">Parti Politique</label>
         </div>
    </form>

    <form materialize>
         <div class="input-field col l3">
            <i class="material-icons prefix">room</i>
            <input id="icon_prefix" type="text" class="validate" name="something" [(ngModel)]='depSearch'>
            <label for="icon_prefix">Departement</label>
         </div>
    </form>
    <form materialize>
         <div class="input-field col l3">
            <i class="material-icons prefix">room</i>
            <input id="icon_prefix" type="text" class="validate" name="something" [(ngModel)]='comSearch'>
            <label for="icon_prefix">Commune</label>
         </div>
    </form>




        <table id="print" class="bordered highlight" *ngIf='candInfo && candInfo.length'>
            <thead>
              <tr >
                  <th>Nom</th>
                  <th>Prénom</th>
                  <th>Parti Politique</th>
                  <th>Département</th>
                  <th>Commune<br></th>
              </tr>
            </thead>

            <tbody>
              <tr *ngFor = 'let candidats of candInfo | personSearch: nomSearch:partiSearch:depSearch:comSearch'>
                <td>{{candidats.nom}}</td>
                <td>{{candidats.prenom}}</td>
                <td>{{candidats.parti}}</td>
                <td>{{candidats.departement}}</td>
                <td>{{candidats.commune}}<br></td>
              </tr>
            </tbody>
      </table>
      <a id="bottom"></a>

    </div>
    <div class="col l1"></div>
     <div class="fixed-action-btn vertical click-to-toggle">
        <a class="btn-floating btn-large red">
            <i class="material-icons">menu</i>
        </a>
        <ul>

            <li><a scrollTo href="#top" class="btn-floating green"><i class="material-icons">arrow_upward</i></a></li>
            <li><a  class="btn-floating green" (click)="print()"><i class="material-icons">print</i></a></li>
            <li><a  routerLink="#bottom"class="btn-floating green"><i class="material-icons">insert_chart</i></a></li>
            <li><a scrollTo href="#bottom" class="btn-floating green"><i class="material-icons">arrow_downward</i></a></li>
        </ul>
    </div>

</div>

关联的组件

import { Component, OnInit } from '@angular/core';
import { PaeServiceService } from '../pae-service.service';
import { Icandidat } from '../candidat/icandidat';
import {MaterializeAction} from 'angular2-materialize';

@Component({
  selector: 'app-or-ouest',
  templateUrl: './or-ouest.component.html',
  styleUrls: ['./or-ouest.component.css'],
  providers : [PaeServiceService]
})
export class OrOuestComponent implements OnInit {
    candInfo : any [];
    nomSearch : string ;
    depSearch : string ;
    comSearch : string ;
    partiSearch : string ;





    candidatID : number;
    candidatNom : string;
    canditatPrenom : string ;
    candidatParti : string;
    candidatDepartement : string;
    candidatCommune : string ;

    candidats : Icandidat;
    errorMessage : string;


  constructor(private _candidatService : PaeServiceService) {

   }

  ngOnInit() {

     this._candidatService.getCandidatInfo()
            .subscribe(candidats => this.candInfo = candidats,
            error => this.errorMessage = <any>error);
  }

  print(): void {
    let printContents, popupWin;
    printContents = document.getElementById('print').innerHTML;
    popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
    popupWin.document.open();
    popupWin.document.write(`
      <html>
        <head>
          <title></title>
          <style>
          //........Customized style.......

          </style>
        </head>
        <body onload="window.print();window.close()">${printContents}</body>
      </html>`
    );
    popupWin.document.close();
}

}

到目前为止我的CSS

@media print{
 table {
    display: table;
    border-spacing: 2px;
     display: table-cell;
    border: solid 1px #ccc;
    padding: 2px;
    display: table-row;
    }
}

谢谢你的帮助

最佳答案

我重写打印方法

print(): void {
    var toBePrinted = document.getElementById("print");
    var newWind =window.open("");
    newWind.document.write(toBePrinted.outerHTML);
    newWind.print();
    newWind.close();
}

关于html - "How to correctly display a table with "@媒体打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44850352/

相关文章:

html - 去除html中不必要的空白

javascript - contenteditable - JQuery 将内容仅保存到网页(以及添加和删除)

jquery - 使用 jquery 的 css calc 函数

angular - 没有 FormBuilder 的提供者

javascript - html输入类型按钮在输入时提交

html - 将文本框定位在 'login box' 背景图像上

css - 如何使用 CSS 使表格中的 div 水平居中

python - 使用 beautifulsoup 在 python 中使用 colspan 解析表

部署在子目录中时,Angular 6 路由路径不适用于直接 URL

angular - ionic3/Angular 4 : Injecting a service in another service not working