HTML和Angular JS-默认为降序排列,单击时所有标题

问题描述

我有一些HTML代码,这些代码根据一些JSON虚拟数据生成一个表。当前有一个称为RN的列,它是数字,当前html表将数字分组,并根据分组的数字折叠/展开折叠行。我遇到的问题是它当前是升序,并且我希望列以降序排列。我希望能够按表中所有标题对列进行排序,但也默认情况下按降序显示RN列。

这些列是从$ scope.dummyData2动态生成的,并且没有经过硬编码。

JSON示例:

$scope.dummyData =
    [
      {
        "RN": 10,"Activity": "Summary","Location": "London","Date": "29/12/2019"
      },{
        "RN": 10,"Activity": "Loading",{
        "RN": 9,"Location": "Newcastle (Australia)","Date": "29/01/2020"
      },{
        "RN": 8,"Location": "Spain","Date": "29/01/2020"
      }
    ]

角度:

// init
    $scope.sort = {       
      sortingOrder : 'RN',reverse : true
  };

  $scope.changeSorting = function (column) {
    var sort = $scope.sort;
    if (sort.sortingOrder == column) {
        sort.reverse = !$scope.sort.reverse;
    } else {
        sort.sortingOrder = column;
        sort.reverse = false;
    }
};



$scope.selectedCls2 = function (column) {
    if (column == $scope.sort.sortingOrder) {
        return ('fa fa-chevron-' + (($scope.sort.reverse) ? 'down' : 'up'));
    }
    else {
        return 'fa fa-sort'
    }
};

$scope.dummyData2 = $scope.dummyData.reduce(function (r,a) {
  r[a.RN] = r[a.RN] || [];
  r[a.RN].push(a);
  return r;
},Object.create(null));
console.log($scope.dummyData2)

HTML:

      <pre>Page: {{sort|json}}</pre>
<table class="table table-striped table-bordered" >
                     <thead>
                        <tr>
                         <th> Action </th>
                       <th ng-repeat="(key,val) in dummyData[0]" ng-click="changeSorting(key)">{{ key }} <i ng-class="selectedCls2(key)"></i></th>
                        </tr>
                     </thead>
                  
                     <tbody>
                       <tr ng-repeat-start="(key,group) in dummyData2">
                         <td>
                           <button ng-if="group.expanded" ng-click="group.expanded = false">-</button>
                           <button ng-if="!group.expanded" ng-click="group.expanded = true">+</button>
                         </td>
                         <td>{{key}}</td>
                       </tr>
                       <tr ng-if="group.expanded" ng-repeat-end="" ng-repeat="row in group">
                         <td></td>
                         <td ng-repeat="column in row">
                             {{ column }}
                           </td>
                       </tr>
                           </tbody>
                        </table> 

当我单击标题时,可以看到使用此行代码

Page: {{sort|json}}
进行的排序更改,我可以看到反向:如果单击,则为false和true,但实际上并未对列进行排序。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...