【发布时间】:2018-05-06 19:03:13
【问题描述】:
PrimeNG DataTable 提供[scrollable] 属性来定义垂直和/或水平滚动。这必须与一组scrollHeight 和/或scrollWidth 组合使用。
我怎样才能拥有一个可以调整到任何高度/宽度的表格,同时保持可滚动功能?
这是我尝试过的代码:
<div class="ui-g-12">
<p-dataTable class="ui-g-12" [value]="rows" [hidden]="this.apiService.spinnerIsVisible"
[style]="{ height: 'fit-content', 'margin-top': '10px' }"
[resizableColumns]="false" columnResizeMode="fit" emptyMessage="No records found"
[responsive]="false"
[globalFilter]="tableSearch"
[editable]="true"
[scrollable]="true" scrollHeight="100%" scrollWidth="100%">
<p-header>
<button pButton type="button" icon="fa-refresh" (click)="refresh()" style="float:left"></button>
<label for="tableSearch">Global search: </label>
<input id="tableSearch" #tableSearch type="text" placeholder="type here">
</p-header>
<p-column
*ngFor="let col of cols" [header]="col" [field]="col"
[style]="{'width': '250px', 'min-width': '50px', 'word-wrap': 'break-word'}"
[sortable]="true"
[filter]="true" filterPlaceholder="" filterMatchMode="contains"
[editable]="true">
</p-column>
</p-dataTable>
</div>
但它只解决了响应宽度问题。在屏幕截图中,您可以设置可水平滚动的表格:
由于p-dataTable 的height 属性在百分比值的情况下是相对于父级的,因此我尝试通过将style="height: 100%" 添加到父级div 来使父级div 适合内容。这是更新的代码:
<div class="ui-g-12" style="height: 100%">
<p-dataTable class="ui-g-12" [value]="rows" [hidden]="this.apiService.spinnerIsVisible"
[style]="{ height: 'fit-content', 'margin-top': '10px' }"
[resizableColumns]="false" columnResizeMode="fit" emptyMessage="No records found"
[responsive]="false"
[globalFilter]="tableSearch"
[editable]="true"
[scrollable]="true" scrollHeight="100%" scrollWidth="100%">
<p-header>
<button pButton type="button" icon="fa-refresh" (click)="refresh()" style="float:left"></button>
<label for="tableSearch">Global search: </label>
<input id="tableSearch" #tableSearch type="text" placeholder="type here">
</p-header>
<p-column
*ngFor="let col of cols" [header]="col" [field]="col"
[style]="{'width': '250px', 'min-width': '50px', 'word-wrap': 'break-word'}"
[sortable]="true"
[filter]="true" filterPlaceholder="" filterMatchMode="contains"
[editable]="true">
</p-column>
</p-dataTable>
</div>
我还对我的 styles.scss 文件进行了以下更改以使其正常工作(在 stackoverflow 上的其他问题中找到了这一点):
html, body {
height: 100%;
}
但这对我也不起作用: 在屏幕截图上,高度似乎是正确的,但事实并非如此。当我向下滚动时,首先,它应该按原样进行,但是当接近表格的末尾时,滚动条会从视图中出来,所以当我仍然能够滚动时我看不到它。所以看起来数据表比它应该的高一点。
那么我该如何解决呢?任何帮助将不胜感激!
【问题讨论】:
-
哥们,这个问题你解决了吗?在这里尝试同样的事情。
标签: css angular primeng primeng-datatable