의 속성에 따라 정렬 배열 내 JSON 데이터 각도 2 : 다음은 중첩 된 객체
{
"items": [
{
"id": 26,
"email": "[email protected]",
"firstName": "Poornima ",
"lastName": "karuppu",
"role": "Student",
"studentDetails": {
"discipline": "History",
"currentDegree": "Master",
"currentSemester": 58
},
"fullName": "Poornima karuppu"
},
{
"id": 149,
"email": "[email protected]",
"firstName": "raj",
"lastName": "naga",
"role": "Student",
"studentDetails": {
"discipline": "German Lingustics",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": "raj naga"
},
{
"id": 134,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": {
"discipline": "History",
"currentDegree": "Master",
"currentSemester": 15
},
"fullName": " "
},
{
"id": 20,
"email": "[email protected]",
"firstName": "null",
"lastName": "null",
"role": "Student",
"studentDetails": {
"discipline": "History and Arts",
"currentDegree": "Master",
"currentSemester": 4
},
"fullName": "null null"
},
{
"id": 184,
"email": "[email protected]",
"firstName": "Rob",
"lastName": "Pat",
"role": "Student",
"studentDetails": {
"discipline": "Computer Science",
"currentDegree": "Bachelor",
"currentSemester": 25
},
"fullName": "Rob Pat"
},
{
"id": 151,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": {
"discipline": "Art",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": " "
},
{
"id": 3,
"email": "[email protected]",
"firstName": "Lamija",
"lastName": "Halvadzija",
"role": "Student",
"studentDetails": {
"discipline": "Lingustics",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": "Lamija Halvadzija"
},
{
"id": 25,
"email": "[email protected]",
"firstName": "Rolans",
"lastName": "Mustermann",
"role": "Student",
"studentDetails": {
"discipline": "Linguistics",
"currentDegree": "Bachelor",
"currentSemester": 2
},
"fullName": "Rolans Mustermann"
},
{
"id": 178,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 140,
"email": "[email protected]",
"firstName": "Nilakshi",
"lastName": "Naphade",
"role": "Student",
"studentDetails": null,
"fullName": "Nilakshi Naphade"
},
{
"id": 40,
"email": "[email protected]",
"firstName": "Poornima",
"lastName": "Karuppasamy",
"role": "Student",
"studentDetails": null,
"fullName": "Poornima Karuppasamy"
},
{
"id": 5,
"email": "[email protected]",
"firstName": "Nilakshi",
"lastName": "Naphade",
"role": "Student",
"studentDetails": null,
"fullName": "Nilakshi Naphade"
},
{
"id": 181,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 170,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 38,
"email": "[email protected]",
"firstName": "Nilakshi_UPB",
"lastName": "Naphade",
"role": "Student",
"studentDetails": null,
"fullName": "Nilakshi_UPB Naphade"
},
{
"id": 179,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 175,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 117,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 185,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 155,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 174,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 53,
"email": "[email protected]",
"firstName": "Nilakshi_GMAIL",
"lastName": "Naphade",
"role": "Student",
"studentDetails": null,
"fullName": "Nilakshi_GMAIL Naphade"
},
{
"id": 22,
"email": "[email protected]",
"firstName": "null",
"lastName": "null",
"role": "Student",
"studentDetails": null,
"fullName": "null null"
},
{
"id": 176,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 169,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
}
],
}
입니다 그것의 내가 GET API를 사용하여 UI에 dispalying 오전 학생들의 목록입니다. 각도 2 파이프를 사용하여 이러한 레코드를 정렬합니다. 다음은 sort.pipe.ts 코드 :
import { Injectable, Pipe, PipeTransform } from '@angular/core';
import { User } from '../../../core/user/user.model';
@Pipe({
name: 'hipUsersSorter'
})
@Injectable()
export class UsersSorter implements PipeTransform {
transform(users: any, key: string, direction: number): User[] {
if (key !== '' && users !== null) {
users.sort(
(a: any, b: any) => {
if (a[key] < b[key]) {
return -1 * direction;
} else if (a[key] > b[key]) {
return 1 * direction;
} else {
return 0;
}
}
);
}
return users;
}
}
이 파이프를 사용하여, 나는 이름, 성 및 이메일 필드를 기반으로 데이터를 정렬 할 수 있어요. 그러나 중첩 된 특성을 기반으로 레코드를 정렬 할 수는 없습니다. 규율, currentDegree 및 currentSemester. 나는 이런 종류의 파이프를 호출하고있는 곳에서 HTML 템플릿은 다음과 같은 :
<table>
<thead>
<tr>
<th (click)="sort('lastName')">{{ 'last name' | translate }}</th>
<th (click)="sort('firstName')">{{ 'first name' | translate }}</th>
<th (click)="sort('email')">{{ 'email' | translate }}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of students | hipUsersFilter: query: selectedOption: selectedRole | hipUsersSorter: key: direction
| paginate: { id: 'server', itemsPerPage: 10, currentPage: _page, totalItems: _total }">
<td>{{ user.lastName }}</td>
<td>{{ user.firstName }}</td>
<td>{{ user.email }}</td>
<td>{{ user.studentDetails.discipline }}</td>
<td>{{ user.studentDetails.currentDegree }}</td>
<td>{{ user.studentDetails.currentSemester }}</td>
</tr>
</tbody>
</table>
이것은 구성 요소의 내 정렬 기능 :
direction = -1;
sort(value: string) {
this.direction = this.direction * -1;
this.key = value;
}
어떻게이 중첩 된 필드를 기반으로 데이터를 정렬 할 수 있습니까? 누군가이 문제에 대한 의견을 제공 할 수 있습니까? 사전에 덕분에
다음을 읽어보십시오. https://angular.io/docs/ts/latest/guide/pipes.html#!#no-filter-pipe –