如何在primeNg表中创建打印方法

在我的有角项目中,我使用了一张PrimeNg表,我想打印该表的所有数据和选定数据,但我不知道如何处理,如果有什么方法或方法可以处理它呢? 我的模板

<div class="mainTbl">
            <p-table #filterT
            [columns]="cols" [value]="clients" 
            [scrollable]="true" [paginator]="false" [rows]="2"
            scrollHeight="200px" [resizableColumns]="false">

                    <ng-template pTemplate="colgroup" let-columns>
                            <colgroup>
                                <col *ngFor="let col of columns" >
                            </colgroup>
                    </ng-template>   
                    <ng-template pTemplate="caption"> <!--global search-->
                        <div style="text-align: right"> 
                            <i class="fa fa-search" style="margin:4px 4px 0 0"></i>
                            <input type="text" pInputText size="50" placeholder="بحث" (input)="filterT.filterGlobal($event.target.value,'contains')" style="width:auto">
                        </div>
                    </ng-template> <!--end global search-->
                <ng-template pTemplate="header" let-columns>
                    <tr>
                        <th *ngFor="let col of columns" pResizableColumn [pSortableColumn]="col.field">
                                {{col.header}}
                                <p-sortIcon  [field]="col.field" 
                                            ariaLabel="activate to sort" 
                                            ariaLabelDesc="activate to sort in descending order"
                                            ariaLabelAsc="activate to sort in ascending order">
                                </p-sortIcon>
                        </th>
                        <!-- <th>إجراءات
                            <button class="btn btn-success">
                                <i class="fa fa-plus"></i>
                            </button>
                        </th> -->
                    </tr>
                    <tr>
                        <th *ngFor="let col of columns" [ngSwitch]="col.field">
                            <input class="filterInput" *ngSwitchCase="'id'" pInputText type="text" (input)="filterT.filter($event.target.value,col.field,'contains')">
                            <input class="filterInput" *ngSwitchCase="'name'" pInputText type="text" (input)="filterT.filter($event.target.value,'contains')">
                            <input class="filterInput" *ngSwitchCase="'phone'" pInputText type="text" (input)="filterT.filter($event.target.value,'contains')">
                            <input class="filterInput" *ngSwitchCase="'address'" pInputText type="text" (input)="filterT.filter($event.target.value,'contains')">
                            <input class="filterInput" *ngSwitchCase="'account'" pInputText type="text" (input)="filterT.filter($event.target.value,'contains')">
                            <input class="filterInput" *ngSwitchCase="'nots'" pInputText type="text" (input)="filterT.filter($event.target.value,'contains')">
                        </th>
                    </tr>
                </ng-template>
                <ng-template pTemplate="body" let-rowData let-columns="columns">
                    <tr>
                        <td *ngFor="let col of columns" class="ui-resizable-column" >
                                {{rowData[col.field]}}
                        </td>
                        <!-- <td class="text-center">
                            <button class='btn btn-info'>
                                <span class="fa fa-edit"></span>
                            </button>
                            <button class="btn btn-danger">
                                <span class="fa fa-trash"></span>
                            </button>
                        </td> -->
                    </tr>
                </ng-template>
            </p-table>

        </div>

primeNg 表文档[https://www.primefaces.org/primeng/#/table]

wei988168 回答:如何在primeNg表中创建打印方法

我一年前在我的一个项目中实现了它。我研究了以下选项,最后得到了最后一个本机选项。

  1. 我已经使用HtmlToCanvas插件生成了一个canvas并将其导出为dom树的图像。

    缺点:需要大量处理

  2. 我已经使用jspdf根据我的内容生成了一个pdf文件,并根据要求设计了该pdf文件,它非常有用并且功能丰富。

    缺点:它不能直接打印,它会在新窗口中打开pdf,用户必须发出打印命令。

  3. 我在资产中保留了一个空的html文件,获取要打印的任何数据,在新标签页中打开该html页面,在加载期间,您可以传递数据并生成html内容,并且页面加载后触发打印命令。

    PROS:它可用于绕过打印(直接打印),本机实现,但要花些时间。

希望获得帮助!

本文链接:https://www.f2er.com/3169419.html

大家都在问