Set field styles based on value

Alex Saager1
Tera Contributor

Hi community,

 

I'm trying to set a field style based on a numerical value and wondered if this was even possible.

 

So I have a custom field on the cmdb_ci_computer called score which will be a number and they way I envision this working is if the value is if the value is...

 

less than 30 highlight the field red

between 30 - 60 orange 

between 60 - 100 green.

 

Is what I'm trying even possible?

 

Many thanks,

Alex

1 ACCEPTED SOLUTION

Hi,

Yes it's possible to include a range.

Modify your value into something like this:

javascript&colon; (current.u_score > 0 && current.u_score < 30)

 

View solution in original post

7 REPLIES 7

I can't see any difference between your line and the one I suggested, but great that it worked.


Also, another little detail, make sure that you don't have conflicting styles. In your initial question, the field should be both green and orange if the value was 60.

For some reason on here it shows as &colon; rather than after the javascript, that's just my mistake

The values we're still working on but the requirements still works as needed.

 

Thanks again

Ehab Pilloor
Mega Sage

Hi @Alex Saager1,

Yes it is possible to do it. Use Client Script and make sure your field is of type integer. Otherwise convert string to integer, apply following client script inside the function and change the value back to string.

if (newValue === ''){
control.style = 'background-color';
}
if (newValue <= 30){
control.style = 'background-color: red'; 
} else if (newValue > 30 && newValue <60) {
control.style = 'background-color: orange';
} else if (newValue >= 60 && newValue < 100) {
control.style = 'background-color: green';
}

  This should be onChange client script and field name should be selected as score. Also isolate the script

 

If you found this reply helpful, please mark it as solution and helpful.

 

Thanks and Regards,

Ehab