【问题标题】:How to create print method in primeNg table如何在 primeNg 表中创建打印方法
【发布时间】:2019-11-04 02:14:57
【问题描述】:

在我的角度项目中,我使用了一张 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, 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 表格文档 [https://www.primefaces.org/primeng/#/table]

【问题讨论】:

  • PrimeNg 内置了不同格式的导出功能。 primefaces.org/primeng/#/table/export
  • 我知道,所以根据你的回答,导出和打印方法是一样的吗?
  • 不完全是,但目前没有直接打印功能。
  • 那我该怎么做呢?

标签: javascript angular typescript primeng


【解决方案1】:

我在一年前在我的一个项目中实施了它。我探索了以下选项,最后选择了最后一个原生选项。

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

    缺点:处理繁重

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

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

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

    优点:它可以用来绕过打印(直接打印),本机实现,但需要一些时间。

希望对您有所帮助!

【讨论】:

  • 这是个好主意 ;),顺便说一下,在搜索中我发现了这个插件 ngx-print [npmjs.com/package/ngx-print] ,它非常有用,而且运行良好,无论如何,非常感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2023-02-10
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
相关资源
最近更新 更多