gpt4 book ai didi

sorting - 带有对象数组的 Angular2 排序管道

转载 作者:太空狗 更新时间:2023-10-29 18:27:09 24 4
gpt4 key购买 nike

如何在 angular2 中使用对象数组制作排序管道

原始问题:

我有一个 TODO 列表,(Todo[ ]),我想在每次进行一些更改时对其进行排序。我希望完成的待办事项显示在列表的底部。 Todo 对象有一个名为 .completed 的属性,它存储一个 bool 值,它会告诉我们 todo 是否完成。

创建管道:

在 Angular2 中,“OrderBy”管道不存在。所以我们必须构建它:

import { Pipe, PipeTransform } from "angular2/core";
//Todo is the interface for our todo object
import {Todo} from './todo';

@Pipe({
name: "sort",
//set to false so it will always update, read below the code.
pure: false
})
export class TodosSortPipe implements PipeTransform {
transform(array: Todo[], args: any): Todo[] {
//watch the console to see how many times you pipe is called
console.log("calling pipe");
/* javascript is async, so could be that the pipe is called before
that the todos list is created, in this case we do nothing
returning the array as it is */
if (isBlank(array)) return null;
array.sort((a, b) => {
if (a.completed < b.completed) {
return -1;
//.completed because we want to sort the list by completed property
} else if (a.completed > b.completed) {
return 1;
} else {
return 0;
}
});
return array;
}
}

如果您不理解排序方法,请查看 MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

管道已完成,让我们转到组件。

创建组件:

AppComponent 类,创建一个名为 Todos 的 Todo 数组,从带有服务的 mock 中获取对象。

import {Component, OnInit} from 'angular2/core';
import {Todo} from './todo';
import {TodoService} from './todo.service';
import {TodosSortPipe} from './sort-pipe.component'

@Component({
//name of the html element
selector: 'my-app',
//view of the selector, " ` " is alt + 9
templateUrl: "./app/todo-list.component.html",
providers: [TodoService],
pipes: [ TodosSortPipe ]
})

export class AppComponent implements OnInit{

public todos: Todo[];
public edited = false;
public changes = 0;
//creating an istance of todoService
constructor(private _todoService: TodoService) { };

//getting the todos list from the service
getTodos() {
this._todoService.getTodos().then(todos => this.todos = todos);
}

(...)
editTodo(todo: Todo): void {
//slice is very important, read below the code
this.todos = this.todos.slice();
this.saveTodos();
}
}

模板实现

这是管道调用:

<li *ngFor="#todo of todos | sort; #i=index">
(...)
</li>

演示:

包含所有代码的完整示例:https://plnkr.co/edit/VICRMVNhqdqK9V4rJZYm?p=preview在 github 上观看:https://github.com/AndreaMiotto/Angular2-TodoApp

管道不纯

默认情况下,管道只会在您的管道输入参数发生变化时发生变化,而不会在您的数据发生变化时发生变化。将 Pure 设置为 false,您将使它“不纯”,因此您的管道将始终更新。

最佳答案

也许值todosnull因为它是使用 HTTP 异步加载的。

为了防止这种用例,你可以在你的管道中添加这个:

@Pipe({
name: "sort"
})
export class TodosSortPipe implements PipeTransform {
transform(array: Todo[], args: any): Todo[] {
if (array == null) {
return null;
}
(...)
}
}

然后值todos将收到 transform将使用此非空值再次调用管道的方法...

此外,您的 <li> 似乎标签没有结束。您必须将有效的 HTML 放入组件模板中。不知道是完整的代码还是截取的...

希望对你有帮助,蒂埃里

关于sorting - 带有对象数组的 Angular2 排序管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35176188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com