Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

Getting/setting field values in a subform from Javascript

Post Reply
gib
Posts: 11
Joined: Thu Aug 18, 2016 9:28 pm

Getting/setting field values in a subform from Javascript

Unread post by gib »

Hello:

I am new to NuBuilder, but so far have been very impressed with what I've been able to put together in a short space of time.

Now, I am on to updating fields on the screen in Javascript, and am in need of examples of code that run from form events would do that. Specifically, I have a subform of charges and payments. The fields of the related table are 'date', 'type' (charge or payment), and 'amount'. I want to write Javascript for an onchange event in the 'amount' field such that if the 'type' is 'payment', the amount will be negative. But I can't figure out how to either retrieve or set the field values. I tried this Javascript code based upon examples in the documentation, to no avail:

Code: Select all

if( $('#type').val() == 'Charge' ){
  var Amt = Math.abs($('#amount').val());
}
else
{
  var Amt = Math.abs($('#amount').val())*-1;
}
$('#amount').val(Amt);
I am (obviously) very new to Javascript, so advice is very much appreciated!

Thanks,

Gib
admin
Site Admin
Posts: 2778
Joined: Mon Jun 15, 2009 2:23 am
nuBuilder Version: 4.5

Re: Getting/setting field values in a subform from Javascrip

Unread post by admin »

gib,
e.PNG
e.PNG (12.64 KiB) Viewed 8390 times
And call bob() here..
c.PNG
c.PNG (22.82 KiB) Viewed 8390 times


Steven
gib
Posts: 11
Joined: Thu Aug 18, 2016 9:28 pm

Re: Getting/setting field values in a subform from Javascrip

Unread post by gib »

Hi, Steven:

Thanks for your response. I didn't see this for a while because I thought I'd be notified by email of responses. I just figured out how to update my forum settings for future replies.

Unfortunately, your suggestion did not work for me. Although I can call the form function from an onchange event in a subform field, the function can not read the value of other fields in the record in the subform. (Upon experimentation, I see that it can read the values from fields in the main form.)

Your example appears to show defining an event at the subform level. However, the nuBuilder documentation says that such events are attached to the delete box on the subform record only. After trying that, I can verify the documentation is correct.

So, although I found my syntax was correct for reading values from the main form, my issue still is how to get/set values from a subform in custom Javascript code. Any other thoughts would be appreciated.

Thanks again,

Gib
gib
Posts: 11
Joined: Thu Aug 18, 2016 9:28 pm

Re: Getting/setting field values in a subform from Javascrip

Unread post by gib »

Update:

As a workaround, I created a form for the dues table instead of relying on a subform. In that case, the Javascript code in my initial post above worked fine for setting the proper sign for the Amount field (with the code as a defined function in the form Custom Code, and the call in the object events, as Steven suggested).

Unfortunately, I ran into a different problem where the values for lastName and firstName columns joined from a one table could not be displayed in objects on the form. I documented that as a separate post at http://forums.nubuilder.cloud/viewtopic.php?f=13&t=9026.

Would be nice to solve one of these two issues so I'd have at least one wholly functioning solution.

Thanks,

Gib
Post Reply