React to lookup changes - No working

This sub-forum is for discussing all topics related to AppGini Helper JavaScript Library, provided by bizzworxx as a third-party AppGini plugin.
Post Reply
Vasil Vasilev
Posts: 6
Joined: 2017-08-26 15:39

React to lookup changes - No working

Post by Vasil Vasilev » 2021-06-17 22:52

Hi,
I have followed the example in the "React to lookup changes" section, but in my case only "fade in common fields" is executing. The other part of the code is not working. Can someone explain to me where I make a mistake? Also my programming skills are poor. I attached the .axp and .js files.

Thanks in advance!
Attachments
Test_DB.zip
(14.49 KiB) Downloaded 74 times

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: React to lookup changes - No working

Post by jsetzer » 2021-06-18 05:29

  • Did you clear browser cache when reloading?
  • Are there any error messages in console output?
Troubleshooting guide:
https://appgini.bizzworxx.de/support/troubleshooting/
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Vasil Vasilev
Posts: 6
Joined: 2017-08-26 15:39

Re: React to lookup changes - No working

Post by Vasil Vasilev » 2021-06-18 06:45

Sure, but it is not working. Maybe I don't understand the way the code is supposed to work. As you can see "Елемент" is with id =3. "combine_prt,type_of_module" should hide when this id is selected. In my case when nothing is selected everything is hidden, and when I press id 1 or id 2 only fields "stock, quantity, price" are visible.
Attachments
used_items2.png
used_items2.png (32.29 KiB) Viewed 8438 times
used_items.png
used_items.png (198.07 KiB) Viewed 8438 times

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: React to lookup changes - No working

Post by jsetzer » 2021-06-21 10:16

I strongly recommend adding more console output commands (console.log("..."); ) for printing out the variable values you get using .getValue() function and also for printing out the values of your option_x_is_selected variables. As soon as you can see the variable values, you will see if your function gets executed at all and if your conditions really evaluate what you expect.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: React to lookup changes - No working

Post by jsetzer » 2021-06-21 10:27

I don't know your specific use case and your expectation of which fields to hide and which fields to show under which conditions.

But I have seen you are using both, "fade" and "hide" function. Keep in mind that hide has opposite function than fade/show:
  • fade(true) shows the field
    same as fadeIn() without parameters
  • fade(false) hides the field
    same as fadeOut() without parameters
...and...
  • hide(true) hides the field
    same as hide() without parameters
  • hide(false) shows the field
...and...
  • show(true) shows the field
    same as show() without parameters
  • show(false) hides the field
Maybe the problem is just the understanding of the usage of fade/show and hide.

If, for example...

Code: Select all

var condition1 = true;
...then...

Code: Select all

dv.getField("a").fade(condition1);
dv.getField("b").hide(condition1);
...will show the field "a" but hide the field "b"
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Vasil Vasilev
Posts: 6
Joined: 2017-08-26 15:39

Re: React to lookup changes - No working

Post by Vasil Vasilev » 2021-06-21 18:58

Ok thanks for the examples, I did not understood how "hide, fade" worked.
Can you explain to me how I can create the following logic:
I would like to create a cascade show/hide fields depending on the value (id) of the drop-down menu.
For example if you have a warehouse where you store a lot of different things categorized like so ("Modules, measuring equipment, elements"). In my case I created a table where I can use it in a couple of places. Imagine you repaired something but you used different types of components ("Capacitors, Resistors etc."). Depending on the choice different fields need to show and others to hide.
Example: If you pick from "Type" -> (Elements) only one field needs to show "Type of element". If you pick "Capacitors" only "combine_cap" need to show. The fields that need to be constantly shown are "Stock, Quantity, Price".

I have tried the logic described in "React to lookup changes" but I could not figure it out.
I need to apologies about my bad programming skills and it will be awesome if you can show me a example of such logic.
Thanks in advance!

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: React to lookup changes - No working

Post by jsetzer » 2021-06-22 07:15

Sorry, I cannot do the job, but I can send a few examples* for better understanding.

Example (1)

Show field "a" only if type.id equals 1

Code: Select all

var type_field = dv.getField("type");
var type_id = type_field.getValue().id;

if (type_id == 1) {
  dv.getField("a").show();
} else {
 dv.getField("a").hide();
}
Shorter version

Code: Select all

