AngularJS表的创建与NG-重复 [英] AngularJS Table creation with ng-repeat

查看:122
本文介绍了AngularJS表的创建与NG-重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下从数据库的响应。关于在哪里类嵌套在小组最后学生类数组。

I get following response from database. about array of classes where classes are nested in groups and finally students.

"Response":[
    {
      "Id":1,"Name":"Class 1","Location":"Building 1","Groups":[
        {
          "Id":1,"Name":"GB1","Students":[
            {
              "Id":1,"Name":"Mike","RollNo":"1","Performance":{
                "Id":1,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":2,"Name":"John","RollNo":"2","Performance":{
                "Id":2,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":3,"Name":"Muffin","RollNo":"3","Performance":{
                "Id":3,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        },  {
          "Id":2,"Name":"GB2","Students":[
            {
              "Id":4,"Name":"Ema","RollNo":"1","Performance":{
                "Id":4,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":5,"Name":"Sunny","RollNo":"2","Performance":{
                "Id":5,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":6,"Name":"Jen","RollNo":"3","Performance":{
                "Id":6,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        }
      ]
    },{
      "Id":2,"Name":"Class 2","Location":"Building 1","Groups":[
        {
          "Id":3,"Name":"G1","Students":[
            {
              "Id":7,"Name":"Ron","RollNo":"1","Performance":{
                "Id":7,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":8,"Name":"Kaka","RollNo":"2","Performance":{
                "Id":8,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":9,"Name":"Mark","RollNo":"3","Performance":{
                "Id":9,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        },  {
          "Id":4,"Name":"G2","Students":[
            {
              "Id":10,"Name":"Lily","RollNo":"1","Performance":{
                "Id":10,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":11,"Name":"Lina","RollNo":"2","Performance":{
                "Id":11,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":12,"Name":"Linda","RollNo":"3","Performance":{
                "Id":12,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        }
      ]
    }
  ]

现在我想创建一个表像这样用列跨度。

谁能帮我用NG-重复和angularjs做到这一点?无法弄清楚如何动态地合并此列。到目前为止,我管理扁平化使用阵列选项做最后一部分。

Now I want to create a table like this using colspan. Could anyone help me to do this using ng-repeat and angularjs ? can't figure out how to merge this columns dynamically. So far I managed to do last part using flatten array option.

推荐答案

虽然在这种解决方案我不得不添加一些添加JavaScript方法我还是希望看到一个更好的解决方案,并接受作为一个答案。 演示

Although in this solution I had to add add few JavaScript method I still expect to see a better solution and accept as an answer. DEMO

<!DOCTYPE html>
<html ng-app = "demo">

  <head>
    <script data-require="angular.js@*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller = "demoCtrl">
    <h1>Hello Plunker!</h1>

    <br>

    <table>

        <tr>
         <td>Class name</td>
         <td colspan="{{lengthCount(r)}}"  ng-repeat ="r in response"  >
           {{r.Name}}
         </td>
      </tr>

    <tr>
         <td>Group name</td>
         <td colspan="{{gr.Students.length}}"  ng-repeat ="gr in Groups"  >
           {{gr.Name}}
         </td>
      </tr>
      <tr>
         <td>Student name</td>
         <td  ng-repeat ="st in Studs"  >
           {{st.Name}}
         </td>
      </tr>
      <tr>
         <td>Maths</td>
         <td  ng-repeat ="st in Studs"  >
           {{st.Performance.Math}}
         </td>
      </tr>
       <tr>
         <td>Physics</td>
         <td  ng-repeat ="st in Studs"  >
           {{st.Performance.Physics}}
         </td>
      </tr>
       <tr>
         <td>English</td>
         <td  ng-repeat ="st in Studs"  >
           {{st.Performance.English}}
         </td>
      </tr>

    </table>

    <br>



  </body>

</html>

code放在这里

Code goes here

angular.module("demo",[])
  .controller("demoCtrl", ['$scope', function($scope){
    $scope.response = response;

        function flattenArray(array, fn) {
        var output = [];
        console.log("<i was flatten here");
        for (var i = 0; i < array.length; ++i) {
            var result = fn(array[i]);
            if (result)
                output = output.concat(result);
        }
        return output;
    }

     $scope.lengthCount = function(obj) {
        var k = 0;
        console.log("<i was flatten here");
        for (var i = 0; i < obj.Groups.length; ++i) {
           k= k+ obj.Groups[i].Students.length;
        }
        return k;
    }

       $scope.Groups = flattenArray($scope.response, function (item) {
            console.log("<i was here");

            return item.Groups;
        });
       $scope.Studs = flattenArray($scope.Groups, function (item) {
            console.log("<i was here");

            return item.Students;
        });


  }]);

var response = [{
      "Id":1,"Name":"Class 1","Location":"Building 1","Groups":[
        {
          "Id":1,"Name":"GB1","Students":[
            {
              "Id":1,"Name":"Mike","RollNo":"1","Performance":{
                "Id":1,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":2,"Name":"John","RollNo":"2","Performance":{
                "Id":2,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":3,"Name":"Muffin","RollNo":"3","Performance":{
                "Id":3,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        },  {
          "Id":2,"Name":"GB2","Students":[
            {
              "Id":4,"Name":"Ema","RollNo":"1","Performance":{
                "Id":4,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":5,"Name":"Sunny","RollNo":"2","Performance":{
                "Id":5,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":6,"Name":"Jen","RollNo":"3","Performance":{
                "Id":6,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        }
      ]
    },{
      "Id":2,"Name":"Class 2","Location":"Building 1","Groups":[
        {
          "Id":3,"Name":"G1","Students":[
            {
              "Id":7,"Name":"Ron","RollNo":"1","Performance":{
                "Id":7,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":8,"Name":"Kaka","RollNo":"2","Performance":{
                "Id":8,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":9,"Name":"Mark","RollNo":"3","Performance":{
                "Id":9,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        },  {
          "Id":4,"Name":"G2","Students":[
            {
              "Id":10,"Name":"Lily","RollNo":"1","Performance":{
                "Id":10,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":11,"Name":"Lina","RollNo":"2","Performance":{
                "Id":11,"Math":"90","Physics":"70","English":"60"
              }
            },{
              "Id":12,"Name":"Linda","RollNo":"3","Performance":{
                "Id":12,"Math":"90","Physics":"90","English":"90"
              }
            }
          ]
        }
      ]
    }
    ]

这篇关于AngularJS表的创建与NG-重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