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

How to create print method in primeNg table

在我的angular项目中,我使用了一个table的PrimeNg,我想打印这个table的所有数据和选定的数据,我不知道我是怎么做到的那么有没有办法或方法来处理呢? 我的模板

<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, col.field, 'contains')">
                            <input class="filterInput" *ngSwitchCase="'phone'" pInputText type="text" (input)="filterT.filter($event.target.value, col.field, 'contains')">
                            <input class="filterInput" *ngSwitchCase="'address'" pInputText type="text" (input)="filterT.filter($event.target.value, col.field, 'contains')">
                            <input class="filterInput" *ngSwitchCase="'account'" pInputText type="text" (input)="filterT.filter($event.target.value, col.field, 'contains')">
                            <input class="filterInput" *ngSwitchCase="'nots'" pInputText type="text" (input)="filterT.filter($event.target.value, col.field, '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 table 文档 [https://www.primefaces.org/primeng/#/table]

我一年前在我的一个项目中实现了它。我探索了以下选项,最后以最后一个选项结束。

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

    缺点:需要大量处理

  2. 我使用jspdf生成了一个pdf文件,根据我的内容,并根据需要设计了那个pdf,这是一个非常有用且功能丰富的插件。

    缺点:它不能直接打印,它在新的 window 中打开 pdf,用户必须给出打印命令。

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

    优点:可用于绕过打印(直接打印),原生实现,但需要一些时间。

希望对您有所帮助!