Dynamic IF Default Values

Hello,

I am trying to make a form where the user can update information that will be logged in a separate SQL query. To avoid nulls or additional conditional statements in my SQL query, I am trying to set the default value of a number input (borrower_committed_capital) to the above selected company (select3). If the user is trying to add a company (select2), I want the default to be 0. The problem I am having is trying to match the text identifier to the array pulled by a query listing all borrowers and amount desired numbers.

Below is what I have so far:

{{
 var selectVal = select3.value;
 var indexedVal = list_active_borrowers.data.borrower_id.indexOf(selectVal);
 
 if (select2.value == 'edit') {
   borrower_committed_capital.value = list_active_borrowers.data.borrower_committed[indexedVal]
 ;}

 else {
   0.00
 }
}} 

You should be able to do this within the fields themselves using a ternary operator

Any screenshots?

Hi Scott,

i'm not quite sure how you mean. I did try and use ternary to select the value, but I am not sure how to do that without passing the index to get as a variable.

{{select2.value === 'edit'? borrower_committed_capital.value = list_active_borrowers.data.borrower_committed[select3.selectedIndex]:0.00}}

That seemed to work. But now there is a dependency cycle with my insert vs update queries, leaving the default value at zero regardless of input.

I think because you cannot put that in the field itself...
What you could do is for the select2 field where a user selects "edit", you can run an event handler on that field instead using....

{{self.value === 'edit'? borrower_committed_capital.value = list_active_borrowers.data.borrower_committed[select3.selectedIndex]:0.00}}

One note - a little difficult for me to give more specific thoughts as I am not building your app and am only seeing parts of it....so I can't promise my ideas will work! :slight_smile:

1 Like

Thanks Scott for the ideas! I will play around a bit more with them and see if I can work it out.

:blush:

1 Like

Agreed with Scott :slightly_smiling_face:

Code inside {{}} needs to be self-fulfilling one liner code. If/else statements should generally be replaced with ternaries

Let us know if you run into any further issues!