У меня есть этот JSON:
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"id": "1",
"datetime_device": "2018-05-11 12:05:00",
"client_name": "Client1"
},
{
"id": "2",
"datetime_device": "2018-05-11 12:05:00",
"client_name": "Client1"
},
{
"id": "3",
"datetime_device": "2018-04-11 12:05:00",
"client_name": "Client1"
},
{
"id": "4",
"datetime_device": "2018-01-11 12:05:00",
"client_name": "Client1"
},
{
"id": "5",
"datetime_device": "2018-02-11 12:05:00",
"client_name": "Client2"
},
{
"id": "6",
"datetime_device": "2018-01-05 12:05:00",
"client_name": "Client2"
},
{
"id": "7",
"datetime_device": "2018-05-09 12:05:00",
"client_name": "Client1"
},
{
"id": "8",
"datetime_device": "2018-05-01 12:05:00",
"client_name": "Client1"
},
{
"id": "9",
"datetime_device": "2018-01-01 12:01:00",
"client_name": "Client1"
}
]
}
Я создаю функцию, которая сортирует по дате этот JSON.
private getTime(date?: Date) {
return date != null ? date.getTime() : 0;
}
public sortbydate(): any[] {
return this.notification0.sort((a, b) => {
return this.getTime(new Date(a.datetime_device)) - this.getTime(new Date(b.datetime_device));
}
);
}
В html:
<div class="input-field col s2">
<mat-checkbox class="example-margin" (click)="sortbydate()">Time</mat-checkbox>
</div>
до этого момента он работает очень хорошо
Теперь я использовал функцию, которая вызывает в интервале 10000 эту функцию.this.getallnotif();
public notification: Notifications[];
ngOnInit() {
this.subscription = Observable.interval(10000).subscribe(x => {
this.getallnotif();
});
getallnotif() {
this.ws.notif().subscribe(
notification => {
console.log(notification)
this.notification = notification;
}
);
}
service.ts
public notif(): Observable<Notifications[]> {
let headers = new Headers();
headers.append('x-access-token', this.auth.getCurrentUser().token);
return this.http.get(Api.getUrl(Api.URLS.notif), {
headers: headers
})
.map((response: Response) => {
let res = response.json();
if (res.StatusCode === 1) {
this.auth.logout();
} else{
return res.StatusDescription.map(notiff => {
return new Notifications(notiff);
});
}
});
}
Этот код в настоящее время работает, события вызываются в этом интервале, а функция заказа сортирует даты по порядку.Когда я нажимаю кнопку sortbydate (), мои данные сортируются должным образом, но только в течение промежутка времени 10000. Я хочу, чтобы мои данные оставались отсортированными после этого интервала.
У вас есть идеи, как мне это сделать??
Обновление:
Я пробовал этот код
this.subscription = Observable.interval(10000).subscribe(x => {
let notification = this.sortbydate();
if (x >= notification.length) {
x = notification.length
}
console.log(notification0[x]) // data are all of time sorted but new events not show, only when I refresh page. Please any idea why?
return notification0[x]
});