2016-02-11 56 views
0

我想在AngularJS中使用ng-switch來擴展表格列表中的一行,以便在單擊時顯示另一個表格(包含有關該列表項目的更多詳細信息)。如何使用ng-switch切換顯示AngularJS的嵌套表格?

但是,我無法顯示嵌套表。我注意到這個問題只發生在嵌套表格中。如果我使用ng-switch來擴展td而不是表,它會按預期工作。

這裏有一個的jsfiddle和我的代碼來說明問題:fiddle

的HTML:

<body ng-app="listAndDetails"> 
<div> 
    <table class="table table-bordered" ng-controller="ListAndOneDetailCtrl"> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>e-mail</th> 
      </tr> 
     </thead> 
     <tbody ng-repeat="user in users" ng-click="toggleSelected()" ng-switch on="isSelected(user)"> 
     <tr> 
      <td>{{user.name}}</td> 
      <td>{{user.email}}</td> 
     </tr> 
     <tr ng-switch-when="true" class="light-gray"> 
      <table> 
       <tr> 
        <td>Country</td> 
        <td>Address</td> 
        <td>Description</td> 
       </tr> 
       <tr> 
        <td>{{user.country}}</td> 
        <td>{{user.address}}</td> 
        <td>{{user.desc}}</td> 
       </tr> 
      </table> 
     </tr> 
     </tbody> 
    </table> 
</div> 

<div ng-controller="ListAndManyDetailsCtrl"> 
    <table class="table table-bordered"> 
     <thead> 
     <tr> 
      <th>Name</th> 
      <th>e-mail</th> 
     </tr> 
     </thead> 
     <tbody ng-repeat="user in users" ng-controller="UserCtrl" ng-click="toggleSelected()" ng-switch on="isSelected()"> 
     <tr> 
      <td>{{user.name}}</td> 
      <td>{{user.email}}</td> 
     </tr> 
     <tr ng-switch-when="true" class="light-gray"> 
      <td>{{user.country}}</td> 
      <td>{{user.address}}</td> 
      <td>{{user.desc}}</td> 
     </tr> 
     </tbody> 
    </table> 
</div> 
</body> 

的JavaScript:

angular.module('listAndDetails', []) 

    .value('users', [ 
    { name:'Damien', email:'[email protected]', desc:'Damien details go here...', country:'US', address:'address1'}, 
    { name:'Alex', email:'[email protected]', desc:'Alex details go here...', country:'UK', address:'address2'} 
    ]) 

    .controller('ListAndOneDetailCtrl', function ($scope, users) { 
    $scope.users = users; 

    $scope.selectUser = function (user) { 
     $scope.selectedUser = user; 
    }; 

    $scope.isSelected = function (user) { 
     return $scope.selectedUser === user; 
    }; 
    }) 

    .controller('ListAndManyDetailsCtrl', function ($scope, users) { 
    $scope.users = users; 
    }) 

    .controller('UserCtrl', function ($scope) { 

    $scope.toggleSelected = function() { 
     $scope.selected = !$scope.selected; 
    }; 

    $scope.isSelected = function (user) { 
     return $scope.selected; 
    }; 
    }); 

之上嘗試的div來顯示一個嵌套表點擊列表項,但沒有任何反應。

底部的div試圖顯示孩子td項目單擊列表項時,它的工作原理完全按預期。

有沒有人有任何想法當我試圖展開使用ng-switch顯示嵌套表的問題是什麼?以及如何解決它?

回答

0

你的代碼有兩個錯誤。

首先

在上ng-click控制器ListAndOneDetailCtrl你沒有一個功能toggleSelected

與控制器ListAndOneDetailCtrl你沒有在嵌套表標籤td第一個表。

見正確的代碼在jsfiddle

angular.module('listAndDetails', []) 
 

 
    .value('users', [ 
 
    { name:'Damien', email:'[email protected]', desc:'Damien details go here...', country:'US', address:'address1'}, 
 
    { name:'Alex', email:'[email protected]', desc:'Alex details go here...', country:'UK', address:'address2'} 
 
    ]) 
 

 
    .controller('ListAndOneDetailCtrl', function ($scope, users) { 
 
    $scope.users = users; 
 

 
    $scope.selectUser = function (user) { 
 
     $scope.selectedUser = user; 
 
    }; 
 

 
    $scope.isSelected = function (user) { 
 
     return $scope.selectedUser === user; 
 
    }; 
 
    }) 
 

 
    .controller('ListAndManyDetailsCtrl', function ($scope, users) { 
 
    $scope.users = users; 
 
    }) 
 

 
    .controller('UserCtrl', function ($scope) { 
 

 
    $scope.toggleSelected = function() { 
 
     $scope.selected = !$scope.selected; 
 
    }; 
 

 
    $scope.isSelected = function (user) { 
 
     return $scope.selected; 
 
    }; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="listAndDetails"> 
 
<div> 
 
    <table class="table table-bordered" ng-controller="ListAndOneDetailCtrl"> 
 
     <thead> 
 
      <tr> 
 
       <th>Name</th> 
 
       <th>e-mail</th> 
 
      </tr> 
 
     </thead> 
 
     <tbody ng-repeat="user in users" ng-click="selectUser(user)" ng-switch on="isSelected(user)"> 
 
     <tr> 
 
      <td>{{user.name}}</td> 
 
      <td>{{user.email}}</td> 
 
     </tr> 
 
     <tr ng-switch-when="true" class="light-gray"> 
 
     <td> 
 
      <table> 
 
       <tr> 
 
        <td>Country</td> 
 
        <td>Address</td> 
 
        <td>Description</td> 
 
       </tr> 
 
       <tr> 
 
        <td>{{user.country}}</td> 
 
        <td>{{user.address}}</td> 
 
        <td>{{user.desc}}</td> 
 
       </tr> 
 
      </table> 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div> 
 

 
<div ng-controller="ListAndManyDetailsCtrl"> 
 
    <table class="table table-bordered"> 
 
     <thead> 
 
     <tr> 
 
      <th>Name</th> 
 
      <th>e-mail</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody ng-repeat="user in users" ng-controller="UserCtrl" ng-click="toggleSelected()" ng-switch on="isSelected()"> 
 
     <tr> 
 
      <td>{{user.name}}</td> 
 
      <td>{{user.email}}</td> 
 
     </tr> 
 
     <tr ng-switch-when="true" class="light-gray"> 
 
      <td>{{user.country}}</td> 
 
      <td>{{user.address}}</td> 
 
      <td>{{user.desc}}</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div> 
 
</body>

0

有兩個問題:

  • 你需要一個TD部分封閉你的表;

<tr> 
 
    <td> 
 
    <table> 
 
     <!-- table information --> 
 
    </table> 
 
    </td> 
 
</tr>

  • 需要一些修改,使得所選擇的用戶可以具有的值的控制器。

.controller('ListAndOneDetailCtrl', function ($scope, users) { 
 
    $scope.users = users; 
 
    
 
    $scope.toggleSelected = function(user) { 
 
     $scope.selectedUser = user; 
 
    }; 
 

 
    $scope.selectUser = function (user) { 
 
     $scope.selectedUser = user; 
 
    }; 
 

 
    $scope.isSelected = function (user) { 
 
     return $scope.selectedUser === user; 
 
    }; 
 
    })

這裏有一個更新的小提琴:https://jsfiddle.net/jp43agsb/2/