1

I am trying to generate a dynamic table from Angular JS by receiving a response from Spring Rest Service.

This is my code:

// JavaScript Document
var app = angular.module("SDLC", []);
app.config(['$httpProvider', function($httpProvider) {
 $httpProvider.defaults.useXDomain = true;
 $httpProvider.defaults.headers.common = 'Content-Type: application/json';
 delete $httpProvider.defaults.headers.common['X-Requested-With'];
 }]);
var urlBase = "http://localhost:8080";
app.controller('projecttable_controller', function($scope, $http) {
		
 $scope.ordered_columns = [];
 $scope.all_columns = [{
 "title": "Project Name",
 "type": "string"
 }, {
 "title": "Project Description",
 "type": "string"
 }, {
 "title": "Owner",
 "type": "string"
 }, {
 "title": "Start Date",
 "type": "string"
 }, {
 "title": "End Date",
 "type": "string"
 },{
 "title": "Record ID",
 "type": "string"
 }];
 
 $http.get(urlBase+'/hello?input=raina').
 success(function(data) {
		//alert(data);	
		$scope.data=data; 
		alert($scope.data);
	 });
	 
 
 $scope.$watch('all_columns', function() {
 update_columns();
 }, true);
 var update_columns = function() {
 $scope.ordered_columns = [];
 for (var i = 0; i < $scope.all_columns.length; i++) {
 var column = $scope.all_columns[i];
 $scope.ordered_columns.push(column);
 }
 };
});
<div class="btn-box-row row-fluid">
 	<table class="table table-striped">
 <thead>
 <tr>
 <th ng-repeat="c in ordered_columns">{{ c.title }}
 </th>
 </tr>
 </thead>
 <tbody>
 <tr ng-repeat="ds in data">
 <td ng-repeat="c in ordered_columns">{{ d[c.title] }}</td>
 </tr>
 </tbody>
 </table>
 </div>

There are no errors in the console. The table headers are displayed correctly. However, the data does not get populated in the table. If I hardcode the json in $scope.data, the table gets populated properly. Any help would be appreciated.

asked May 3, 2015 at 7:23
2
  • 1
    you get your response data and you place it in "$scope.data" and that is where it always stays Commented May 3, 2015 at 7:35
  • change <tr ng-repeat="ds in data"> to <tr ng-repeat="d in data">, seems to be typo Commented May 3, 2015 at 7:49

1 Answer 1

2

When you fetch your GET response, you write $scope.data = data; and you do nothing with $scope.data

change:

 $http.get(urlBase+'/hello?input=raina').
 success(function(data) {
 //alert(data); 
 $scope.data=data; 
 alert($scope.data);
 });

to:

 $http.get(urlBase+'/hello?input=raina').
 success(function(data) {
 //alert(data); 
 $scope.all_columns=data; 
 alert($scope.data);
 });

Because I see you populate your table from $scope.all_columns

answered May 3, 2015 at 7:39
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.