Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2+;如何同时使用嵌套属性和非嵌套属性对数据数组进行排序?_Angular_Sorting_Nested - Fatal编程技术网

Angular 角度2+;如何同时使用嵌套属性和非嵌套属性对数据数组进行排序?

Angular 角度2+;如何同时使用嵌套属性和非嵌套属性对数据数组进行排序?,angular,sorting,nested,Angular,Sorting,Nested,我有一个包含6列的表,其中包含来自根数组的数据: columns: any[] = [{ sortBy: 'startTime', title: 'Order Date' }, { sortBy: 'btcAmount', title: 'BTC' }, { sortBy: 'btcPrice', title: 'Rate' }, { sortBy: 'total', title: 'Total' }, { sortBy: 'status', title: 'Status' }, {

我有一个包含6列的表,其中包含来自根数组的数据:

  columns: any[] = [{ sortBy: 'startTime', title: 'Order Date' }, { sortBy: 'btcAmount', title: 'BTC' },
  { sortBy: 'btcPrice', title: 'Rate' }, { sortBy: 'total', title: 'Total' },
  { sortBy: 'status', title: 'Status' }, { sortBy: 'paymentResult.updated', title: 'Payment Date' }];
前5列可以正确排序,而最后一列
paymentResult.updated
paymentResult.updated
排序的
在同一根数组(
this.transactions
)中包含无法相应排序的嵌套属性数据。这是我的排序功能:

sort(property = 'startTime') {
    this.column = property;
    this.isDesc = !this.isDesc;
    this.direction = this.isDesc ? 1 : -1;
    this.transactions.sort((a, b) => {
      if (a === null) return -1;
      if (b === null) return 1;
      if (a[property] === b[property]) return 0;
      return a[property] < b[property] ? -1 * this.direction : 1 * this.direction;
    });
  } 
这是一种观点:

<table class="table table-responsive table-hover" *ngIf="transactions" >
        <thead class="thead-default">
          <tr>
            <th *ngFor="let columnName of columns" 
                (click)="sort(columnName.sortBy)">
              {{columnName.title}}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let transaction of transactions>
            <td>{{ transaction.startTime | date: 'short' }}</td>
            <td>{{ transaction.btcAmount.toFixed(6) }}</td>
            <td>{{ transaction.btcPrice | currency:'USD':true }}</td>
            <td>{{ transaction.total | currency:'USD':true }}</td>
            <td>{{ transaction.status }}</td>
            <td> {{transaction.paymentResult?.updated}}</td>
          </tr>
        </tbody>
      </table>

{{columnName.title}}

我猜是这样的-只需检查它是否是嵌套属性,如果是-逐个迭代属性,直到找到值:

sort(property = 'startTime') {
    this.column = property;
    this.isDesc = !this.isDesc;
    this.direction = this.isDesc ? 1 : -1;
    this.transactions.sort((a, b) => {
    let a1 = a;
    let b1 = b;
    if(property.indexOf('.') > -1) {
     let keys = property.split('.');
     keys.forEach((key) => {
       if(a1 == null) return -1;
       if (b1 === null) return 1;
       a1 = a1[key];
       b1 = b1[key];
     })
    }
      if (a === null) return -1;
      if (b === null) return 1;
      if (a1 === b1) return 0;
      return a[property] < b[property] ? -1 * this.direction : 1 * this.direction;
    });
  } 
sort(属性='startTime'){
this.column=属性;
this.isDesc=!this.isDesc;
this.direction=this.isDesc?1:-1;
this.transactions.sort((a,b)=>{
设a1=a;
设b1=b;
if(property.indexOf('.')>-1){
让keys=property.split('.');
key.forEach((key)=>{
if(a1==null)返回-1;
if(b1==null)返回1;
a1=a1[键];
b1=b1[键];
})
}
if(a==null)返回-1;
如果(b==null)返回1;
如果(a1==b1)返回0;
返回a[property]

未测试,但想法很清楚。

这是一个好主意,但属性
this.transactions.paymentResult
有时可能为空,因此控制台将返回错误,如
无法读取null的属性“updated”
我通过将嵌套属性映射到非嵌套属性中来制定解决方案,然后我可以像往常一样对数组进行排序。无论如何,谢谢你的解决方案。
sort(property = 'startTime') {
    this.column = property;
    this.isDesc = !this.isDesc;
    this.direction = this.isDesc ? 1 : -1;
    this.transactions.sort((a, b) => {
    let a1 = a;
    let b1 = b;
    if(property.indexOf('.') > -1) {
     let keys = property.split('.');
     keys.forEach((key) => {
       if(a1 == null) return -1;
       if (b1 === null) return 1;
       a1 = a1[key];
       b1 = b1[key];
     })
    }
      if (a === null) return -1;
      if (b === null) return 1;
      if (a1 === b1) return 0;
      return a[property] < b[property] ? -1 * this.direction : 1 * this.direction;
    });
  }