【问题标题】:Nested rows in Angular TableAngular 表中的嵌套行
【发布时间】:2017-11-26 15:20:58
【问题描述】:

我有一个这样的 JSON 数组:

[
  {
    "students": {
      "Student1": {
        "number": "15",
        "books": {
          "Mystery": [
            "Book1",
            "Book2"
          ]
        }
      },
      "Student2": {
        "number": "12",
        "books": {
          "Romance": [
            "Book1"
          ],
          "Adventure": [
            "Book1",
            "Book2"
          ]
        }
      },
      "Student3": {
        "number": "116",
        "books": {
        }
      }
    },
    "class": "7th Grade",
    "school": "High School 1"
  }
]

我想通过以下方式在 Angular1 页面上将此数据显示为表格:

如何使列相互调整大小,以及如何根据需要嵌套 ng-repeats?

我尝试在 student, student-number, and books 列内嵌套表,并在 books 列内的表内创建一个表,但对齐总是关闭,我无法获得 "No Books" 的默认值如果 "books" 字段为空,则显示。

这是我迄今为止尝试过的Plunkr

【问题讨论】:

  • 你能用你试过的代码做一个 plunker 吗?
  • 你试过了吗?把你已经尝试过的代码放在这里。
  • 您的数组将只包含一个对象?
  • 这个问题太宽泛了:(
  • @Vivz 不,它可以包含多个对象。

标签: html angularjs web frontend


【解决方案1】:

我选择创建隐藏的嵌套表格,以便在点击时显示更多信息。

【讨论】:

    【解决方案2】:

    一种可能的解决方案是通过float:left将几个表(基于$scope.base$scope.students$scope.books 数组,在控制器中计算)组合成一个虚拟的、所需的表@(以确保它们位于一个排)。行的停靠是通过设置他们的style:height 来提供的,这取决于他们的孩子的数量。由于具有rowspan 属性的表格的复杂html标记,直接解决方案将非常复杂。我还添加了第二项 (items.push(items[0])),以证明该解决方案适用于多个项目。

    angular.module('app', [])
    .controller('MyController', ['$scope', function($scope) {
        var items = [
        {
          "students": {
            "Student1": {
              "number": "15",
              "books": {
                "Mystery": [
                  "Book1",
                  "Book2"
                ]
              }
            },
            "Student2": {
              "number": "12",
              "books": {
                "Romance": [
                  "Book1"
                ],
                "Adventure": [
                  "Book1",
                  "Book2"
                ]
              }
            },
            "Student3": {
              "number": "116",
              "books": {
              }
            },
            "class": "7th Grade",
            "school": "High School 1"
          }
        }
      ];
      items.push(items[0]);
       
       $scope.base = [];
       $scope.students = [];
       $scope.books = [];
       for(var item of items){
          var temp = $scope.books.length;
          for(var student in item.students){
            if(['class', 'school'].indexOf(student) == -1){
               var studentV = item.students[student];
               $scope.students.push({name:student, number: studentV.number, w: Object.keys(studentV.books).length || 1});
               for(var book in studentV.books)
                  $scope.books.push((book + ':' + JSON.stringify(studentV.books[book])).replace(/"/g, ''));       
               if(Object.keys(studentV.books).length == 0)
                 $scope.books.push('No books');
            }
          } 
          $scope.base.push({cl: item.students.class, school: item.students.school, w: $scope.books.length - temp});
       }
       
    }]);
    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    table.left, table.left th, table.left td {
        border-right: 0px
    }
    tr {
      text-align:center
    }
    <script src="//code.angularjs.org/snapshot/angular.min.js"></script>
    
    <div ng-app='app' ng-controller="MyController">           
      <table style='float:left;border-right:0' class='left'>
          <thead>
            <tr>
              <th>School</th>
              <th>Class</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat='item in base' style='height:{{item.w*30}}px'>
              <td>{{item.school}}</td>
              <td>{{item.cl}}</td>
            </tr>
          </tbody>
      </table>
    
      <table style='float:left' class='left'>
          <thead>
            <tr>
              <th>Student</th>
              <th>Student-number</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat='item in students' style='height:{{item.w*30}}px'>
              <td>{{item.name}}</td>
              <td>{{item.number}}</td>
            </tr>
          </tbody>
      </table>
    
      <table>
          <thead>
            <tr>
              <th>Books</th>
            </tr>
          </thead>
          <tbody>
            <tr ng-repeat='item in books track by $index' style='height:30px'>
              <td>{{item}}</td>
            </tr>
          </tbody>
      </table>
    
    </div>

    【讨论】:

    • 谢谢你!我想知道是否有没有在控制器中创建单独数组的解决方案?
    • @NishantRoy,你有三个表,所以你将有三个数组。正如我已经写的那样,仅使用一个表和初始数组来解决这个问题将非常复杂,因为需要实现非常复杂的结果表的 html 结构。
    猜你喜欢
    • 2021-12-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多