具有未知列数的AngularJS动态表

Max*_*nko 13 dynamic tabular angularjs

我是Angular的新手,我的项目需要一些起点.如何通过鼠标单击背景从ajax数据创建新表?我知道ajax数据的列数未知,并且可能会不时有所不同.

例如:

the first click on background = table 1, ajax request to /api/table
| A | B | C |
| 1 | 2 | 3 |
| 5 | 7 | 9 |

the second click on background = table 2 and server returns new data from the same url /api/table
| X | Y |
| 5 | 3 |
| 8 | 9 |
Run Code Online (Sandbox Code Playgroud)

Dav*_*ank 33

您基本上可以通过以下方式使用两个嵌套的ng-repeats:

<table border="1" ng-repeat="table in tables">
  <tr>
      <th ng-repeat="column in table.cols">{{column}}</th>
  </tr>
  <tr ng-repeat="row in table.rows">
    <td ng-repeat="column in table.cols">{{row[column]}}</td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

function MyCtrl($scope, $http) {
    $scope.index = 0;
    $scope.tables = [];
    $scope.loadNew = function() {
        $http.get(/*url*/).success(function(result) {
            $scope.tables.push({rows: result, cols: Object.keys(result)});
        });
        $scope.index++;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在某处调用loadNew(),例如. <div ng-click="loadNew()"></div>

示例:http: //jsfiddle.net/v6ruo7mj/1/