How do I generate nested table in one column if it has list of values using angular js?

前端 未结 2 1985
悲哀的现实
悲哀的现实 2020-12-20 07:46

index.js

var app = angular.module(\'plunker\', []);

app.controller(\'MainCtrl\', function($scope) {
  $scope.name = \'World\';

  $scope.data =[{\"Id\":1,\"         


        
相关标签:
2条回答
  • 2020-12-20 08:19

    You write your markup this way:

    <table>
      <thead>
        <tr>
          <th ng-repeat="(k,v) in data[0]">{{k}}</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="item in data">
          <td ng-repeat="(prop, value) in item" ng-init="isArr = isArray(value)">
            <table ng-if="isArr">
              <thead>
                <tr>
                  <th ng-repeat="(sh, sv) in value[0]">{{sh}}</th>
                </tr> 
              </thead>
              <tbody>
                <tr ng-repeat="sub in value">
                  <td ng-repeat="(sk, sv) in sub">{{sv}}</td>
                </tr>
              </tbody>
            </table>
            <span ng-if="!isArr">{{value}}</span>
          </td>
        </tr>
      </tbody>
    </table>
    

    Full code: https://gist.github.com/anonymous/487956892d760c17487c

    0 讨论(0)
  • 2020-12-20 08:21

    I'm not really sure what you want to render, but this might give you some ideas.

    see: http://plnkr.co/edit/znswagx45a2F7IThdQJn?p=preview

    app.js

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      $scope.data =[{"Id":1,"Title":"en-US","Description":"UnitedStates","MyValues":[{"Id":100,"Value":"Save"}]},
    {"Id":1,"Title":"en-UK","Description":"UK","MyValues":[{"Id":102,"Value":"Delete"}]}]
      $scope.cols = Object.keys($scope.data[0]);
    
      $scope.notSorted = function(obj){
        if (!obj) {
            return [];
        }
        return Object.keys(obj);
    }  
    });
    

    index.html

    <table border=1>
      <thead>
        <tr>
          <th ng-repeat="key in notSorted(cols)" ng-init="value=cols[key]">{{value}}</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="row in data">
          <td ng-if="!$last" ng-repeat="dat in notSorted(row)" ng-init="value=row[dat]">
            <div ng-if="value[0].Id">
              <table>
                <tr>
                  <td>Id:</td><td>{{value[0].Id}}</td>
                </tr>
                <tr>
                  <td>Value:</td><td>{{value[0].Value}}</td>
                </tr>
              </table>
            </div>
            <div ng-if="!(value[0].Id)">
              {{value}}
            </div>
          </td>
        </tr>
      </tbody>
    </table>   
    

    If you need to be more general, maybe instead of <div ng-if="value[0].Id"> you could do something like <div ng-if="angular.isArray(value)">. I didn't have time to try and get that working though, but something to look into.

    0 讨论(0)
提交回复
热议问题