【发布时间】:2017-12-04 15:26:43
【问题描述】:
我正在努力从 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;
}
}
感谢您的帮助
【问题讨论】:
-
您有多个显示属性。删除除可能修复它的“显示:表”之外的所有内容。从技术上讲,默认情况下它显示为表格,因此您可能也不需要该表格。
-
我试了还是不行
-
我认为我们需要查看更多您的代码以获得更好的上下文。只需您提供的代码,按照我的建议进行操作,它就会正确显示。 codepen.io/anon/pen/pwLzwq也许自己创建一个codepen来说明这个问题。
-
或者我只是把我的问题解释错了。实际上我只是想像用打印机一样打印这部分代码,所以我会更新我的代码。但我有一个带有脚本的按钮,它将启动打印请求。所以在预览中(它会向您显示它将在哪里打印)它不尊重 html 表结构
标签: html css angular html-table media-queries