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

asifnoor
Kilo Patron

Hi Ahmed,

Do you want to print this data in the form of a html table or create a db table with this data?

Ahmed15
Tera Contributor

Hi Asif,

 

I want to print the data on a html table.

 

Thanks,

 

Ahmed

Here is how you can do it

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":"Failed"},
{"configuration_item":"tstt","element":"u_lifecyclaaaae_state","state":"Failsssd"}]

gs.print(arr.length);
var myhtml="<html><body><table><th>Configuration Item</th><th>Element</th><th>Environment</th>";
for(i=0;i<arr.length;i++) {
myhtml = myhtml+"<tr><td>"+arr[i].configuration_item+"</td><td>"+arr[i].element+"</td><td>"+arr[i].state+"</td></tr>";
}
myhtml = myhtml+"</table></body></html>";
gs.print(myhtml);

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

 

Ahmed15
Tera Contributor

Thank you for your assistance.

 

Apologies if I wasn’t clear  I was hoping to get format like this:

 

configuration item

u_environment

u_affected_business_units

u_lifecycle_state

5a40ea6ddb56e680067450cbbf961921

Certified

Failed

Failed

 

So each element will be the header and the state the value below.

 

Thanks,

 

A