AngularJS - 在表格内重复行

时间:2018-04-28 21:51:51

标签: angularjs angularjs-ng-repeat

AngularJS - ng-repeat& $ http.get

编辑:尝试使用唯一,但也无效:

<table class="table" ng-controller="fetchData">
    <tr ng-repeat="elements in rawData.value">
        <th ng-repeat="(key, value) in elements | unique:'key'">
            {{key}}
        </th>
        <td ng-repeat="element in elements">
            {{ element }}
        </td>
    </tr>
</table>

我正在使用ng-repeat填充表格和外部信息,但我正在努力填写标题。我找到了一个提取密钥的例子:

<td ng-repeat="(key, element) in elements">

然而,这对我没有帮助,因为我应该只填写一次数据,而不是数据。我也试过添加额外的ng-repeat,但没有用。

重新考虑$ http.get和ODATA-sources - 有没有办法一次用所有可用数据填充表格?或者我可以在任何地方读取哪些令牌被接受(在谷歌搜索时没有找到任何信息)?现在我只需要最多11个元素 - 而且我不允许像我现在这样做的方式将令牌添加到URL。有没有更好的方法来获取数据?任何指针将不胜感激,如果需要其他信息,请告诉我。这是代码:

<html>
  <head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.0.0.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ODataResources/1.0.25/odataresources.min.js"></script>
    <link rel="stylesheet" href="/css/style.css">
  </head>

<script>
    const BASE_URL = 'http://services.odata.org/V3/OData/OData.svc/';

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    var app = angular.module('getData', []);
    app.controller('fetchLinks', function($scope, $http) {
        $http.get(BASE_URL)
        .then(function(response) {
            $scope.rawLinks = response.data;
        });
    });
    app.controller('fetchData', function($scope, $http) {
        let dynamicContent = getParameterByName('q');
        console.log(BASE_URL+dynamicContent);
        $http.get(BASE_URL+dynamicContent)
        .then(function(response) {
            $scope.rawData = response.data;
        });
    });
</script>
  <body ng-app="getData">
        <div class="container main-container">
                <div class="row">
                    <div class="col menu">
                        <nav class="navbar navbar-expand-lg navbar-light bg-light">
                            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                                <span class="navbar-toggler-icon"></span>
                            </button>
                            <div class="collapse navbar-collapse" id="navbarNav">
                            <ul>
                                <div ng-controller="fetchLinks">
                                    <li class="nav-item" ng-repeat="element in rawLinks.value">
                                        <a class="nav-link" href="?q={{ element.url }}">{{ element.name }}</a>
                                    </li>
                                </div>
                            </ul>
                          </div>
                        </nav>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12 content">
                        <table class="table" ng-controller="fetchData">
                            <tr ng-repeat="elements in rawData.value">
                                <td ng-repeat="element in elements">
                                    {{ element }}
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

要重复多行,请使用Special Repeat Start and End Points

<table class="table" ng-controller="fetchData">
    <tr ng-repeat-start="elements in rawData.value">
        <th ng-repeat="(key, value) in elements>
            {{key}}
        </th>
    </tr>
    <tr ng-repeat-end>
        <td ng-repeat="element in elements">
            {{ element }}
        </td>
    </tr>
</table>

这将重复两行;第一个用键,第二个用值。

  

您的代码几乎完成了我想要实现的目标,因此我向ng-if="$first"行添加了ng-repeat-start

如果您的目标是只创建一个标题行,则执行此操作会更有效:

<table class="table" ng-controller="fetchData">
    <tr>
        <th ng-repeat="(key, value) in rawData.value[0]">
            {{key}}
        </th>
    </tr>
    <tr ng-repeat="elements in rawData.value">
        <td ng-repeat="element in elements">
            {{ element }}
        </td>
    </tr>
</table>

这可以避免评估多个ng-if表达式的所有开销。它还假设rawData.value数组中的每个对象都具有相同的键。