Java Script Calculated and Read-Only Field

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

Java Script Calculated and Read-Only Field

Post by AEmpeno » 2018-08-21 03:24

Hello,

I need help or I'm looking for java script code for my my small project.

I do have a table named "LABEL" which has fields named - "BrokenJars", "LowVacuum", "Repack" and "Shrinkage"

1. I would like to make field "Shrinkage" as automated calculated field:
Shrinkage = BrokenJars + LowVacuum + Repack

2. Also, I would like to make the field Shrinkage as read-only field.

I will appreciate any suggestions or java script codes shared. Thanks in advance! :D

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Java Script Calculated and Read-Only Field

Post by pbottcher » 2018-08-21 07:15

Hi,

you can try to add this in the /hooks/<tablename>-dv.js


Code: Select all

(function(){
  $j(function () {
	$j('#Shrinkage').prop('readonly', true);
	var update_Shrinkage= function () {
		var v_BrokenJars = $j('#BrokenJars').val();
		var v_LowVacuum = $j('#LowVacuum').val();
		var v_Repack = $j('#Repack').val();
		var v_Shrinkage = v_BrokenJars+v_LowVacuum+v_Repack;
		$j('#Shrinkage').val(v_Shrinkage);
	} 
 
	$j('#BrokenJars' ).on('change', update_Shrinkage);
	$j('#LowVacuum' ).on('change', update_Shrinkage);
	$j('#Repack' ).on('change', update_Shrinkage);
  }
});
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

Re: Java Script Calculated and Read-Only Field

Post by AEmpeno » 2018-08-21 16:29

Thank you. I saved the code and refreshed the page but no changes happened.

Am I missing anything?

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Java Script Calculated and Read-Only Field

Post by pbottcher » 2018-08-21 17:08

Sorry there were some typos in the script.

Please use

Code: Select all

 $j(function () {
	$j('#Shrinkage').prop('readonly', true);
	var update_Shrinkage= function () {
		var v_BrokenJars = $j('#BrokenJars').val();
		var v_LowVacuum = $j('#LowVacuum').val();
		var v_Repack = $j('#Repack').val();
		var v_Shrinkage = v_BrokenJars*1+v_LowVacuum*1+v_Repack*1;
		$j('#Shrinkage').val(v_Shrinkage);
	} 
 
	$j('#BrokenJars' ).on('change', update_Shrinkage);
	$j('#LowVacuum' ).on('change', update_Shrinkage);
	$j('#Repack' ).on('change', update_Shrinkage);
  });
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

Re: Java Script Calculated and Read-Only Field

Post by AEmpeno » 2018-08-21 17:23

I'm still getting no changes. Below is my code. Please note that I added few fields and followed your code.

Also, I attached a screenshot of my label page.

$j(function () {
$j('#Shrinkage').prop('readonly', true);
var update_Shrinkage = function () {
var v_Repack = $j('#Repack').val();
var v_BrokenJars = $j('#BrokenJars').val();
var v_LowBrine = $j('#LowBrine').val();
var v_NoVacuum = $j('#NoVacuum').val();
var v_Shrinkage = v_Repack*1+v_BrokenJars*1+v_LowBrine*1+v_NoVacuum*1;
$j('#Shrinkage').val(v_Shrinkage);
}

$j('#Repack' ).on('change', update_Shrinkage);
$j('#BrokenJars' ).on('change', update_Shrinkage);
$j('#LowBrine' ).on('change', update_Shrinkage);
$j('#NoVacuum' ).on('change', update_Shrinkage);

});
Attachments
label1.png
label1.png (165.13 KiB) Viewed 2778 times

AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

Re: Java Script Calculated and Read-Only Field

Post by AEmpeno » 2018-08-21 17:54

Hello, Its working now! Thank you!

I do have another question. If I wanted to make "Labeled Cases" as calculated ("Labeled Jars" divided by "Pak") and make it as read-only field as well, how do I do that?

Thanks again.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Java Script Calculated and Read-Only Field

Post by pbottcher » 2018-08-21 19:40

just add

$j('#Labeled Cases').prop('readonly', true);

and in the function

var v_Labeled_Jars = $j('#Labeled Jars').val();
var v_Pak = $j('#Pak').val();
var v_Labeled_Cases=v_Labeled_Jars/v_Pak; //maybe you need to check if v_Pak is != 0 otherwise do something about it :-)
$j('#Labeled Cases').val(v_Labeled_Cases);
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply