Hide/Show one checkbox field if another checkbox field is ch

Posted by Paulh0763 on 29-Feb-2016 10:59

Version 2.2.2.0 (old)

So I have this simple script...well I thought it was because it is not working. I want to show/hide a checkbox field based on whether or not another checkbox field is checked...I have used this type of script with other fields mostly picklist using the same concept for rbf_getPicklistCode and it works fine...so this one has me stumped for some reason???

<script>
document.getElementById("add_to_general_ledger").onchange = type1;
function type1(){
var type1 = rbf_getFieldValue("add_to_general_ledger");

if(type1 != 'true')

hideField();

$('#rbi_F_gli').hide();
$('#rbi_L_gli').hide();

}else if(type1 == 'true'){

hideField();

$('#rbi_F_gli').show();
$('#rbi_L_gli').show();
}

}
function hideField()
{
$('#rbi_F_gli').show();
$('#rbi_L_gli').show();
}
</script>

I have on the onload of the edit page =  $('#rbi_F_gli').hide(); $('#rbi_L_gli').hide(); and on the onload of the view page and the event handler on the field set with onchange = type1();

No need to have this on the New page.

Thanks for any input in advance :)

Paul

All Replies

Posted by jsniemi79 on 29-Feb-2016 18:31

Hi Paul. It's been a while since I've looked at 2.2.2 and wasn't doing much script work then, but there are other APIs that are available in the current version to handle this.  I don't know if they were there in 2.2.2, but it is worth  checking. Instead of using rbf_getFieldValue, there is an api called rbf_isChecked that will return true or false for boolean fields.  There is another one called rbf_showOrHideField that will handle the visibility of both the label and input.

Something like this would be how I would write it in the current version.

<script>

document.getElementById("add_to_general_ledger").onchange = type1;

function type1(){

var type1 = rbf_isChecked("add_to_general_ledger");

 if (type1 === true)

   {

     hidefield(false);

   }

 else

   {

     hidefield(true);

   }

}

function hideField(showField)

{

 rbf_showOrHideField("gli", showField)

}

</script>

If those aren't available, I would try logging the value of your type1 to the console (console.log(type1);).  That will show you the value that is being passed on the rbf_getFieldValue api. It might be passing 0 or 1 instead of true/false.  Also, just a tip, but when you compare values against true/false, you should use !== and === instead of != and ==.

I hope this helps.

Jason

Posted by Paulh0763 on 01-Mar-2016 08:37

Hi Jason,

Thanks for the direction...I got it to work with this script (see below) using the 'rbf_isChecked' however, on the view page of the record the checkbox field that is hidden does not show when Add To General Ledger is checked??? Is this a bug?

I have type1(); on the onload of the Edit and View pages, and on the field level for the onChange event handler.

<script>

document.getElementById("add_to_general_ledger").onchange = type1;

function type1(){

var type1 = rbf_isChecked("add_to_general_ledger");

if (type1 === true)

  {

    hideField();

    $('#rbi_F_gli').show();

    $('#rbi_L_gli').show();          

}

else if (type1 === false)

  {

    hideField();

    $('#rbi_F_gli').hide();

    $('#rbi_L_gli').hide();

 }

}

function hideField()

{

    $('#rbi_F_gli').show();

    $('#rbi_L_gli').show();

}

</script>

Posted by jsniemi79 on 01-Mar-2016 08:42

If you are using the inline editing on the view page, it might not be firing the event or hitting the script on the page. I don't know if that is a bug or not.  Someone from Progress would need to confirm that.

Instead of using the script to get the change event, could you add a call to the type1 function on the onchange event of the field itself?  You could then pass the current value to type1 and get the same effect.  I don't know if that would help in this current situation or not as it might require a page refresh if you are using the inline editing.

Posted by Paulh0763 on 01-Mar-2016 08:51

I have inline editing off.

Posted by jsniemi79 on 01-Mar-2016 09:04

So the onload event isn't recognizing the value and displaying the field correctly? If that is the case, you should try updating the hiding/showing of the field in another function like my example above.  Instead of calling type1(); on the onload event of the view page, call the hide/display function and pass in the true/false value based on the add_to_general_ledger field in the onload event. Then update your client side script to have the type1 function call the new display/hide function so the edit page works as expected.  I'm guessing it isn't getting into the type1 function inside your onchange event since the value on the view page isn't "changed".  Make sense?

Posted by Paulh0763 on 01-Mar-2016 09:07

Yes that does make sense...okay I will give that a go.

Thanks.

Posted by Paulh0763 on 01-Mar-2016 10:44

Hello Jason,

I may be a bit lost here...cannot wrap my head around it. I know some scripting but not a lot. Could you provide an example for me...thanks,

Paul

Posted by jsniemi79 on 01-Mar-2016 11:01

Sure.  Using the example from above, update the script on your view page to something like below.

<script>

document.getElementById("add_to_general_ledger").onchange = type1;

function type1(){

var type1 = rbf_isChecked("add_to_general_ledger");

if (type1 === true)

  {

    hidefield(false);

  }

else

  {

    hidefield(true);

  }

}

function hideField(showField)

{

rbf_showOrHideField("gli", showField)

}

</script>

Then in your onload event of the view page, change your script to below.

if (rbf_isChecked("add_to_general_ledger") === true)

 {

   hideField(false);

 }

else

 {

   hideField(true);

 }

I haven't run through this exact scenario, but hopefully that will work for you.

This thread is closed