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

时间:2016-03-02 13:46:33

标签: javascript jquery angularjs arrays html-table

我有一个数组的动态结果,需要与表绑定,但使用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>

  

期望的结果

<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>

1 个答案:

答案 0 :(得分:2)

你必须循环桌面的所有元素,然后再循环你的身体。我用一个固定的数字在这里循环你的问题。您只需修改过滤器即可。

<强> 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;
  };
});