Angular 角度4排序列表
我已经基于此示例对表的列进行了排序: 这是代码管道:Angular 角度4排序列表,angular,Angular,我已经基于此示例对表的列进行了排序: 这是代码管道: import {Pipe, PipeTransform} from 'angular2/core'; @Pipe({ name: 'orderBy' }) export class OrderrByPipe implements PipeTransform { transform(records: Array<any>, args?: any): any { return records.sort(funct
import {Pipe, PipeTransform} from 'angular2/core';
@Pipe({ name: 'orderBy' })
export class OrderrByPipe implements PipeTransform {
transform(records: Array<any>, args?: any): any {
return records.sort(function(a, b){
if(a[args.property] < b[args.property]){
return -1 * args.direction;
}
else if( a[args.property] > b[args.property]){
return 1 * args.direction;
}
else{
return 0;
}
});
};
}
我想将管道类迁移到Angular 4,该怎么做
这是console中的错误:
error_handler.js:60 Error: Uncaught (in promise): Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined
Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined
您的问题来自未定义的
详细信息
变量 从'@angular/core'导入{Pipe,PipeTransform};
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderby'
})
export class OrderbyPipe implements PipeTransform {
//private records : Array<any> = [];
transform(records :Array<Object>, args?: any): any {
if(records != null){
return records.sort(function(a, b){
if (a[args.property] === '' || a[args.property] === null || typeof a[args.property] === 'undefined') {
return 1 * args.direction;
}
if (b[args.property] === '' || b[args.property] === null || typeof b[args.property] === 'undefined') {
return -1 * args.direction;
}
if(a[args.property] < b[args.property]){
return -1 * args.direction;
}
else if( a[args.property] > b[args.property]){
return 1 * args.direction;
}
else{
return 0;
}
});
}
};
}
@烟斗({
名称:'orderby'
})
导出类OrderbyPipe实现PipeTransform{
//私有记录:数组=[];
转换(记录:数组,参数?:any):any{
if(记录!=null){
返回记录。排序(函数(a,b){
if(a[args.property]=''|| a[args.property]==null | | a[args.property]==='undefined'){
返回1*args.direction;
}
如果(b[args.property]=''|| b[args.property]===null | | b[args.property]的类型==='undefined'){
返回-1*参数方向;
}
if(a[args.property]b[args.property]){
返回1*args.direction;
}
否则{
返回0;
}
});
}
};
}
您的问题是详细信息为空(嗯未定义
完全正确)。请你检查一下好吗?这就是问题所在,谢谢不再给出任何错误,但不要只订购代码,我帮不了你。方向和列的值是什么?我刚刚解决了它,函数的参数不是很好,我插入了一个大写字母,它必须与对象相同,请检查这个,它对我有效,但我看到一件事,整数排序假设我们取1,2,3,4,5,6,7,8,9,10,11,12,13。它的行为很古怪。
error_handler.js:60 Error: Uncaught (in promise): Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined
Error: Error in ./ParticularsListComponent class ParticularsListComponent - inline template:42:14 caused by: Cannot read property 'sort' of undefined
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderby'
})
export class OrderbyPipe implements PipeTransform {
//private records : Array<any> = [];
transform(records :Array<Object>, args?: any): any {
if(records != null){
return records.sort(function(a, b){
if (a[args.property] === '' || a[args.property] === null || typeof a[args.property] === 'undefined') {
return 1 * args.direction;
}
if (b[args.property] === '' || b[args.property] === null || typeof b[args.property] === 'undefined') {
return -1 * args.direction;
}
if(a[args.property] < b[args.property]){
return -1 * args.direction;
}
else if( a[args.property] > b[args.property]){
return 1 * args.direction;
}
else{
return 0;
}
});
}
};
}