0

メンバーIDに基づいて、他のリストからメンバー名をテーブルに取得するオプションはありますか?

(function(){

var app = angular.module('tableApp',[]);
app.controller('tableController', function($scope){
    $scope.users = [
        { id: 2, name: 'member2'},
        { id: 5, name: 'member5'},
        { id: 1, name: 'member1'},
        { id: 4, name: 'member4'},
        { id: 3, name: 'member3'}
    ];
    $scope.items = [	    
        { product: 'Lorem ipsum', date: '12-March-2013', rate: 12.35, member: 1, status: 'approved' },
        { product: 'dolor sit', date: '1-January-2011', rate: 60.54, member: 3, status: 'pending'},
        { product: 'adipisicing', date: '14-November-2014', rate: 0.99, member: 1, status: 'approved'},
        { product: 'do eiusmod', date: '2-November-2014', rate: 4.00, member: 2, status: 'approved'},
        { product: 'culpa qui', date: '1-November-2014', rate: 34.05, member: 1, status: 'pending'},
        { product: 'mollit anim', date: '3-November-2014', rate: 45.00, member: 4, status: 'approved'}
    ];
});

})();
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="tableApp">
  <table class="table table-striped table-hover" ng-controller="tableController">
    <thead>
      <tr>
        <th>Date</th>
        <th>Amount</th>
        <th>Member</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in items ">
        <td>{{item.product}}</td>
        <td>{{item.date}}</td>
        <td>{{users[item.member-1].name}}</td>
      </tr>
    </tbody>
  </table>
</div>

4

2 に答える 2

3

インデックス番号を使用します。すなわち、ng-repeat="(ind, item) ..."。例:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="tableApp">
    <table class="table table-striped table-hover" ng-controller="tableController">
        <thead>
        <tr>
            <th>Date</th>
            <th>Amount</th>
            <th>Member</th>
            <th>Name</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="(ind, item) in items | orderBy : 'rate'">
            <td>{{item.product}}</td>
            <td>{{item.date}}</td>
            <td>{{item.member}}</td>
            <td><span ng-repeat="info in users">{{info.id === item.member ? info.name : ''}}</span></td>
        </tr>
        </tbody>
    </table>
</div>

<script>
    (function(){

        var app = angular.module('tableApp',[ ]);
        app.controller('tableController', function($scope){
            $scope.users = [
                { id: 2, name: 'member2'},
	            { id: 5, name: 'member5'},
	            { id: 1, name: 'member1'},
	            { id: 4, name: 'member4'},
	            { id: 3, name: 'memeber3'}
            ];
            $scope.items = [
                { product: 'Lorem ipsum', date: '12-March-2013', rate: 12.35, member: 1, status: 'approved' },
                { product: 'dolor sit', date: '1-January-2011', rate: 60.54, member: 3, status: 'pending'},
                { product: 'adipisicing', date: '14-November-2014', rate: 0.99, member: 1, status: 'approved'},
                { product: 'do eiusmod', date: '2-November-2014', rate: 4.00, member: 2, status: 'approved'},
                { product: 'culpa qui', date: '1-November-2014', rate: 34.05, member: 1, status: 'pending'},
                { product: 'mollit anim', date: '3-November-2014', rate: 45.00, member: 4, status: 'approved'}
            ];
        });

    })();
</script>

于 2014-11-29T14:43:47.383 に答える
1

アイテムが与えられたときにユーザーを検索するメソッドをスコープに追加します。

(function(){

var app = angular.module('tableApp',[ ]);
app.controller('tableController', function($scope){
    $scope.users = [
        { id: 2, name: 'member2'},
        { id: 5, name: 'member5'},
        { id: 1, name: 'member1'},
        { id: 4, name: 'member4'},
        { id: 3, name: 'memeber3'}
    ];
    $scope.items = [        
        { product: 'Lorem ipsum', date: '12-March-2013', rate: 12.35, member: 1, status: 'approved' },
        { product: 'dolor sit', date: '1-January-2011', rate: 60.54, member: 3, status: 'pending'},
        { product: 'adipisicing', date: '14-November-2014', rate: 0.99, member: 1, status: 'approved'},
        { product: 'do eiusmod', date: '2-November-2014', rate: 4.00, member: 2, status: 'approved'},
        { product: 'culpa qui', date: '1-November-2014', rate: 34.05, member: 1, status: 'pending'},
        { product: 'mollit anim', date: '3-November-2014', rate: 45.00, member: 4, status: 'approved'}
    ];
    $scope.findUser = function(item){
        for (var i = 0; i < $scope.users.length; i++) {
            user = $scope.users[i];
            if (user.id === item.member){
              return user;
            }
        }
    }
});

})();

そのメソッドを使用して、ユーザーを検索します。

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="container" ng-app="tableApp">
  <table class="table table-striped table-hover" ng-controller="tableController">
    <thead>
      <tr>
        <th>Date</th>
        <th>Amount</th>
        <th>Member</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="item in items ">
        <td>{{item.product}}</td>
        <td>{{item.date}}</td>
        <td>{{findUser(item).name}}</td>
      </tr>
    </tbody>
  </table>
</div>
于 2014-11-29T15:47:44.383 に答える