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

Jace Benson
Mega Sage

Everything I've read about .getRowNumber() has been a performance drag.  You dont need to use that but you could just as easily make your own variable;

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
test<br/>
	<g:evaluate jelly="true" object="true">
	var i=0;
	var results = [];
		var inc = new GlideRecord('incident');
	//inc.addActiveQuery();
	//inc.addQuery('priority',1);
	inc.setLimit(10);
	inc.query();
	while(inc.next()){
	  results.push({
		link: inc.getLink(),
		listLink: inc.getTableName() + '_list.do?sysparm_query=' + inc.getEncodedQuery(),
		className: parseInt(results.length,10) % 2 == 0 ? 'even' : 'odd'
	  });
	}
</g:evaluate>
<!--results: ${results.length}
    results.length: ${results.length}<br/>
	results: ${results}<br/>
	results[0]: ${results[0]}<br/>-->
	<table><tbody>
	<j:while test="${results.length!=i}">
		<tr class="${results[i].className}">
			<td>${results[i].link}</td>
			<td>${results[i].listLink}</td>
		</tr>
		<g:evaluate var="not_important" expression="i++"/>
	</j:while>
		</tbody></table>
</j:jelly>

Naples1966
Giga Contributor

First of all, thank you for your answer.

Since this is the first time to build the script in the service, I have trouble understanding your script and I have not found anything missing to fix ....

Because, I copied your processing into the PAGE UI, and then I renamed a classname ("color" instead of "classname") and I removed some spaces. for instance  : "color:parseInt(results.length,10)%2==0 " instead of "

className: parseInt(results.length,10) % 2 == 0 ? 'even' : 'odd'

 "

 

It did not work! ... Sorry ...

 

See below, I put your processing copied from the UI page . Is it correct ?

 

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
test<br/>
<g:evaluate jelly="true" object="true">
var i=0;
var results = [];
var inc = new GlideRecord('incident');
//inc.setCategory('homepage');
inc.setLimit(10);
inc.query();

while(inc.next()){
results.push({
link:inc.getLink(),
listLink:inc.getTableName()+'_list.do?sysparm_query='+inc.getEncodedQuery(),
color:parseInt(results.length,10)%2==0 ? 'even' : 'odd'
});
}
</g:evaluate>

results: ${results.length}
results.length: ${results.length}<br/>
results: ${results}<br/>
results[0]: ${results[0]}<br/>

<table><tbody>
<j:while test="$[inc.next()]">
<tr class="${results[i].color}">
<td>${results[i].link}</td>
<td>${results[i].listLink}</td>
</tr>
<g:evaluate var="not_important" expression="i++"/>
</j:while>
</tbody></table>
</j:jelly>

When you say it doesn’t work what does it do? Did you try it with out modification?

hi, Yes, of course, I tried it . the scroll list did not appeared . And, I removed only "

 

see below, screenshots.

find_real_file.pngfind_real_file.png