我尝试使用ng2-table,但我需要翻译标题和占位符,我使用ngx-translate/core进行翻译。
我需要翻译每个列,但如果我使用translation.instant(" my.text"),我只会收到密钥。我正在研究角度,我不知道如何解决这个问题。
如果我在html中使用翻译工作,{{" my.text" |翻译}},但我不知道如何使用ng2-table。
这是我的component.html
<div class="content-heading">
<div>Data-Tables components
<small>Native and not wrapped angular2 based data-tables</small></div>
</div>
<!-- <alert [type]="'info'">None of this examples are based on the official jQuery-Datatables</alert> -->
<h4 class="page-header">ng2 Table
<small>Simple table extension with sorting, filtering, paging... for Angular2 apps by ValorSoftware</small>
</h4>
<div class="mb-3 clearfix">
<div class="pull-let wd-md">
<input *ngIf="config.filtering" placeholder="Filter all columns" [ngTableFiltering]="config.filtering" class="form-control" (tableChanged)="onChangeTable(config)" />
</div>
</div>
<div class="card card-default">
<div class="table-responsive">
<ng-table [config]="config" (tableChanged)="onChangeTable(config)" (cellClicked)="onCellClick($event)" [rows]="rows" [columns]="columns"></ng-table>
</div>
</div>
<div class="text-right">
<pagination *ngIf="config.paging" class="pagination-sm" [(ngModel)]="page" [totalItems]="length" [itemsPerPage]="itemsPerPage" [maxSize]="maxSize" [boundaryLinks]="true" [rotate]="false" (pageChanged)="onChangeTable(config, $event)" (numPages)="numPages = $event">
</pagination>
</div>
<!-- <pre *ngIf="config.paging" class="card card-block card-header">Page: {{page}} / {{numPages}}</pre> -->
这是我的component.ts:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as _ from 'lodash';
import { TableData } from './ng2-table-data';
@Component({
selector: 'app-datatable',
templateUrl: './datatable.component.html',
styleUrls: ['./datatable.component.scss']
})
export class DatatableComponent implements OnInit {
public singleData;
constructor(public http: HttpClient) {
// ng2Table
this.length = this.ng2TableData.length;
}
// ng2Table
public rows: Array<any> = [];
public columns: Array<any> = [
{ title: 'Name', name: 'name', filtering: { filterString: '', placeholder: 'Filter by name' } },
{
title: 'Position',
name: 'position',
sort: false,
filtering: { filterString: '', placeholder: 'Filter by position' }
},
{ title: 'Office', className: ['office-header', 'text-success'], name: 'office', sort: 'asc' },
{ title: 'Extn.', name: 'ext', sort: '', filtering: { filterString: '', placeholder: 'Filter by extn.' } },
{ title: 'Start date', className: 'text-warning', name: 'startDate' },
{ title: 'Salary ($)', name: 'salary' }
];
public page: number = 1;
public itemsPerPage: number = 10;
public maxSize: number = 5;
public numPages: number = 1;
public length: number = 0;
public config: any = {
paging: true,
sorting: { columns: this.columns },
filtering: { filterString: '' },
className: ['table-striped', 'table-bordered', 'mb-0', 'd-table-fixed']
};
public ng2TableData: Array<any> = TableData;
public ngOnInit(): void {
this.onChangeTable(this.config);
}
public changePage(page: any, data: Array<any> = this.ng2TableData): Array<any> {
let start = (page.page - 1) * page.itemsPerPage;
let end = page.itemsPerPage > -1 ? (start + page.itemsPerPage) : data.length;
return data.slice(start, end);
}
public changeSort(data: any, config: any): any {
if (!config.sorting) {
return data;
}
let columns = this.config.sorting.columns || [];
let columnName: string = void 0;
let sort: string = void 0;
for (let i = 0; i < columns.length; i++) {
if (columns[i].sort !== '' && columns[i].sort !== false) {
columnName = columns[i].name;
sort = columns[i].sort;
}
}
if (!columnName) {
return data;
}
// simple sorting
return data.sort((previous: any, current: any) => {
if (previous[columnName] > current[columnName]) {
return sort === 'desc' ? -1 : 1;
} else if (previous[columnName] < current[columnName]) {
return sort === 'asc' ? -1 : 1;
}
return 0;
});
}
public changeFilter(data: any, config: any): any {
let filteredData: Array<any> = data;
this.columns.forEach((column: any) => {
if (column.filtering) {
filteredData = filteredData.filter((item: any) => {
return item[column.name].match(column.filtering.filterString);
});
}
});
if (!config.filtering) {
return filteredData;
}
if (config.filtering.columnName) {
return filteredData.filter((item: any) =>
item[config.filtering.columnName].match(this.config.filtering.filterString));
}
let tempArray: Array<any> = [];
filteredData.forEach((item: any) => {
let flag = false;
this.columns.forEach((column: any) => {
if (item[column.name].toString().match(this.config.filtering.filterString)) {
flag = true;
}
});
if (flag) {
tempArray.push(item);
}
});
filteredData = tempArray;
return filteredData;
}
public onChangeTable(config: any, page: any = { page: this.page, itemsPerPage: this.itemsPerPage }): any {
if (config.filtering) {
(<any>Object).assign(this.config.filtering, config.filtering);
}
if (config.sorting) {
(<any>Object).assign(this.config.sorting, config.sorting);
}
let filteredData = this.changeFilter(this.ng2TableData, this.config);
let sortedData = this.changeSort(filteredData, this.config);
this.rows = page && config.paging ? this.changePage(page, sortedData) : sortedData;
this.length = sortedData.length;
}
public onCellClick(data: any): any {
console.log(data);
}
}