Calculated field that change value of other field

Rox

New member
Hi,
I have a form with the following fields:
Field "Co-owner", a select_single with values Yes and No
Field "Name", a text field
Field "Address", a text field
Field "Calculated Co-owner", a text field with calculated function.

I want that if the user set Co-owner to "No", fields Name and Address assume the value "N/A", while Calculated Co-owner assume the value "The co-owner is XXX, operating at YYY".

I've created a calculated function for the field Calculated Co-owner as the following:

function dadabik_coowner_calculated($parameters_ar){
if ( $parameters_ar['Co-owner'] == 'No'){
return ("There is no co-owner");
}
else{
return ("The co-owner is ".$parameters_ar['Name'].", operating at ".$parameters_ar['Address']);
}
}

It works, but I don't know how to assign the value "N/A" to fields Name and Address if Co-owner is set to "No". If I create other calculated functions for fields Name and Address, they won't be more editable when I choose Yes in Co-owner field... Any suggestions?
Thanks,

Ale




My system info are:
You are using DaDaBIK version 9.0-Monterosso enterprise, installed on 16-08-2018 (installation code: 0), the latest version of DaDaBIK is 9.0-Monterosso released on 05-07-2018

You are runnning the last release of DaDaBIK

PHP Version: 7.2.7

mysql version: 5.7.21

Web server: Apache/2.2.34 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.2.7 mod_ssl/2.2.34 OpenSSL/1.0.2o DAV/2 mod_fastcgi/mod_fastcgi-SNAP-0910052141 mod_perl/2.0.9 Perl/v5.24.0

Client: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
 

eugenio

Administrator
Staff member
Hi,
I don't you can handle this with calculated fields. There are two problems about having name and address as calculated fields as well:
1) You cannot use, as a source of a calculated fields, other calculated fields
2) you cannot change "on the fly" the status of a field (when field x is set to "N", field y is calculated otherwise is not calculated).

Possible solutions:

1) Leave name and function blank (instead of N/A) and live with the fact that a user can fill those fields even if co-owner is NO

2) Try with adding (in addition of your function) a javascript function (onchange event on the co-owner field) that set name and address to N/A if co-owner is no; however, I would check carefully if everything is working and there isn't any conflict with the function you already wrote

Best,
 

FRW

Well-known member
Could you give a hint for option 2?

How do I have to write the javascript to change the value of another field?

My suggestion is to do this together with some styles coming from the onchange event like:

[pre]
function dadabik_Status(field){
switch(field.value){
case 'Aktiv':
$('input[name="wirkbetrieb"]').css('visibility', 'visible');
$('label[for="wirkbetrieb"]').css('visibility', 'visible');
$('select[name=stat_grund]').css('visibility', 'hidden');
$('label[for="stat_grund"]').css('visibility', 'hidden');
$('input[name="stat_datum"]').css('visibility', 'hidden');
$('label[for="stat_datum"]').css('visibility', 'hidden');
HERE TO CHANGE THE VALUE OF FIELD "datum_start"...
break;
...
}}
[/pre]
 

FRW

Well-known member
Reading is helpful!

I missed the number of array-element:
[pre]
$('[name="datum_start"]')[0].value = '';
[/pre]

will do it!
 

FRW

Well-known member
Is it possible to keep the value of the field?

  1. selection Aktiv => datum_start = 1.1.2019
  2. selection change => datum_start = ''
  3. selection back to Aktiv => fill in the old value (1.1.2019)

At the moment I have tried it with
[pre]
var name_bak = $('[name="datum_start"]')[0].value;
[/pre]

but this gives me the current value ('') of the field (what I can understand, but I don't have a solution)
 

eugenio

Administrator
Staff member
Hello,
I didn't get all the details but about your last question, you should store the value somewhere, you cannot retrieve the old value.

Best,
 

FRW

Well-known member
The question is:

I want to buffer the original value. So I need a way to store this value, while the input form is "in process".
I thought about global variables (because I had done some VBA-Coding) but it seems that in Javascript the variable I set, can only store the actual value, every time the onchange_event is triggered.

This seems quiet logical....

Where can I store the value to maybe retrieve it later before saving the input-form?
 

eugenio

Administrator
Staff member
You can do that in javascript if you declare a variable as global, OUTSIDE of the function you are writing.
For example here I increment a every time I call test()

[pre]
<script>

var a = 1;

function test(){
a = a +1;
alert(a);
}

</script>
[/pre]

Not sure if it will be easy to implement this in your dadabik app but you can probably add the declaration of the global var in the header.php file with something like

[pre]
<script>

var old_value = '';

</script>
[/pre]

Best,
 

FRW

Well-known member
Thanks a lot, part 1 I had done, but I haved missed the second part within the header.php (this make sense, indeed ;) )
 
Top