Anubha Gupta
Anubha Gupta

Reputation: 189

GroupBy filter is not working in angularJs

I have created a table in angularjs. Now I am trying to group the table rows based on a column value whose title is "source".

For that, I am using groupBy filter. It is not giving any error. But it is displaying empty rows now. Can someone tell me where I am going wrong?

Below is my code:

 <table>
    <thead>
        <tr>
           <th>Metric</th>
           <th>Source</th>
           <th class="value">Value</th>
           <th></th>
        </tr>
        </thead>
        <tr ng-repeat="metric in metrics | filter:query | orderBy:orderProp | groupBy:'info.source'>
            <td class="metric">{{metric.name}}</td>
            <td class="source">{{metric.info.source}}</td>
            <td class="value">{{metric.info.value}}</td>
       </tr>
    </table>

I am pasting a sample of my Json data to get you an idea about my table data:

[{
  "name": "kafka.health",
  "info": {
    "source": "kafka",
    "value": "OK",
    "timestamp": "1459438855068",
    "causes": ""
  }
},
{
  "name": "kafka.bringo.health",
  "info": {
    "source": "kafka",
    "value": "Error",
    "timestamp": "1459438855068",
    "causes": ""
  }
}]

Upvotes: 0

Views: 1370

Answers (2)

Anubha Gupta
Anubha Gupta

Reputation: 189

I myself found the solution of my problem. Correct way to use "groupBy" filter in table is as below:

var app = angular.module("myApp", ['angular.filter']);
app.controller("SimpleController", function($scope) {
  $scope.metrics =  [{
  "name": "kafka.health",
  "info": {
    "source": "kafka",
    "value": "OK",
    "timestamp": "1459438855068",
    "causes": ""
  }
},
{
  "name": "kafka.bringo.health",
  "info": {
    "source": "kafka",
    "value": "Error",
    "timestamp": "1459438855068",
    "causes": ""
  }
},
{
  "name": "dm.bingo.health",
  "info": {
	"source": "deployment-manager",
	"value": "Error",
	"timestamp": "1459438855068",
	"causes": ""
  }
},
{
  "name": "dm.different.health",
  "info": {
	"source": "deployment-manager",
	"value": "Warn",
	"timestamp": "84834883483",
	"causes": ""
  }
},
{
  "name": "kadmfka.bringo.health",
  "info": {
	"source": "deployment-manager",
	"value": "Error",
	"timestamp": "1459438855068",
	"causes": ""
}
}];
});
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.16/angular-filter.js" > </script>
    <style>
	table{
	width: 100%;
	border: 1px solid black;
	border-collapse: collapse;
	}
	table>thead>tr>th, table>tbody>tr>td{
	border: 1px solid black;
	}
</style>

</head>    
<body ng-app="myApp">
        <div>
            <div data-ng-controller="SimpleController">      
             <table>
		<thead>
			<tr>
			   <th>Metric</th>
			   <th>Source</th>
			   <th class="value">Value</th>
			   <th></th>
			</tr>
		</thead>
		<tbody>
		<tr ng-repeat="metric in metrics | groupBy:'info.source' ">
				<td class="metric">
					<table>
					<tr ng-repeat="item in metric">
					<td>{{item.name}}</td>
					</tr>
					</table>
				</td>
				<td class="source">{{metric[0].info.source}}</td>
				<td class="value">
				<table>
					<tr ng-repeat="item in metric">
					<td>{{item.info.value}}</td>
					</tr>
			   </table>
			   </td>
		</tr>
		</tbody>
    </table>
            </div>
        </div>
            </div>
        </div>
    </body>

    </html>

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222582

Use

 <tr ng-repeat="(key, value) in metrics | groupBy:'info.source' ">

DEMO

var app = angular.module("myApp", ['angular.filter']);
app.controller("SimpleController", function($scope) {
  $scope.metrics =  [{
  "name": "kafka.health",
  "info": {
    "source": "kafka",
    "value": "OK",
    "timestamp": "1459438855068",
    "causes": ""
  }
},
{
  "name": "kafka.bringo.health",
  "info": {
    "source": "kafka",
    "value": "Error",
    "timestamp": "1459438855068",
    "causes": ""
  }
}];
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.16/angular-filter.js" > </script>
<body ng-app="myApp">
    <div>
        <div data-ng-controller="SimpleController">      
         <table>
    <thead>
        <tr>
           <th>Metric</th>
           <th>Source</th>
           <th class="value">Value</th>
           <th></th>
        </tr>
        </thead>
        <tr ng-repeat="(key, value) in metrics | groupBy:'info.source' ">
            <td class="metric">{{key}}</td>
            <td class="source">{{key}}</td>
            <td class="value">{{key}}</td>
       </tr>
    </table>
        </div>
    </div>
        </div>
    </div>
</body>

</html>

Upvotes: 0

Related Questions