I need to dynamically create a div using the Bootstrap panel for displaying it. The div's class will either be "panel panel-success" or "panel panel-danger" based on the server response, which can be either "success" or "failure."
To assign the classes "success" or "failure" to the div, I plan to define in my CSS file that the class "success" is equivalent to "panel panel-success" and the class "failure" is equivalent to "panel panel-danger."
The div in question is one of the cells in a UI-grid and is defined in statusTemplate1, statusTemplate2, and statusTemplate3, as shown below:
JavaScript File
angular.module('Spectacle', ['ui.grid'])
.controller('SpectacleCtrl',
function($scope, $http, $q) {
$scope.loaded = true;
$scope.myData = [];
var statusTemplate1 = '<div class="panel panel-success">{{row.entity.build1.number}} <span class="label label-success">P-{{row.entity.build1.passCount}}</span> <span class="label label-danger">F-{{row.entity.build1.failCount}}</span> <span class="label label-warning">S-{{row.entity.build1.skipCount}}</span></div>';
var statusTemplate2 = '<div class="panel panel-success">{{row.entity.build2.number}} <span class="label label-success">P-{{row.entity.build2.passCount}}</span> <span class="label label-danger">F-{{row.entity.build2.failCount}}</span> <span class="label label-warning">S-{{row.entity.build2.skipCount}}</span></div>';
var statusTemplate3 = '<div class="panel panel-success">{{row.entity.build3.number}} <span class="label label-success">P-{{row.entity.build3.passCount}}</span> <span class="label label-danger">F-{{row.entity.build3.failCount}}</span> <span class="label label-warning">S-{{row.entity.build3.skipCount}}</span></div>';
$scope.gridOptions = { data: 'myData',rowHeight:62 };
$scope.gridOptions.columnDefs = [
{ field: 'job' },
{ field: 'build1',cellTemplate: statusTemplate1},
{ field: 'build2',cellTemplate: statusTemplate2},
{ field: 'build3',cellTemplate: statusTemplate3},
{ field: 'build4'},
{ field: 'build5'}
];
function fnGetList(url)
{
var deferred = $q.defer();
$http.get(url)
.success(function (data)
{
console.log("Data: " + data.buildList[0].number);
deferred.resolve(data);
});
return deferred.promise;
};
$scope.GetGridData = function ()
{
console.log("In GetGridData: " + "Getting the data");
$http.get('http://localhost:8989/api/job').
then(function(response) {
$scope.totalJobs = 2;
$scope.totalBuilds = 5;
console.log($scope.totalJobs);
$scope.jobs = response.data.jobs;
$scope.loaded = false;
console.log($scope.jobs);
console.log('size of the response data:'+ response.data.jobs.length +' ');
for(var i=0; i<response.data.jobs.length; i++)
{
console.log('The jobid for job '+i+' is:'+response.data.jobs[i].id);
var url = "http://localhost:8989/api/job/"+response.data.jobs[i].id+"";
fnGetList(url).then(function (content)
{
// Assuming your content.data contains a well-formed JSON
console.log('Content: '+content.name);
if (content !== undefined)
{
console.log('promise success: '+content);
console.log('printing grid options:'+$scope.gridOptions.data);
$scope.myData.push({
"job": content.name,
"build1": content.buildList[0],
"build2": content.buildList[1],
"build3": content.buildList[2]
// "build4": content.buildList[3].number,
// "build5": content.buildList[4].number
});
}
});
}
});
};
});
HTML Code
<div class="gridStyle" ui-grid="gridOptions" ng-init="GetGridData()"></div>