Create a dynamic table from and array

Ahmed15
Tera Contributor

 Hi,

I need some assistance on creating a dynamic table from an array.

I want the configuration item to be a fixed header and the elements to be the custom headers with the state below.

 

The script so far and outcome:

 

var arr = [];
var ga = new GlideAggregate("cert_element");
ga.addQuery("cert_task", current.sys_id);
ga.query();
while (ga.next()) {

var obj = {};
obj.configuration_item= ga.configuration_item.toString(),
obj.element = ga.element.toString();
obj.state = ga.state.toString();
arr.push(obj)

}

 gs.log(JSON.stringify(arr));

 

results:  

[{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_environment","state":"Certified"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_affected_business_units","state":"Failed"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_lifecycle_state","state":"Failed"}]

 

 Any advise on printing out on to a table would be much appreciated. 

 

 

Ahmed

 

 

 

 

 

7 REPLIES 7

Hi Ahmed,

Try this code

var arr = [{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_environment","state":"Certified"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_affected_business_units","state":"Failed"},{"configuration_item":"5a40ea6ddb56e680067450cbbf961921","element":"u_lifecycle_state","state":"Pass"}]

var myhtml="<html><body><table><th>Configuration Item</th><th> u_environment</th><th> u_affected_business_units </th><th> u_lifecycle_state </th><tr>";

for(i=0;i<arr.length;i++) {
  switch(i) {
    case 0: {
      myhtml = myhtml + "<td>"+arr[i].configuration_item+"</td><td>"+arr[i].state+"</td>"; 
      break;
    }
    case 1:{
      myhtml = myhtml + "<td>"+arr[i].state+"</td>";
      break;
    }
    case 2: {
      myhtml = myhtml + "<td>"+arr[i].state+"</td>";
      break;
    }
  }
}
myhtml = myhtml+"</tr></table></body></html>";
gs.print(myhtml);

Mark the comment as a correct answer and also helpful once worked.

Hello Ahmed,

If my given solution has worked for you, kindly mark the comment as a correct answer and also helpful.

Harsh Vardhan
Giga Patron

did you design the html table? if yes then you can simply call the json object inside the html table tag.