我们正在使用Angular可视化AG网格。我们希望以用户的语言翻译ag网格的标题。
Ag grid code:
<ag-grid-angular class="ag-theme-material" [rowData]="productionOrders">
<ag-grid-column [headerName]="'ORDERS.GRID.EntryCode' | translate" field="entry"></ag-grid-column>
<ag-grid-column [headerName]="ORDERS.GRID.EntryDescription" field="entryDescription"></ag-grid-column>
</ag-grid-angular>
The same way we can translate a value on our html page itself
<span>{{ 'ORDERS.Status' | translate}}</span>
我注意到,当加载翻译时,ag网格不会注意到何时加载翻译。但是html页面本身上的值会被翻译。
其他信息:ngx-translate的翻译管道是“不纯的”管道,这意味着其值可以更改(例如,在加载所有翻译时)
以同样的方式,当使用没有转换的headerName时,它不会被更新:
Ag grid code:
<ag-grid-angular class="ag-theme-material" [rowData]="productionOrders">
<ag-grid-column [headerName]="bindedString" field="entry"></ag-grid-column>
</ag-grid-angular>
Angular/typescript
this.lazyString = 'test-1';
setTimeout(() => {
this.lazyString = 'test-2';
}, 3000);
标头名称永远不会更新为“ test-2”
答案 0 :(得分:3)
<ag-grid-angular style="width: 100%; height: 100%;" class="ag-theme-material" [rowData]="rowData"
[columnDefs]="columnDefs" (gridReady)="onGridReady($event)" [pagination]="true">
</ag-grid-angular>
import { Component } from '@angular/core';
import { ColDef, GridApi } from 'ag-grid-community';
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-demo',
templateUrl: './demo.component.html',
styleUrls: ['./demo.component.scss']
})
export class DemoComponent {
private gridApi: GridApi = null;
public columnDefs: ColDef[] = [
{ headerName: "Code", field: 'code', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) },
{ headerName: 'Version', field: 'version', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) },
{ headerName: 'IsEnabled', field: 'isEnabled', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) }
];
public rowData: any[] = [];
constructor(private translateService: TranslateService) {
this.translateService.onLangChange.subscribe(() => {
this.gridApi.refreshHeader();
})
}
public onGridReady(parameters: any): void {
this.gridApi = parameters.api;
}
public localizeHeader(parameters: ICellRendererParams): string {
let headerIdentifier = parameters.colDef.field;
return this.translateService.instant(headerIdentifier);
}
}
答案 1 :(得分:1)
使用
headerValueGetter
代替colDef.headerName
来允许动态标头名称。
private translator: TranslateService;
...
colDef.headerValueGetter : this.localizeHeader.bind(this)
....
localizeHeader(params){
let headerIdentifier = params.colDef.field; // params.column.getColId();
this.translator.get(headerIdentifier).map((res: string) => {
return res;
});
}
{
headerName: "Country",
field: "country",
width: 120,
enableRowGroup: true,
enablePivot: true,
headerValueGetter: countryHeaderValueGetter
},
function countryHeaderValueGetter(params) {
switch (params.location) {
case 'csv': return 'CSV Country';
case 'clipboard': return 'CLIP Country';
case 'toolPanel': return 'TP Country';
case 'columnDrop': return 'CD Country';
case 'header': return 'H Country';
default: return 'Should never happen!';
}
}
答案 2 :(得分:0)
如果你想使用 TurboYang 的异步翻译加载解决方案,只需添加:
private destroy = new Subject<void>();
constructor(private toastService: ToastService, private translate: TranslateService) {
translate.onLangChange.pipe(takeUntil(this.destroy)).subscribe(() => {
this.gridApi.refreshHeader();
});
translate.onDefaultLangChange.pipe(takeUntil(this.destroy)).subscribe(() => {
this.gridApi.refreshHeader();
});
}
ngOnDestroy(): void {
this.destroy.next();
this.destroy.complete();
}