如何將p-table的高度調整為與視窗高度相同
P粉132730839
P粉132730839 2023-08-25 12:06:40
0
1
535
<p><strong>問題:</strong> 我正在嘗試在angular中設定一個prime p-table的高度。我已經在網上搜索了很多結果,但到目前為止都沒有起作用(可能是由於這個問題存在多年,並且對框架進行了一些更改,禁用了某些解決方案)。 </p> <p>我已經嘗試了兩種方法,第一種方法在下面的程式碼中。第二種方法是將p-table的父div的高度設定為innerWindowHeight,並將p-table的scrollheight設定為flex(也不行)。 </p> <p><strong>工具:</strong>Angular 14.2.0與PrimeNG 14.1.1在一個新專案</p> <p>我在<strong>app.component.html</strong>有以下html代碼:</p> <pre class="brush:php;toolbar:false;"><div> <p-table #table (window:resize)="onResize()" [value]="data" [paginator]="true" [showCurrentPageReport]="true" [scrollable]="true" [scrollHeight]="tableScrollHeight" [rows]="10" [rowsPerPageOptions]="rowsPerPage" styleClass="p-datatable-gridlines p-datatable-striped p-datatable-sm" currentPageReportTemplate="{first} to {last} of {totalRecords} records"> <ng-template pTemplate="header"> <tr> <th>日期</th> <th>ID</th> <th>描述</th> <th>值</th> <th>稅</th> </tr> </ng-template> <ng-template pTemplate="body" let-entry> <tr> <td>{{entry.date}}</td> <td>{{entry.id}}</td> <td>{{entry.description}}</td> <td>{{entry.value}}</td> <td>{{entry.tax}}</td> </tr> </ng-template> </p-table> </div></pre> <p>以及以下的<strong>app.component.ts</strong>:</p> <pre class="brush:php;toolbar:false;">import {Component} 從 '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Testapp'; data: any[]; tableScrollHeight: string = "700px"; rowsPerPage: number[] = [5, 10, 20, 50]; constructor() { this.data = []; let entry: any = {}; entry.description = "First entry"; this.data.push(entry); } onResize() { this.tableScrollHeight = window.innerHeight 'px'; } }</pre> <p><strong>我想要實現的行為:</strong> 我希望無論表格是否為空或沒有足夠的條目來填充窗口,分頁器都保持在窗口底部,並且當表格行大於螢幕大小時,表格可以滾動(頭部保持在頂部)。 </p> <p>為了澄清我想要實現的目標,這裡有一個截圖展示它的當前狀態: 目前狀態</p> <p>這裡有一個截圖展示我希望它看起來的樣子: 應該的樣子</p> <p><strong>問題:</strong> 有沒有辦法以乾淨的方式達成這個目標? </p> <p>如評論中建議的那樣,我添加了一個<strong>stackblitz</strong>:https://angular-ivy-sfk7pw.stackblitz.io</p> <p><strong>編:</strong> 我發現如果將table的scrollHeight設定為表格內部的一個<strong>div</strong>(由primeng產生),該div的類別名稱為<strong>p-datatable-wrapper</strong>,它會取得樣式"<strong>max-height</strong>: <strong>XXX</strong>px",其中<strong>XXX</strong>是<strong>tableScrollHeight的值</strong> 。我可以寫一個CSS選擇器將樣式更改為min-width,但這可能不是一個好主意,因為我將不得不從typscript文件中訪問dom並蒐索自動生成的div。 </p>
P粉132730839
P粉132730839

全部回覆(1)
P粉529581199

也許你可以嘗試將這個樣式加入你的 css/scss 中,但這不是最佳實踐。

.p-datatable-wrapper {
  min-height: calc(100vh - 90px);
}

注意: 100vh 是你的螢幕高度,90px 是一個範例的分頁高度。

就是這樣。希望能對你有幫助。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!