Field color in grid/form

How and where can we set a field value color depending on some conditional statements?

There is a macro where you can set all kind of field properties in a grid. But afaik there’s no such thing for forms funny enough.

Ok so this will be a request for forms too.

C’mon guys. You don’t need a macro for everything do you?

onLoad: //or wherever you need it
coloration();

PHP Method coloration:

$field = ‘invoice_id’;
$color = ‘red’;
if({invoice_status} == 1)
{
$color = ‘green’;
}
sc_ajax_javascript(‘set_color’,array($field,$color));

Javascript Method set_color(field,color):

document.F1[field].style.backgroundColor = color;

There you have it, no need to wait and wait and wait …

jsb

Albert, which macro for grid can do it?

@jsbinca nice workaround but all that code can be reduced by a simple macro :slight_smile:
@daretzki sc_field_style,sc_field_color

@jsbinca: This is a nice workaround. But afaik F1 is not a constant variable and might change. It requires that before doing this you need to hack into the code to see what variable has been generated? I’m not a javascript guru so I’m not sure. So this little sample is even very helpful for me and I will check this out for sure. Keep in mind that SC is a tool with a broad audience, not everybody is a die-hard programmer. If there are macro’s for changing grid fields then it’s not logical that there aren’t for a form. I never understood that, especially if I see how easy it can be done looking at your sample.

Hi there,
don’t get me wrong, I love the macros and I have a long list what I would like to see realized. Instead of some fancy graphs. :slight_smile:

Albert, I totally agree with you. It is a tool and I would go a little bit further and say it’s more a tool box and compared what else is available, a pretty good one (and nothing is perfect).

Rant mode on
It is certainly not a box of Lego blocks you somehow stick together and get a masterpiece of art, nor is it an ‘eierlegende Wollmilchsau’.
Rant mode off

Try to do it on a grid ‘by hand’ and you understand why there is a macro for this task.
For a form there is no need for it, because the macro you need is already there: sc_ajax_javascript(), that’s it! Think about it. You would save one line of code but loose control.
And for the Javascript part, I had to look up the name of the attribute I had to set. Don’t think of me as a Javascript guru. :slight_smile:
I’m using SC for two years now (popped a bottle of champagne yesterday) and never had a document variable other than F1 for a simple form, so I think it’s pretty save to use.
I’m not sure but I believe having read somewhere in the documentation/samples/knowledge base referring to this variable. And if it changes some day … yeah.

This isn’t a workaround because there is nothing to work around. (nice pun isn’t it?) In the end we are calling ourselves programmer, aren’t we?

Sorry for wandering slightly off topic. :slight_smile:

Doesn’t matter, I’m happy with the sample. Still think it should be a macro. Not for the one line less code, but for consistancy…

It is very scary to see that no one actually knows how to change a colour of a field in a form using the scriptcase application. Not even Scriptcase can tell you either! I bought the scriptcase product thinking it could help us in saving development time but now it takes more time in trying to figure out where to put what code in scriptcase for it to work. Scarier is to see the answers given by experts about the usage of scriptcase. Yes I spend one week in reading most of the posts in this forum.

sc_ajax_javascript(‘set_color’,array($field,$color )); is a macro???

Come on Scriptace - Prove me wrong on my assumption and post your code how to add colour into a field on a certain condition of a FORM not a GRID or Report. I might then use your product in future.

Hi jsb
Are you not using a macro in your code above? sc_ajax_javascript is a SC macro?

Yes, sc_ajax_javascript() is a macro but it does not change the style of a form field and that is what the discussion was about.
It calls a javascript function instead, which sets the field color.
There is no macro for setting the attributes of form field like the sc_field_style() macro we have for grid fields.

jsb

Agreed! Thanks jsb

I must not understand how the macros are used. I am trying to dynamically change the field text color. I use sc_field_color. I put it under scriptinit event. all I get is…
Fatal error: Call to undefined function sc_field_color() in C:\Program Files (x86)\NetMake\v71\wwwroot\scriptcase8\app\project6\form_dbo_gcFRADDFranchiseeEmail\form_dbo_gcFRADDFranchiseeEmail_apl.php on line 858

where do you put the code to change field color via a macro?

thanks

if
({freIncludeInDist} =1) { sc_field_color (“Contact/Entity Name”, “#006600”); }
else
{ sc_field_color (“Contact/Entity Name”, “000000”);
}

above is my code copied directly from the help manual

If you are in a grid application it should work, but if you have a form (also the editable grid) it will not.

thanks, im using editable grid so that explains my confustion.

Hi all, I am using this JS code to colour an entire row based on a value in a cell. It will check for the right field and then a value in the field. Later I’ll probably extend this to use a SQL table so the users can create their own colour filter.

Put the following in your event onFooter


echo '<script>


$("tbody tr td span").each(function() {

var cellValue = $(this).text();
var cellName = $(this).attr("id");

// Call this function as many times as you want. The later calls will override the colour of previous calls.
// ColourRow parameters - field name to check, field string to check for, value to check, value to check for, colour to change row too.

ColourRow.call($(this), cellName, "visa_type", cellValue, "Follower", "rgb(250, 255, 181)");
ColourRow.call($(this), cellName, "status", cellValue, "inactive", "rgb(181, 240, 250)");

})

function ColourRow (cellName_c, cellName_v, cellValue_c, cellValue_v, colour) {
// window.alert(cellName_c + cellName_v + cellValue_c + cellValue_v);
try {
if ( cellValue_c == cellValue_v && cellName_c.includes(cellName_v) == true ) {
if ( $(this).parent().attr("class").includes("Odd") == true ) {
$(this).parent().parent().css("background-color", colour);
} else {
$(this).parent().parent().css("background-color", shadeRGBColor(colour, "0.5"));
}
}
} catch(e) {}
}

function shadeRGBColor(color, percent) {
var f=color.split(","),t=percent<0?0:255,p=percent<0?p ercent*-1:percent,R=parseInt(f[0].slice(4)),G=parseInt(f[1]),B=parseInt(f[2]);
return "rgb("+(Math.round((t-R)*p)+R)+","+(Math.round((t-G)*p)+G)+","+(Math.round((t-B)*p)+B)+")";
}


</script>
';

I know this is digging up an old thread… but is there now a more simple way of changing the background color of a field on a form using a condition?

Hello,

How can I dynamically change the color of a total field of a Group By?

I’ve tried using macros sc_field_style and sc_field_color in onGroupBy events, but no success!
They work perfect for normal fields, but I can’t change the style of totals according to their values.

Thanks in advance!