• Resolved svd89

    (@svd89)


    Hello CodePeople,

    I was wondering if it’s possible to calculate with choice texts. For example:

    When in fieldname1 ‘A1’ is selected, fieldname2 will show 100,00 (with 2 decimals) and when ‘A2’ is selected, fieldname2 will show 200,00.

    Hope to hear from you. Many thanks in advance!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    Yes, of course, you can use conditional statements in the equation.

    You can insert a calculated field in the form and enter the equation:


    (function(){
    if(fieldname1|r == 'A1') return PREC(100,2);
    if(fieldname1|r == 'A2') return PREC(200,2);
    })()

    Best regards.

    Thread Starter svd89

    (@svd89)

    This is great! Thank you so much.
    Unfortunately I’m not able to get it working. I have adjusted it to my form’s needs (more lines to come). Did I do something wrong? This is the equation below:

    (function(){
    if(fieldname104|r == ‘A1 Autotransporter’) return PREC(250,2);
    if(fieldname104|r == ‘A2 Autotransporter (luchtgeveerd)’) return PREC(250,2);
    if(fieldname104|r == ‘B1 Bagagewagen’) return PREC(100,2);
    })()

    Many thanks in advance!

    • This reply was modified 3 months, 1 week ago by svd89.
    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    You want to access the choices texts, not the choices values, so, the code would be:

    (function(){
    if(fieldname104|v == 'A1 Autotransporter') return PREC(250,2);
    if(fieldname104|v == 'A2 Autotransporter (luchtgeveerd)') return PREC(250,2);
    if(fieldname104|v == 'B1 Bagagewagen') return PREC(100,2);
    })()

    Please pay attention to the |v modifier, and the use of single quotes (')

    Best regards.

    Thread Starter svd89

    (@svd89)

    Yes, the choice texts it is! Thank you very much. I’ve learned a lot.
    All the best and have a great day!

    Thread Starter svd89

    (@svd89)

    Hello,

    Is it also possible to return a certain text instead of PREC(250,2); ? For example:

    (function(){
    if(fieldname104|v == 'A1 Autotransporter') return (This is a test);
    if(fieldname104|v == 'A2 Autotransporter (luchtgeveerd)') return (another test);
    if(fieldname104|v == 'B1 Bagagewagen') return (final test);
    })()

    But this isn’t working. What am I doing wrong? Hope to hear from you.
    Thank you in advance.

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    Yes, you can. However, in Javsacript the plain texts must be enclosed between single or double quotes. So, the correct code would be:

    (function(){
    if(fieldname104|v == 'A1 Autotransporter') return 'This is a test';
    if(fieldname104|v == 'A2 Autotransporter (luchtgeveerd)') return 'another test';
    if(fieldname104|v == 'B1 Bagagewagen') return 'final test';
    })()

    Best regards.

    Thread Starter svd89

    (@svd89)

    Thank you so much, it works! But I lose my ‘style.’ Is it possible to combine or integrate the code with html or css?
    For example:

    (function(){
    if(fieldname104|v == 'A1 Autotransporter') return '<b>This is a test</b>';
    if(fieldname104|v == 'A2 Autotransporter (luchtgeveerd)') return '<p>another <u>test</u></p>';
    if(fieldname104|v == 'B1 Bagagewagen') return '<h4>final test</h4>';
    })()
    • This reply was modified 3 months ago by svd89.
    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    The calculated fields display the results in input tags, and in the HTML standard input tags do not render other HTML tags. So, you must use the calculated field as an auxiliary to determine the result, but display it in another field.

    Assuming your calculated field is the fieldname123, insert an “HTML Content” field in the form and enter a tag in its content with the data-cff-field attribute telling the plugin the field whose value you want to display in the tag. E.g.

    <span data-cff-field="fieldname123"></span>

    Since the calculated field is used as an auxiliary you can hide it by ticking a checkbox in its settings.

    Best regards.

    Thread Starter svd89

    (@svd89)

    Yes, this is great and it works, thank you! But how am I able to change a part of the text to bold instead of the whole thing? Or to put some text in a <table> or so? Is that possible?

    All the best

    Plugin Author CodePeople2

    (@codepeople2)

    Hello @svd89

    You only need to return the equation result with the tags and the styles you prefer, just like you did in your equation. E.g.

    (function(){
    if(fieldname104|v == 'A1 Autotransporter') return '<b>This is a <span style="color:red;">test</span></b>';
    if(fieldname104|v == 'A2 Autotransporter (luchtgeveerd)') return '<p>another <u>test</u></p>';
    if(fieldname104|v == 'B1 Bagagewagen') return '<h4>final test</h4>';
    })()

    The result will be rendered as HTML in the HTML Content field where you inserted the tag with the data-cff-field attribute.

    I recorded a video emulating your form, and the different variants to teach you the result:

    https://resources.developers4web.com/cff/tmp/2024/12/21/video-form_o.mp4

    Best regards.

    Thread Starter svd89

    (@svd89)

    Hello @codepeople2 ,

    This is amazing, thank you so much for taking the time to create a video.
    I really appreciate it. I got it working now!

    All the best

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.