使用ng-repeat生成动态表行

时间:2017-08-23 07:58:33

标签: javascript java html angularjs mongodb

我的问题类似于线程

  

Dynamic table creation with ng-repeat in angularjs

JSON看起来像这样

  

{           " id":" String",           "马克" :                   {                      " NumMin的":"整数"                      " NumMax的值":"整数"                      " subjectname":" string",                       }       }

只是会有一个额外的功能,用户将不输入需要生成的行,然后将根据将从数据库中提取的动态JSON文件生成表。提前致谢

1 个答案:

答案 0 :(得分:0)

试用this JS-Fiddle

请告诉我这是否对您有帮助!

let app = angular.module('app', []);
app.controller('ctrl', ['$scope', ($scope) => {

    $scope.numberOfRows = 0;
    $scope.data = [];

    $scope.$watch('data', () => {
        console.log($scope.data);
    }, true);

}]);
app.filter('range', function () {
    return function (input, total) {
        total = parseInt(total, 10);

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


<div ng-app="app">
<div ng-controller="ctrl">
    <input type="number" ng-model="numberOfRows" />
    <br />
    <table width="100%">
    <tr>
        <th>nummin</th><th>nummax</th><th>...</th>
    </tr>
    <tr ng-repeat="i in [] | range:numberOfRows">
        <td>
        <input type="number" ng-model="data[i].nummin" />
        </td>
        <td>
        <input type="number" ng-model="data[i].nummax" />
        </td>
        <td>...</td>
    </tr>
    </table>
</div>
</div>