docassemble
docassemble copied to clipboard
Calculate button?
Is there a way to make a button that doesn't advance you to the next screen, but will run code on the current screen?
Price: price Partial Payment: payment
Calculate Button: price - partial payment= balance
Balance: balance
I've tried to figure out a way to use the built in functions, and could not figure out a way to make a button that would run the code, but not go to the next question. I did cobbled something together with "check in:" but sometimes does not work reliably.
Sounds like a task for JavaScript.
mandatory: True
question: |
Tell me stuff
fields:
- Price: price
datatype: currency
- Partial Payment: payment
datatype: currency
- note: |
${ action_button_html('javascript:calculateIt()', label="Calculate", color="primary", size="sm") }
- Balance: balance
datatype: currency
script: |
<script>
function calculateIt(){
if (val('price') !== undefined && val('payment') !== undefined){
$(getField('balance')).val(parseFloat(val('price')) - parseFloat(val('payment')));
}
}
</script>
You could skip the button and add event listeners:
mandatory: True
question: |
Tell me stuff
fields:
- Price: price
datatype: currency
- Partial Payment: payment
datatype: currency
- Balance: balance
datatype: currency
script: |
<script>
$(getField('balance')).attr('readonly', true);
function calculateIt(){
if (val('price') !== "" && val('payment') !== ""){
$(getField('balance')).val(parseFloat(val('price')) - parseFloat(val('payment')));
}
}
$(getField('price')).change(calculateIt);
$(getField('payment')).change(calculateIt);
</script>
Thank you. The first solution got me fixed up.
I tested the second solution, but I it doesn't seem to update with what I hope to accomplish.
First attempt with check in
mandatory: true
question: Calculate Loan
fields:
- Purchase Price: purchase_price
datatype: currency
- Down Payment Type: input_down_payment
input type: radio
choices:
- Fixed Dollar Amount
- Percentage
- Down Payment Percentage: percent_down_payment
datatype: number
default: 0
min: 0
max: 100
show if:
variable: input_down_payment
is: Percentage
- Down Payment Amount: fixed_down_payment
datatype: currency
default: 0
show if:
variable: input_down_payment
is: Fixed Dollar Amount
- Loan Amount: loan_amount
datatype: currency
check in: calculate_loan
---
event: calculate_loan
code: |
if action_argument('purchase_price') != '':
if action_argument('fixed_down_payment') != '' and action_argument('input_down_payment') == 'Fixed Dollar Amount':
answer = int(action_argument('purchase_price')) - int(action_argument('fixed_down_payment'))
elif action_argument('percent_down_payment') != '' and action_argument('input_down_payment') == 'Percentage':
downpayment = (int(action_argument('purchase_price')) * int(action_argument('percent_down_payment')) * 0.01)
answer = int(action_argument('purchase_price')) - downpayment
background_response({'loan_amount': answer}, 'fields')
background_response()
Second attempt with button (most reliable):
mandatory: true
question: Calculate Loan
fields:
- Purchase Price: purchase_price
datatype: currency
- Down Payment Type: input_down_payment
input type: radio
choices:
- Fixed Dollar Amount
- Percentage
- Down Payment Percentage: percent_down_payment
datatype: number
default: 0
min: 0
max: 100
show if:
variable: input_down_payment
is: Percentage
- Down Payment Amount: fixed_down_payment
datatype: currency
default: 0
show if:
variable: input_down_payment
is: Fixed Dollar Amount
- Loan Amount: loan_amount
datatype: currency
- note: |
${ action_button_html('javascript:calculate_loan()', label="Calculate Loan Amount", color="secondary", size="md", icon="calculator", block="true") }
- note: |
**Dates:**
script: |
<script>
function calculate_loan(){
if (val('purchase_price') !== undefined){
if (val('fixed_down_payment') !== undefined && val('input_down_payment') == 'Fixed Dollar Amount'){
$(getField('loan_amount')).val(parseFloat(val('purchase_price')) - parseFloat(val('fixed_down_payment')));
} else if (val('percent_down_payment') !== undefined && val('input_down_payment') == 'Percentage'){
$(getField('loan_amount')).val(parseFloat(val('purchase_price') - parseFloat(val('purchase_price')) * parseFloat(val('percent_down_payment') * 0.01)))
}
}
}
</script>
Third attempt listeners. Only works with I click the radio buttons and go back and forth.
mandatory: true
question: Calculate Loan
fields:
- Purchase Price: purchase_price
datatype: currency
- Down Payment Type: input_down_payment
input type: radio
choices:
- Fixed Dollar Amount
- Percentage
- Down Payment Percentage: percent_down_payment
datatype: number
default: 0
min: 0
max: 100
show if:
variable: input_down_payment
is: Percentage
- Down Payment Amount: fixed_down_payment
datatype: currency
default: 0
show if:
variable: input_down_payment
is: Fixed Dollar Amount
- Loan Amount: loan_amount
datatype: currency
script: |
<script>
$(getField('loan_amount')).attr('readonly', true);
function calculate_loan(){
if (val('purchase_price') !== ""){
if (val('fixed_down_payment') !== "" && val('input_down_payment') == 'Fixed Dollar Amount'){
$(getField('loan_amount')).val(parseFloat(val('purchase_price')) - parseFloat(val('fixed_down_payment')));
} if (val('percent_down_payment') !== "" && val('input_down_payment') == 'Percentage'){
$(getField('loan_amount')).val(parseFloat(val('purchase_price') - parseFloat(val('purchase_price')) * parseFloat(val('percent_down_payment') * 0.01)))
}
}
}
$(getField('purchase_price')).change(calculate_loan);
$(getField('percent_down_payment')).change(calculate_loan);
$(getField('fixed_down_payment')).change(calculate_loan);
$(getField('input_down_payment')).change(calculate_loan);
</script>
I'm going back through old issues and I realized I didn't respond to your last point. I think you can get around this by calling calculate_loan() when the screen loads.
$(document).on('daPageLoad', function(){
calculate_loan();
});