var type_id = dv.getField("type").getValue().id;
dv.getField("a").show(type_id == 1);
---
Example (2)

Show field "a" if type.id equals 1
Show field "b" if (type.id equals 1) AND ((category.id equals 12) OR (category.id equals 14))

Code: Select all

var type_field = dv.getField("type");
var type_id = type_field.getValue().id;

var category_field = dv.getField("category");
var category_id = category_field.getValue().id;

var a_field = dv.getField("a");
var b_field = dv.getField("b");


if (type_id == 1) {

  a_field.show();
  
  if (category_id == 12) {
    b_field.show();
  } else if (category_id == 14) {
    b_field.show();
  } else {
    b_field.hide();
  }
} else if (type_id==2) {
  b_field.show();
} else {
  a_field.hide();
  b_field.hide();
}

Shorter version

Code: Select all

var type_id = dv.getField("type").getValue().id;
var category_id = dv.getField("category").getValue().id;

// evaluate conditions
// I have added unnecessary brackets for better readability

var show_field_a = (type_id == 1);
var show_field_b = (type_id == 1) && ( (category_id == 12) || (category_id == 14) );

// show fields "a", "b" under certain conditions:
dv.getField("a").show(show_field_a);
dv.getField("b").show(show_field_b);

Debugging

I recommend adding as many console.log("..."); commands as necessary during development. This allows you to check the content of your variables if something does not work as expected, for example:

Code: Select all


var type_id = dv.getField("type").getValue().id;
var category_id = dv.getField("category").getValue().id;

console.log("type_id = " + type_id);
console.log("category_id = " + category_id);

// note: getValue() function for lookup fields returns not just the id, but an ...
// object which contains both: id and text
var type_value = dv.getField("type").getValue();
console.log("type:");
console.log(type_value);
console.log("type.id = " + type_value.id);
console.log("type.text = " + type_value.text);

Notes about conditions

In javascript (and many other programming languages)...
  • "&&" means "AND"
  • "||" means "OR"
  • "==" means "EQUALS"
Don't mix up "==" (equals operator) and "=" (variable assignment)

Code: Select all

var a = 1; // assignment
// declare variable "a", assign value 1 to variable "a"

var b = (a == 1); // declare variable "b", evaluate condition, then assign result to variable "b"
// first, evaluate expression ("if a equals 1"). This gives true. Then assign the result (true) to variable "b"

console.log(a);
console.log(b);

// console output:
// 1
// true
More on conditions More on debugging * I did not create a test project, but just wrote down the code. So, there may be typos.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Vasil Vasilev
Posts: 6
Joined: 2017-08-26 15:39

Re: React to lookup changes - No working

Post by Vasil Vasilev » 2021-06-24 16:13

Hi,
Thanks for the examples, I learned a lot from them. Now I have the following problem: According to the console "getValue().id" has no value. Here I attach a picture from the console and the code.

//code begin
var dv = AppGiniHelper.DV;
dv.ready(onReady);
function onReady() {
type_init();
}
function type_init() {
var type_field = dv.getField("type");
var type_id = type_field.getValue().id;
var currentvalue = type_id;
hideOrShowFields(currentvalue);
field.onChange(onTypeIdChanged);

}
function onTypeIdChanged(value) {
hideOrShowFields(value);
}

function hideOrShowFields(value) {

if (type_id == 1) {
dv.getField("type_of_module").show();
} else {
dv.getField("type_of_module").hide();
}
console.log("type_id = " + type_id);

}


//code end

Best regards!
Attachments
Console.png
Console.png (153.5 KiB) Viewed 8338 times

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: React to lookup changes - No working

Post by jsetzer » 2021-06-25 06:23

Hi,

have a look at the error message which shows the reason.

If there is no selection, .getValue()-function can not return an object having properties id and text but getValue() returns null instead.

You have to check if the return value equals null. If it equals null, there cannot be an .id nor .text property.

Long version

Code: Select all

var type_field = dv.getField("type");
var type_value = type_field.getValue();
if (type_value != null) {
  var type_id = type_value.id;
  // 
} else {
  // whatever you want to do if type is empty
}
Shorter version

Code: Select all

var type_value = dv.getField("type").getValue();
var type_id = type_value != null ? type_value.id : 0;
// type_id: 
// 0 if there is no selection
// n if there is a selection (n: primary key of selected lookup entry)
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply