表格行中的重复数据,Angular

时间:2018-05-17 09:12:22

标签: javascript angularjs

为什么我在表的所有行中获得相同的值。表具有行,单击行时,将打开另一行,其中包含其他信息,每行包含与上次单击的行相同的数据。可能是因为我在相同的对象中保存数据,但我真的不知道如何解决它。我从json文件中获取带有因子的数据,因此您不会感到困惑,我的数据来自哪里。这是代码。非常感谢。enter image description here

    'use strict';

angular.module('sbAdminApp')
    .controller('TreeTableCtrl', TreeTableCtrl);

function TreeTableCtrl($scope, $http, factor) {
    
    $scope.nonCollapsedUser = [];

 var getUsers = function () {
            factor.getUse().then(function (response) {
                $scope.users = response.data;
            });
        };
        getUsers();
        var getUsersInfo = function () {
            factor.getChi().then(function (response) {
                $scope.child = response.data;
                console.log($scope.child);

            });
        };


        getUsersInfo();

        $scope.togglePerson = function (index) {
            $scope.nonCollapsedUser.push(index);
            $scope.newObj=$scope.child[index];
            console.log($scope.newObj);
        };


        $scope.hidePerson = function (index)
        {
            for(var i=0; i<$scope.nonCollapsedUser.length; i++){
                if($scope.nonCollapsedUser[i] === index){
                    $scope.nonCollapsedUser.splice(i, 1);
                }
            }
        };


}
<div class="panel panel-default" style="background: #fcf8e3">
    <div class="panel-body">
        <table st-safe-src="leads" class="table table-hover">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Last Name</th>
            </tr>
            </thead>
            <tbody ng-repeat="user in users track by $index">
            <tr>
                <td>{{user.id}}</td>
                <td>{{user.name}}</td>
                <td>{{user.lastName}}</td>
                <td>
                    <button ng-if="nonCollapsedUser.indexOf($index) == -1" class="btn" ng-click="togglePerson($index)">
                        <i class="fa fa-arrow-down"></i></button>
                    <button ng-if="nonCollapsedUser | contains:$index" class="btn" ng-click="hidePerson($index)"><i
                            class="fa fa-arrow-up"></i></button>
                </td>
            </tr>

            <tr ng-if="nonCollapsedUser | contains:$index">
                <div>
                    <td>{{newObj.address}}</td>
                    <td>{{newObj.mobNumb}}</td>
                </div>
            </tr>
        </table>
        </td>
        </tr>
        </tbody>
        </table>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

根据你的描述, 我假设你的用户,像这样的子数组

  $scope.users =[
  {"id":1,"name":"Mirko","lastName":"Spriko"},
  {"id":2,"name":"Pero","lastName":"Peric"},
  {"id":3,"name":"Marko","lastName":"Prezime"}
  ];

  $scope.child =[
  {"address":"Address1","mobNumb":"47423756324"},
  {"address":"Address2","mobNumb":"73454635545"},
  {"address":"Address3","mobNumb":"87464343648"}
  ];

首先,您需要将child映射到用户

  angular.forEach($scope.users, function(value,key){
      value.child =$scope.child[key];
  });

现在你可以实现你的目标

<table st-safe-src="leads" class="table table-hover">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Last Name</th>
                <th></th>
            </tr>
            </thead>
            <tbody  ng-repeat="user in users" >
            <tr>
                <td>{{user.id}}</td>
                <td>{{user.name}}</td>
                <td>{{user.lastName}}</td>
                 <td>
                    <button ng-show="nonCollapsedUser.indexOf($index) == -1" class="btn" ng-click="togglePerson($index)">
                        <i class="fa fa-arrow-down"></i>veiw</button>
                    <button ng-show="nonCollapsedUser.indexOf($index) > -1" class="btn" ng-click="hidePerson($index)"><i
                            class="fa fa-arrow-up"></i>hide</button>
                </td>

            </tr>
            <tr ng-show="nonCollapsedUser.indexOf($index) > -1">
                    <td></td>
                    <td>{{user.child.address}}</td>
                    <td>{{user.child.mobNumb}}</td>
                    <td

            </tr>

        </table>

这有效JSFiddle