【问题标题】:Angularjs: two ng-repeats not showing the correct information in table columnAngularjs:两个 ng-repeat 未在表列中显示正确的信息
【发布时间】:2018-08-01 03:13:44
【问题描述】:

我正在尝试创建一个动态表,该表可以保存具有不同列数的搜索结果。

我创建了一个表,每个条目都应该有一行,每个数据字段都有一个列,都填充有 ng-repeat -functions,但由于某种原因,它根本没有在列中显示任何信息,尽管它确实创建正确的数量。

如果我尝试在{{}} 中显示e,它会显示存在的正确密钥。如果我在{{}} 中尝试使用i,它会在每一列中显示以下内容(所有列的信息相同,但每一行的信息不同):

{"etunimi":"firstname","sukunimi":"lastname","optunnus":"010101010101011001"}

这里是html:

<table id="raporttiTulos" class="resultTable">
        <tr ng-repeat="i in raportointiLista">
                <td ng-repeat=" e in raportointiAvaimet">{{i.e}}</td>
        </tr>
</table>

这里是负责传入数据的函数:

    $scope.haeMaksut = function(){
            $scope.raportointiAvaimet = {};
            $http.post('/maksuhaku')
             .then(function(res){
                    x = 0;
                    $scope.raportointiLista = res.data.message;
                    for(i in $scope.raportointiLista[0]){
                            $scope.raportointiAvaimet[x] = i;
                    x+=1
                    }
                    console.log($scope.raportointiAvaimet);
                    $scope.maksamattomat = $scope.raportointiLista.length;
                    $scope.lataus = true;
            }, function(error){
                    console.log(error);
            });
    }

这是密钥列表的样子:

Object [ "etunimi", "sukunimi", "optunnus" ]

以下是数据列表中的一些行:

[…]
  [0…99]
    0: Object { etunimi: "firstname", sukunimi: "lastname", optunnus: "101010101010101010", … }

【问题讨论】:

    标签: arrays angularjs html-table row multiple-columns


    【解决方案1】:

    您可以直接访问对象键,而不是这样做。

    HTML

    <tr ng-repeat="i in raportointiLista">
        <td ng-repeat="key in raportointiAvaimet">{{i[key]}}</td>
    </tr>
    

    控制器

    $scope.haeMaksut = function () {
        $scope.raportointiAvaimet = {};
        $http.post('/maksuhaku')
            .then(function (res) {
                $scope.raportointiLista = res.data.message;
                $scope.raportointiAvaimet = Object.keys($scope.raportointiLista[0]);
            }, function (error) {
                console.log(error);
            });
    }
    

    【讨论】:

    • 是的,我可以这样做,但是如果我想将同一个表用于另一个包含例如 5 个具有不同键的列的报告,它将不再起作用。
    • 干得好,现在可以正常工作了!万分感谢!我想问一下是什么区别使它在您的编辑中找到了正确的值?
    • 你在做i.e,它检查e 对象内的e 键而不是e 的值。
    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多