【问题标题】:Html/Angular toggle table rows to show hidden a tableHtml/Angular 切换表格行以显示隐藏的表格
【发布时间】:2018-02-24 09:25:51
【问题描述】:

我对 Angular 完全陌生,我需要创建一个表格。数据数组如下:-

data = [{rollno: 1,name: 'abc',subject: 'maths'},
        {rollno: 4,name: 'xyz',subject: 'history'},
        {rollno: 2,name: 'pqr',subject: 'history'}
       ];

我想根据这些数据创建一个包含一些汇总行的表格,然后当我单击展开按钮时,子行应出现在该汇总行下方,指示实际数据。

例如:-

Expand/Collapse | No of Students | Subject
 click here        1                Maths
 click here        2               History

例如,当我切换第二行上的展开/折叠按钮时,我希望实际行如下所示:-

 Expand/Collapse | No of Students | Subject
 click here        1                Maths
 click here        2               History
 RollNo | StudentName
   4       xyz
   2       pqr

我怎样才能做到这一点?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    1) 按主题对数据进行分组

    首先您需要将数据按subject 分组,然后计算每个组中的项目。

    您可以使用 angular.filter 模块的 groupBy 过滤器来执行此操作。

    1a) 添加对该模块的依赖,如下所示:

    var app = angular.module("yourModuleName", ["angular.filter"]);
    

    1b) 然后,您可以在 ng-repeat 指令中对 <tbody> 标记使用 groupBy 过滤器,如下所示:

    <tbody ng-repeat="(key, value) in data | groupBy: 'subject'">
    

    1c) 您现在正在处理以下格式的数据。这是key/value 对的对象,其中"maths" 和"history" 都是keys,数组是values

    {
      "maths": [
        {
          "rollno": 1,
          "name": "abc",
          "subject": "maths",
        }
      ],
      "history": [
        {
          "rollno": 4,
          "name": "xyz",
          "subject": "history",
        },
        {
          "rollno": 2,
          "name": "pqr",
          "subject": "history",
        }
      ]
    }
    

    2) 显示分组数据并统计每组中的项目

    使用key 和value 在表格中显示分组数据,如下所示:

    <table>
      <thead>
        <tr>
          <th>Subject</th>
          <th>Number of Students</th>
          <th>Expand/Collapse</th>
        </tr>
      </thead>
      <tbody ng-repeat="(key, value) in data | groupBy: 'subject'">
        <tr>
          <td>{{ key }}</td>
          <td>{{ value.length }}</td>
          <td>
            <button>
              Expand/Collapse
            </button>
          </td>
        </tr>
        <tr>
          <td colspan="3">
            <table>
              <thead>
                <tr>
                  <th>Roll Number</th>
                  <th>Name</th>
                </tr>
              </thead>
              <tbody>
                <tr ng-repeat="student in value">
                  <td>{{ student.rollno }}</td>
                  <td>{{ student.name }}</td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    

    注意额外的&lt;tr&gt; 和嵌套表与另一个ng-repeat 用于显示学生数据。目前将显示所有嵌套的学生数据,下一步是根据单击的展开/折叠按钮有条件地显示/隐藏嵌套表。

    3) 显示/隐藏嵌套的学生数据

    3a) 在按钮上添加一个ng-click 指令,以便它将key 传递给控制器​​上的onExpandClicked 函数:

    <button ng-click="onExpandClicked(key)">
      Expand/Collapse
    </button>
    

    3b) 在控制器中创建onExpandClicked 函数:

    $scope.onExpandClicked = function(name){
      $scope.expanded = ($scope.expanded !== name) ? name : "";
    }
    

    这会在$scope 上设置一个值,该值可在视图中用于决定是否显示/隐藏学生数据的一部分。 key 作为name 参数传递到函数中,$scope.expanded 将设置为name 或重置为"",具体取决于传入的name 是否与当前@987654354 相同@值与否。

    3c) 最后,在&lt;tbody&gt; 的第二个&lt;tr&gt; 标记上的ng-if 指令中使用$scope.expanded 变量来显示或隐藏嵌套的学生数据:

    <table>
      <thead>
        <tr>
          <!-- Omitted for brevity -->
       </tr>
      </thead>
      <tbody ng-repeat="(key, value) in data | groupBy: 'subject'">
        <tr>
          <!-- Omitted for brevity -->
        </tr>
        <tr ng-if="expanded === key">
          <!-- 
            Omitted for brevity 
          -->
        </tr>
      </tbody>
    </table>
    

    演示

    CodePen: How to show/hide grouped data created by the angular.filter module's groupBy filter

    【讨论】:

      【解决方案2】:

      这是一个可以解决您的问题的工作示例。

      var indexCtrl = ['$scope', function($scope){
        $scope.num = 0;
        $scope.test = [{rollno: 1,name: 'abc',subject: 'maths'},
              {rollno: 4,name: 'xyz',subject: 'history'},
              {rollno: 2,name: 'pqr',subject: 'history'}
             ];
        $scope.changeShow = function(index){
          $scope.num = index;
        };
      }];
      <!DOCTYPE html>
      <html ng-app>
      <head>
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
        <meta charset="utf-8">
        <title>JS Bin</title>
      </head>
      <body ng-controller='indexCtrl'>
      <table>
        <tr>
        <td>Expand/Collapse</td>
        <td>No of Students</td>
        <td>Subject</td>
        </tr>
        <tr ng-repeat='i in test' ng-class="num == $index ? red : none">{{}}
          <td ng-click='changeShow($index)'><a href="javascript:void(0)">click here</a></td>
          <td>{{$index +1}}</td>
          <td >{{i.subject}}</td>
         </tr>
      </table>
        <table>
        <tr>
        <td>RollNo</td>
        <td>StudentName</td>
        </tr>
        <tr ng-repeat='i in test'>
          <td ng-show='num == $index'>{{i.rollno}}</td>
           <td ng-show='num == $index'>{{i.name}}</td>
        </tr>
      </table>
        <style>
         table tr td{
              border: 1px solid black
          }
      </style>
      </body>
      </html>

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        首先,您应该用 div 结构替换实际的表格,因为不可能像您计划的那样混合两种表格(当我说对了时)。

        您可以通过 ng-click 切换每一行,并使用相应的扩展内容,如下所示(伪代码,我希望这个想法变得清晰):

        <div class="row-header">
          <span>RollNo</span>
          <span>StudentName</span>
        </div>
        
        <div class="row-content" ng-if="!row_4_expanded" ng-click="row_4_expanded = !row_4_expanded">
          <span>4</span>
          <span>xyz</span>
        </div>
        
        <div ng-if="row_4_expanded">
          <div class="row-expanded-header">
            <span>No of Students</span>
            <span>Subject</span>
          </div>
          <div class="row-expanded-content">
            <span>1</span>
            <span>Math</span>
          </div>
          <div class="row-expanded-content">
            <span>2</span>
            <span>History</span>
          </div>
        </div>
        
        
        <div class="row-content" ng-if="!row_2_expanded" ng-if="row_2_expanded" ng-click="row_2_expanded = !row_2_expanded">
          <span>2</span>
          <span>pqr</span>
        </div>
        
        <div ng-if="row_2_expanded">
          <div class="row-expanded-header">
            <span>No of Students</span>
            <span>Subject</span>
          </div>
          <div class="row-expanded-content">
            <span>1</span>
            <span>Math</span>
          </div>
          <div class="row-expanded-content">
            <span>2</span>
            <span>History</span>
          </div>
        </div>
        

        当您现在单击一行时,它会与相应的展开行切换存在。

        【讨论】:

          【解决方案4】:

          使用 html 设计一个表格,并使用 ng-repeat 循环遍历您的 data 对象以显示数据。

          ngRepeat

          W3Schools 有一些关于如何使用 AngularJS 显示表格的基本示例

          Angular Tables

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-12-13
            • 2021-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-27
            • 1970-01-01
            相关资源
            最近更新 更多