javascript - 动态结果数组需要使用 Angular JS 与具有不同布局的表绑定(bind)

标签 javascript jquery angularjs arrays html-table

我有一个数组的动态结果,需要与表绑定(bind),但使用 Angular JS 有一些不同的布局。我尝试了很多尝试但没有成功。我需要想要的结果。绝对感谢您的帮助。

var arr = [
  {
    "unique_id": "CS",
    "college": "BSCS",
    "arr_length": 1,
    "program_section": [
      {
        "question": "How you rate your teacher",
        "total": 135,
        
      },
      {
        "question": "Are you satisfy with your teacher",
        "total": 142,
        
      },
      {
        "question": "Which course you have most like throughout the session",
        "total": 135,
        
      },
      {
        "question": "How much you have rate your instructor",
        "total": 137,
        
      },
      
    ]
  },
  {
    "unique_id": "MBA",
    "college": "BBA",
    "arr_length": 2,
    "program_section": [
      {
        "question": "How you rate your teacher",
        "total": 175,
        
      },
      {
        "question": "Are you satisfy with your teacher",
        "total": 142,
        
      },
      {
        "question": "Which course you have most like throughout the session",
        "total": 165,
        
      },
      {
        "question": "How much you have rate your instructor",
        "total": 137,
        
      },
      
    ]
  },
  {
    "unique_id": "CA",
    "college": "Account",
    "arr_length": 1,
    "program_section": [
      {
        "question": "How you rate your teacher",
        "total": 145,
        
      },
      {
        "question": "Are you satisfy with your teacher",
        "total": 162,
        
      },
      {
        "question": "Which course you have most like throughout the session",
        "total": 125,
        
      },
      {
        "question": "How much you have rate your instructor",
        "total": 117,
        
      },
      
    ]
  }
];
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table  border="1" ng-repeat="x in names">
  <tr>
    <td>Question</td>
    <td>{{ x.college }}</td>
  </tr>
  <tr>
    <td>
    <table border="1" ng-repeat="y in x.program_section">
      <tr>
        <td width="100px">{{ y.question }}</td>
        <td width="100px">{{ y.total }}</td>
      </tr>
    </table>
    </td>
    <td>
   </tr> 
</table>

</div>

Desired Result

<table width="200" border="1">
  <tr>
    <td>Question</td>
    <td>CS</td>
    <td>MBA</td>
    <td>CA</td>
  </tr>
  <tr>
    <td>How you rate your teacher</td>
    <td>135</td>
    <td>175</td>
    <td>145</td>
  </tr>
  <tr>
    <td>Are you satisfy with your teacher</td>
    <td>142</td>
    <td>142</td>
    <td>162</td>
  </tr>
  <tr>
    <td>Which course you have most like throughout the session</td>
    <td>135</td>
    <td>165</td>
    <td>125</td>
  </tr>
  <tr>
    <td>How much you have rate your instructor</td>
    <td>137</td>
    <td>137</td>
    <td>117</td>
  </tr>
</table>

最佳答案

您必须循环表头的所有元素,然后再循环表体的所有元素。我在这里用一个固定的数字来循环你的问题。不过,您可以简单地修改过滤器。

<强> JSFiddle

HTML

<div ng-app="myApp" ng-controller="customersCtrl">
    <table border="1">
        <thead>
        <tr>
            <td>Question</td>
            <td ng-repeat="x in names">{{ x.college }}</td>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="n in [] | range:4">
            <td>
                {{ names[0].program_section[n].question }}
            </td>
            <td width="100px" ng-repeat="x in names">
                {{ x.program_section[n].total }}
            </td>
        </tr>
        </tbody>
    </table>
</div>

Javascript

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
}).filter('range', function() {
  return function(input, total) {
    total = parseInt(total);

    for (var i=0; i<total; i++) {
      input.push(i);
    }

    return input;
  };
});

关于javascript - 动态结果数组需要使用 Angular JS 与具有不同布局的表绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749000/

相关文章:

javascript - 带有 promise 的 Protractor 变量范围

javascript - 为什么我的 Controller 没有触发我的 function.js 脚本

javascript - 确认框 Twig、Symfony 和 Javascript

javascript - 将点击事件设置为由此事件添加的元素

javascript - 使用ajax从API获取图像

javascript - Angular 负载定制 Controller

javascript - 将表单数据发送到 vanilla js

javascript - 错误: Cannot find module '/node_modules\progressbar/package.json'

javascript - 如何从克隆中删除 div(jQuery - 删除元素的父行)

javascript - 使用 PHP,如何将 JSON blob 附加到现有的多级 JSON 对象