Autofill a field with data from the database based on 3 other fields without submit

57 views
Skip to first unread message

Oasis Agano

unread,
Mar 26, 2017, 2:01:38 PM3/26/17
to web2py-users
Greetings

I have a form built with sqlfactory
form = SQLFORM.factory(
Field('meter', requires=IS_NOT_EMPTY()),
Field('date_from', 'datetime'),
Field('date_to', 'datetime'),
Field('amt', 'float'))


then i want to have a form in the view where users will access the 3 first fields in normal mode and the amt one in Readonly,
After filling those 3 i use Ajax(Where the problem is) to load a function that has a query in it to calculate the amount, then i fill the output of that function to the last field(amount).

The problem i have is how to call that ajax method to launch the query and auto fill the field when those 3 fields are filled.

Regards,
Oasis

Dave S

unread,
Mar 26, 2017, 3:17:20 PM3/26/17
to web2py-users


On Sunday, March 26, 2017 at 11:01:38 AM UTC-7, Oasis Agano wrote:
Greetings

I have a form built with sqlfactory
form = SQLFORM.factory(
Field('meter', requires=IS_NOT_EMPTY()),
Field('date_from', 'datetime'),
Field('date_to', 'datetime'),
Field('amt', 'float'))



I would be inclined to just use an event handler (onchange, oninput, or onfocus) to detect when the fields have been filled in, do a jquery call to your controller, and refresh your div.  The other approach may be to calculate amt in the javascript, which may involve having a hidden field in your form for the usage-price rate.

/dps

Oasis Agano

unread,
Apr 3, 2017, 7:51:48 AM4/3/17
to web2py-users
I used event handler as you said now the issue i have is to set the  value of amt to the one returned by the function.
I have a variable holding it in the calculation function but i want to fill it as value for the amt field in the form factory in the view
Reply all
Reply to author
Forward
0 new messages