Help with label and javascript

Of course now I show you everything because I am very confused…

This is the code of editviewdefs.php:
<?php
$module_name = ‘PTN_Quotazioni’;
$viewdefs [$module_name] =
array (
‘EditView’ =>
array (
‘templateMeta’ =>
array (
‘maxColumns’ => ‘2’,
‘widths’ =>
array (
0 =>
array (
‘label’ => ‘10’,
‘field’ => ‘30’,
),
1 =>
array (
‘label’ => ‘10’,
‘field’ => ‘30’,
),
),
‘includes’ =>
array (
0 =>
array (
‘file’ => ‘/custom/modules/PTN_Quotazioni/JS/Quotazioni.js’,
),
),
‘useTabs’ => false,
‘tabDefs’ =>
array (
‘DEFAULT’ =>
array (
‘newTab’ => false,
‘panelDefault’ => ‘expanded’,
),
),
),
‘panels’ =>
array (
‘default’ =>
array (
0 =>
array (
0 =>
array (
‘name’ => ‘date_entered’,
‘comment’ => ‘Date record created’,
‘label’ => ‘LBL_DATE_ENTERED’,
),
1 =>
array (
‘name’ => ‘yard_c’,
‘label’ => ‘LBL_YARD’,
),
),
1 =>
array (
0 => ‘assigned_user_name’,
1 => ‘’,
),
2 =>
array (
0 =>
array (
‘name’ => ‘n_pezzi_c’,
‘label’ => ‘LBL_N_PEZZI’,
),
1 =>
array (
‘name’ => ‘tempo_ciclo_c’,
‘label’ => ‘LBL_TEMPO_CICLO’,
),
),
3 =>
array (
0 =>
array (
‘name’ => ‘ore_turno_c’,
‘label’ => ‘LBL_ORE_TURNO’,
),
),
),
),
),
);
;
?>

And this is the code of Quotazioni.js:

<html>

<form action="/action_page.php">

  <label for="n_pezzi_c">First val:</label>

  <input type="text" id="n_pezzi_c" name="abc"><br></br></input>

  <label for="tempo_ciclo_c">second val:</label>

  <input type="text" id="tempo_ciclo_c" name="def"><br></br></input>

  <label for="ore_turno_c">output:</label>

  <input type="text" id="ore_turno_c" name="efg"><br></br></input>

</form>

</html>

/*function CalcoloOre() {

        var modLabel = Sugar.language.get('PTN_Quotazioni', 'LBL_N_PEZZI');

        var modLabel = Sugar.language.get('PTN_Quotazioni', 'LBL_TEMPO_CICLO');

        var modLabel = Sugar.language.get('PTN_Quotazioni', 'LBL_ORE_TURNO');

        var $modLabel = LBL_N_PEZZI = document.getElementsById('LBL_N_PEZZI').value;

        LBL_TEMPO_CICLO = document.getElementById('LBL_TEMPO_CICLO').value;

    }*/

    $(document).on('change','#f1_input, #f2_input',function(){

      $('#ore_turno_c').val($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val());

    });

I’m sorry but I’m really confused and I can’t figure out how to do it …
Please help me

your js file contains way too much :wink: there should be only js in it.

try to reduce your js to

 $(document).on('change','#f1_input, #f2_input',function(){

      $('#ore_turno_c').val($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val());

    });

But: the first line is copied from my example, you need to add those listeners to the fields that should actually trigger the function.

So I should add some listeners that start the function when I enter the data, did I understand correctly?
thank you so much for all the help you are giving me!

Hi,
exactly. The function should be triggered for specific events, those are defined in your listener. currently, the function would be triggered if you enter something into one of the fields that have the ids “f1_input” or “f2_input”, but those were examples from my form. So change those ids too.

E: here is your updated code :slight_smile:

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){
      console.log("my custom js has been loaded");
      $('#ore_turno_c').val($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val());
      console.log("custom js finished. result ore_turno_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;
    });

Okay, so now my code should work

This is the code:

//CALCOLO ORE LAVORATIVE

var n_pezzi_c = document.getElementById("n_pezzi_c");

var tempo_ciclo_c = document.getElementById("tempo_ciclo_c");

var ore_turno_c = document.getElementById("ore_turno_c");

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

      $('#ore_turno_c').val($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val());

    });

I’ve updated the example above, and here you can see the script working with your ids:
https://jsfiddle.net/2qaxueog/

Maybe I have some problem while loading my JavaScript code as in suitecrm the calculations do not happen even after entering your code …
I’ll be back to thank you for the time you are dedicating to me

