在Angularjs中显示输出时删除重复的名称

时间:2016-10-27 09:11:23

标签: javascript angularjs

我在

下有一个json(在我的控制器中)
//creating an application module
var myAppModule = angular.module("myApp", []);


myAppModule.controller("MyCtrl", function($scope, $http){   

    var jsonData = [
                      {
                        "bucket": ">120",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 640                      
                      },
                      {
                        "bucket": ">120",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1391
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1081
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 408
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 998
                      },                      

                      {
                        "bucket": "X",
                        "productCode": "SBML+",
                        "countOfAllocatedAccount": 93
                      }
                    ];
            $scope.products = jsonData;

    });//end controller

和我的观点(index.html)在

下面
<body ng-app="myApp">       
        <div ng-controller="MyCtrl" align="center">  
            <table border="1">
                <tr>

                      <th>Bucket</th>   
                      <th>PRODUCT_CODE</th>              
                      <th>Allocated #</th>                   
                </tr>
                <tr ng-repeat="p in products">

                      <td><span>{{p.bucket}}</span></td>                                     
                      <td><span>{{p.productCode}}</span></td>
                      <td><span>{{p.countOfAllocatedAccount}}</span></td>                     
                </tr>               
            </table>
        </div>      
    </body>
</html>

当前输出

enter image description here

但我正在寻找以下输出

enter image description here

3 个答案:

答案 0 :(得分:1)

你可以这样做。

将当前的桶值与前一个桶值进行比较,如果它们相同,则不要显示它。

<tr ng-repeat="p in products">
    <td><span ng-show="products[$index-1].bucket != p.bucket">{{p.bucket}}</span></td>                                     
    <td><span>{{p.productCode}}</span></td>
    <td><span>{{p.countOfAllocatedAccount}}</span></td>                     
</tr> 

您可以在此处查看结果JSBIN

答案 1 :(得分:1)

&#13;
&#13;
function myCtrl($scope) {
    var jsonData = [
                      {
                        "bucket": ">120",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 640                      
                      },
                      {
                        "bucket": ">120",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1391
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1081
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 408
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 998
                      },                      

                      {
                        "bucket": "X",
                        "productCode": "SBML+",
                        "countOfAllocatedAccount": 93
                      }
                    ];
            $scope.products = jsonData;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<div ng-app ng-controller="myCtrl">
    <table border="1">
                <tr>

                      <th>Bucket</th>   
                      <th>PRODUCT_CODE</th>              
                      <th>Allocated #</th>                   
                </tr>
                <tr ng-repeat="p in products">

                      <td ><span ng-show="products[$index-1].bucket != p.bucket">{{p.bucket}}</span></td>                                     
                      <td><span>{{p.productCode}}</span></td>
                      <td><span>{{p.countOfAllocatedAccount}}</span></td>                     
                </tr>               
            </table>
</div>
&#13;
&#13;
&#13;

查看代码..代码段

答案 2 :(得分:1)

//creating an application module
var myAppModule = angular.module("myApp", []);


myAppModule.controller("MyCtrl", function($scope, $http){   

    var jsonData = [
                      {
                        "bucket": ">120",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 640                      
                      },
                      {
                        "bucket": ">120",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1391
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBHL",
                        "countOfAllocatedAccount": 1081
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 408
                      },
                      {
                        "bucket": "1-30",
                        "productCode": "SBML",
                        "countOfAllocatedAccount": 998
                      },                      

                      {
                        "bucket": "X",
                        "productCode": "SBML+",
                        "countOfAllocatedAccount": 93
                      }
                    ];
         
  
    $scope.products = jsonData;
     $scope.products.push({ "bucket": "X", "productCode": "SBML", "countOfAllocatedAccount": 9 });
 $scope.products=RowSpanSort(jsonData);

       function RowSpanSort(localData) {
        var i, j;

        for (i = 0; i < localData.length; i++) {
            var l1 = localData[i].bucket;

            localData[i].rowspan = 1;

            for (j = i + 1; j < localData.length; j++) {
                var l2 = localData[j].bucket;
                if (l1 == l2) {
                    localData[i].rowspan += 1;
                }
                else {
                    break;
                }
            }
            i = j - 1;
        }
        return localData;
    }

    });//end controller
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<body ng-app="myApp">       
        <div ng-controller="MyCtrl" align="center">  
            <table border="1">
                <tr>

                      <th>Bucket</th>   
                      <th>PRODUCT_CODE</th>              
                      <th>Allocated #</th>                   
                </tr>
                <tr ng-repeat="p in products">

                     <td ng-if="p.rowspan" rowspan={{p.rowspan}} align="center" style="vertical-align:middle;">{{p.bucket}} </td>                   
                      <td><span>{{p.productCode}}</span></td>
                      <td><span>{{p.countOfAllocatedAccount}}</span></td>                     
                </tr>               
            </table>
        </div>      
    </body>
</html>

</html>