Getting/setting field values in a subform from Javascript

Getting/setting field values in a subform from Javascript

Postby gib » Tue Aug 23, 2016 5:08 am

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
gib
 
Posts: 8
Joined: Fri Aug 19, 2016 4:58 am

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

Postby admin » Tue Aug 23, 2016 12:33 pm

gib,

e.PNG
e.PNG (12.64 KiB) Viewed 3533 times


And call bob() here..

c.PNG
c.PNG (22.82 KiB) Viewed 3533 times




Steven
admin
Site Admin
 
Posts: 3078
Joined: Mon Jun 15, 2009 9:53 am

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

Postby gib » Wed Sep 07, 2016 6:46 am

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: 8
Joined: Fri Aug 19, 2016 4:58 am

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

Postby gib » Mon Sep 12, 2016 11:40 pm

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.com/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
gib
 
Posts: 8
Joined: Fri Aug 19, 2016 4:58 am


Return to Custom Code



cron