Have you added some console.log() commands for debugging? Do you see anything broken in your browsers console (e.g. 404 messages)?
-> First step is to figure out if your script was loaded at all.

Now I’m checking and I don’t understand why it gives me these errors including what you said 404 not found, I am attaching all the errors that the console gives me.

sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:8 JQMIGRATE: Migrate is installed, version 3.3.0
quotazioni.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)
index.php:1 Refused to execute script from 'http://localhost/suitecrm/cache/jsLanguage/PTN_Quotazioni/it_it.js?v=dxTHg1cqgKV32ArKLzmfDQ' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
quotazioni.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)

is your file located in that directory? if you’re testing on linux, did you set the proper owner/permissions for that file?
E: and maybe try to remove the leading “/”, not sure if that could cause the issue.

1 Like

I solved it, I changed the path by putting it in the module folder and then I removed the initial “/” as you advised me and now the javascript file starts. Thank you very much for your help and your availability I ask you a last courtesy if I have any problems can I contact you in private? I was really happy with your help and advice! Grace again!

Hi,
you’re welcome, and I’m glad you got it working :slight_smile:

If you want further help/support, pls stick to the forums here. I usually don’t answer such questions in private messages as nobody else would benefit from that (if you post here, we can link this thread for future requests, and people can find the solution using google/the search function).

2 Likes

That’s fine thanks a lot. I can use your function for other calculations as well by just changing the ID. You really helped me a lot whatever I will post here on the forum so that everyone can access it! :slight_smile:

Hi Diligent sorry for the inconvenience I wanted to ask you for a hand I can’t enter the toFixed (3) method to show only 3 decimal places. I would need a hand now I am attaching the code, could you kindly tell me where am I wrong?

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){

  console.log("my custom js has been loaded");

  $('#ore_totali_effettive_c').val($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()).toFixed(3);

  console.log("custom js finished. result ore_totali_effettive_c: " + $('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()) ;

});
 $('#ore_totali_effettive_c').val(parseFloat($('#n_pezzi_c').val() / $('#tempo_ciclo_c').val()).toFixed(3));

E: try it here with 10/3 https://jsfiddle.net/0L3xag68/

Thanks Diligent it works! I realized my mistake I did not enter the parseFloat thanks for all the help you give me!
I wanted to ask you if I wanted to transform this decimal value into HH and MM is it possible or not? I await your reply thank you very much!

Hi,
these are all questions not related to suite, but here you go: https://jsfiddle.net/j9e3kgtv/

I’ve copied a random js method that does what you describe and included it.

Maybe these links can help you getting better with js/jquery:

https://www.w3schools.com/js/DEFAULT.asp
https://www.w3schools.com/jquery/

1 Like

Thank you very much, yes I had already started reading the guides on w3schools so as to improve my skills. Thank you so much for your help!

Hi everyone, I find myself here again as I have problems with suite again. I have the following Javascript and Jquery code and it works up to this calculation where it returns me the following errors, I have searched around but I have not found much help so I kindly ask you.

THIS IS THE CODE:
$(document).on(‘change’,’#durata_ore_turno_c’,’#team_c’,’#ore_c’,function(){

  console.log("JS started with turn calcolation");

  $('#turni_c').val($('#ore_c').val() / $('#durata_ore_turno_c').val() * $('#team_c').val());

  console.log("custom JS finished. result turni_c: " + $('#ore_c').val() / $('#durata_ore_turno').val() * $('#team_c').val());

});

THIS IS THE ERROR:
sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:3 Uncaught TypeError: Cannot create property ‘guid’ on string ‘#ore_c
at Object.add (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:3)
at Object.s.event.add (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:8)
at HTMLDocument. (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:3)
at Function.each (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:2)
at s.fn.init.each (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:2)
at wa (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:3)
at s.fn.init.on (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:3)
at :45:13
at p (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:2)
at Function.globalEval (sugar_grp1_jquery.js?v=KEA9jtZXITCszKE1hvNLxw:2)

that is your error, did you add some other js that tries to work with an attribute called guid?

Edit: your js looks broken too, have a look at the very first line (you’re mixing different apostrophes like ', ", `) and the listener definition seems wrong too (look at the field ids and how you need to list them).:

$(document).on('change','#n_pezzi_c, #tempo_ciclo_c',function(){
vs.
$(document).on(‘change’,’#durata_ore_turno_c’,’#team_c’,’#ore_c’,function(){

1 Like