以下はJSONデータである:角度2パイプ2つの異なるアレイに同じ管を使用して、単一ngForで
{
"tagFrequency": [
{
"value": "At",
"count": 1,
"tagId": 249
},
{
"value": "ipsum",
"count": 1,
"tagId": 251
},
{
"value": "molestie",
"count": 1,
"tagId": 199
}
]
}
Iは、単語としてカラム名を持つテーブルで上記の頻度をUI上にこのデータを移入していJSONオブジェクトの属性値とカウントTagId属性を使用してGET API呼び出しで3列目のタグ名を抽出しています。私のHTMLコードを以下に示します。
<table>
<thead>
<tr>
<th (click)="sort('value')">{{ 'word' | translate }}</th>
<th (click)="sort('tagId')">{{ 'tag-name' | translate }}</th>
<th (click)="sort('count')">{{ 'frequency' | translate }}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let frequency of (frequencies | TagFrequencySorter: key: direction); let i = index;">
<td>{{ frequency.value }}</td>
<td>{{ processedTagNames[i] }}</td>
<td>{{ frequency.count }}</td>
</tr>
</tbody>
</table>
私は「TagFrequencySorter」パイプで作業している、これらの列の値をソートしてカウントします。しかし、同じforループ内の同じパイプを使ってtagNames配列データをソートしたいと思う。私はパイプに必要な変更を加えることができますが、私はこれらの両方の配列を何とかこのパイプに渡すだけです。続き
は、私はコンポーネントで書くソート機能である:
sort(value: string) {
this.direction = this.direction * (-1);
if(value === "tagId") {
this.key = "";
}
else {
this.key = value;
}
}
そしてここでは、パイプの実装です:
export class TagFrequencySorter implements PipeTransform {
transform(tagFrequencies: any, key: string, direction: number): any[] {
if (key !== '' && tagFrequencies !== null) {
console.log(key)
tagFrequencies.sort(
(a: any, b: any) => {
let propertyA: number|string = this.getProperty(a, key);
let propertyB: number|string = this.getProperty(b, key);
if (propertyA < propertyB) {
return -1 * direction;
} else if (propertyA > propertyB) {
return 1 * direction;
} else {
return 0;
}
}
);
}
return tagFrequencies;
}
private getProperty(value: { [key: string]: any}, key: string): number|string {
if (value === null || typeof value !== 'object') {
return undefined;
}
let keys: string[] = key.split('.');
let result: any = value[keys.shift()];
for (let newkey of keys) {
if (result === null) {
return undefined;
}
result = result[newkey];
}
return result;
}
}
誰かが私はこの問題を解決するのに役立つことはできますか?これを達成するための
感謝。私はtagNmaesを前処理する最初のアプローチを使用しました。しかし、headerNameのonClick()を使ってこのprocessedNameメソッドを呼び出すにはどうすればよいですか?私は他の2つのヘッダーのためにそれをやっているように、それぞれのヘッダー名にonClickを使ってソートしたい。 –
の私の更新されたHTMLコードを見てください。この場合、マージオプションの方があなたにとって最も簡単なオプションになるようです –