如何使用 summaryFunc/summaryTemplate 格式化 ngx-datatable 中的摘要值?
How can I format the summary value in ngx-datatable with summaryFunc/ summaryTemplate?
我在我的 angular 项目中实现了 ngx-datatable,我需要显示我的 table 的摘要行。
根据文档,我使用了 [summaryFunc] 并且摘要值显示正确。
但现在我需要格式化这个值。
例如:1,000 美元而不是 1000 美元。
如何使用 [summaryFunc] 做到这一点?
- [summaryFunc] = "sampleFunc | number" => 不起作用,它说 sampleFunc 的结果不是数字(我也在 sampleFunc 中返回了数字)。
我尝试在 sampleFunc 内部使用 DecimalPipe => 不起作用,因为在 sampleFunc 内部一切都未定义(这个和所有管道...)。
我也试过用[summaryTemplate]="sampleTemplate"。 samplateTempate 由 ng-template 定义。但是我不知道如何将汇总值传递给 sampleTemplate?
<ngx-datatable
#table
class="material"
[headerHeight]="40"
[footerHeight]="40"
[columnMode]="'flex'"
[rowHeight]="'auto'"
[summaryRow]="hasSummaryRow"
[summaryHeight]="45"
(resize)="onResize($event)"
(reorder)="onReorder($event)"
(activate)="onActive($event)"
[loadingIndicator]="!items"
[limit]="pageItems"
[rows]="items"
[count]="totalItems"
[externalPaging]="externalPaging"
[offset]="offset"
(page)="onPaging($event)">
<ngx-datatable-column *ngFor="let col of displayedColumns; let i=index"
[name]="col.value"
[flexGrow]="calculateWidth(col)"
[sortable]="!col.columnType"
[canAutoResize]="true"
[resizeable]="!col.columnType"
[summaryFunc]="sampleFunc"
[summaryTemplate]="sampleTemplate">
</ngx-datatable-column>
</ngx-datatable>
<ng-template #sampleTemplate let-value="value">
<span>{{value|number}}</span>
</ng-template>
最后我自己找到了答案...在 summaryFunc 中我只需要声明新管道来格式化数字。
- 首先,我们创建新的自定义管道以根据您的要求格式化数字。参考这个posthttps://medium.com/better-programming/creating-a-custom-pipe-for-your-angular-app-64b419a93f2a.
- 其次,在 summaryFunc 中,我们通过
const customePipe = new CustomPipe();
创建自定义管道的新实例
- 最后,我们通过调用 customePipe 的 transform 方法格式化值
customPipe.transform(value);
我在我的 angular 项目中实现了 ngx-datatable,我需要显示我的 table 的摘要行。 根据文档,我使用了 [summaryFunc] 并且摘要值显示正确。 但现在我需要格式化这个值。 例如:1,000 美元而不是 1000 美元。
如何使用 [summaryFunc] 做到这一点?
- [summaryFunc] = "sampleFunc | number" => 不起作用,它说 sampleFunc 的结果不是数字(我也在 sampleFunc 中返回了数字)。
我尝试在 sampleFunc 内部使用 DecimalPipe => 不起作用,因为在 sampleFunc 内部一切都未定义(这个和所有管道...)。
我也试过用[summaryTemplate]="sampleTemplate"。 samplateTempate 由 ng-template 定义。但是我不知道如何将汇总值传递给 sampleTemplate?
<ngx-datatable
#table
class="material"
[headerHeight]="40"
[footerHeight]="40"
[columnMode]="'flex'"
[rowHeight]="'auto'"
[summaryRow]="hasSummaryRow"
[summaryHeight]="45"
(resize)="onResize($event)"
(reorder)="onReorder($event)"
(activate)="onActive($event)"
[loadingIndicator]="!items"
[limit]="pageItems"
[rows]="items"
[count]="totalItems"
[externalPaging]="externalPaging"
[offset]="offset"
(page)="onPaging($event)">
<ngx-datatable-column *ngFor="let col of displayedColumns; let i=index"
[name]="col.value"
[flexGrow]="calculateWidth(col)"
[sortable]="!col.columnType"
[canAutoResize]="true"
[resizeable]="!col.columnType"
[summaryFunc]="sampleFunc"
[summaryTemplate]="sampleTemplate">
</ngx-datatable-column>
</ngx-datatable>
<ng-template #sampleTemplate let-value="value">
<span>{{value|number}}</span>
</ng-template>
最后我自己找到了答案...在 summaryFunc 中我只需要声明新管道来格式化数字。
- 首先,我们创建新的自定义管道以根据您的要求格式化数字。参考这个posthttps://medium.com/better-programming/creating-a-custom-pipe-for-your-angular-app-64b419a93f2a.
- 其次,在 summaryFunc 中,我们通过
const customePipe = new CustomPipe();
创建自定义管道的新实例
- 最后,我们通过调用 customePipe 的 transform 方法格式化值
customPipe.transform(value);