how to retreive a number of line as record to display ? or to calculate to know if a number of line is odd or even

Naples1966
Giga Contributor

I don't find the solution to retreive a number of line as record to calculate to know if this  is odd or even.

if even,  set the green in color of record  , if no, gray in color of record.

Because, I would like to appear the alternating color of line in a table.

there is my script, see below. May you help it   please ?. Thank you in advance for your return

-----

<g2:evaluate var="jvar_inc">
var inc = new GlideRecord('incident');
var color;

inc.addActiveQuery();
inc.addQuery('priority',1);

//** here : how to retreive a number of line as record ? **//

//** ROW as a number of line **//

var row = inc.getRowNumber();
row;


if(row % 2 == 0){
Color = "even";
}else{
Color= "odd";
}

inc.setCategory('homepage');
inc.query();
</g2:evaluate>

<style>
.odd{background-color: white;}
.even{background-color: gray;}
</style>

<table class="fixed_header" >
<tbody >
<j2:while test="$[inc.next()]">
<j2:set var="jvar_inc_link" value="incident.do?sys_id=$[inc.sys_id]"/>
<j2:set var="jvar_inc_list_link" value="incident_list.do?sysparm_query=active=true"/>

<tr class="${color}">
<td ....</td>
</tr>
</j2:while>

</tbody>
</table>

----

1 ACCEPTED SOLUTION

Sunil B N
ServiceNow Employee
ServiceNow Employee

you may want to add a unique style class name to the your table and wrap the above CSS with the parent table style class.

View solution in original post

14 REPLIES 14

Sorry, it looks like the style code didnt get pasted into the snippet.  Im standing up my PDI now to look at it again.

hi, thank you for your message.

I made an effort to dig into your script and tested, step by step. it works at 80%.

Because, the value , as a data, is not updated in the class. Here, the color did not appear in the list while 'even' and 'odd' are displayed in the column. This means that 'even' or 'odd' are not forced and placed in a class variable , named "colorClass". (bold in my script).

this doesn't work.

see attachment file concerning a displayed result . ( red circle, here, I would want to take these values to get in a class to display a color in a line)

Are there some things missed? Could you help me to correct on this subject?

See the screenshot of my script, below.

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

<g:evaluate var="jvar_inc" jelly="true" object="true">

var i=0;
var results =new Array(0);
var results4 =new Array(0);

var inc = new GlideRecord('incident');
inc.addActiveQuery();
inc.addQuery('priority',3);
inc.setCategory('homepage');
inc.setLimit(10);
inc.query();

while(inc.next()){
results.push(inc.number.toString()+ " ")
results4.push(colorClass=(parseInt(results.length,10) % 2 ==0 ? 'even':'odd'))

}
</g:evaluate>

<style>
.fixed_header{
width: 400px;
table-layout: fixed;
border-collapse: collapse;
}


.fixed_header thead {
background: black;
color:#fff;
}

.fixed_header th {
padding: 5px;
text-align: left;
width:200px;
}

.fixed_header td {
padding: 5px;
text-align: left;

}

.odd{background-color: green;}
.even{background-color: gray;}

</style>

// results: ${results.length}<br/>
results.length: ${results.length}<br/>


<div>
<table><tbody>
<j:while test="${results.length!=i}">
<tr class="${results4[i].colorClass}">
<td>${results[i]}</td>
<td>${results4[i]}</td>
</tr>
<g:evaluate var="not_important" expression="i++"/>
</j:while>
</tbody></table>
</div>

<div>

<table border="0" cellspacing="2" cellpadding="0" width="100%">
<j2:while test="$[inc.next()]">
<j2:set var="jvar_inc_link" value="incident.do?sys_id=$[inc.sys_id]"/>
<j2:set var="jvar_inc_list_link" value="incident_list.do?sysparm_query=active=true"/>
<tr >
<td>
<a href="$[jvar_inc_link]" class="linked" style="padding-right:10px;">$[inc.number].toString()</a>
</td>
<td>$[inc.short_description]</td>
</tr>
</j2:while>
<tr>
<td align="center" colspan="2"><a href="$[jvar_inc_list_link]" class="linked">${gs.getMessage("View all active Incidents")}</a></td>
</tr>
</table>
</div>

</j:jelly>

 

find_real_file.png

 

I posted my 2 screenshot in my previous message. it corrected... I didn't always find the problem to get the data in a class variable ... See my last messag. thank you in advance to help me . sincerely

Sunil B N
ServiceNow Employee
ServiceNow Employee

You don't need to worry about glide record at all. Make use of CSS selector


<style> 
tr:nth-child(even) {background: gray}
tr:nth-child(odd) {background: white}
</style>

Cheers,
Sunil B N
PS: Please mark this as answer/helpful if it was useful for you.

It works now ! whew !... And now, I build a developpment in script for a scroll list... I hope it works well... again thank you very much sincerely