Javascript not working in my display form

Hi,
my script works not in my display form.

function calcTotal() {

    var value;
    var total;
    var i = 0;

    value = fd.control('ListOrLibrary4').widget.dataItems();
    total = 0;
    if(value){
        for (i = 0; i < value.length; i++){
            if(value[i].Kosten)
              total += parseFloat(value[i].Kosten.replace(/[^0-9,]/g, "").replace(",","."));
        }
    }
    fd.field('Kosten_x0020_Hardware_x0020__x00').value = total;
    
    //ListOrLibrary5
    value = fd.control('ListOrLibrary5').widget.dataItems();
    total = 0;
    if(value){
        for (i = 0; i < value.length; i++){
            if(value[i].Kosten)
              total += parseFloat(value[i].Kosten.replace(/[^0-9,]/g, "").replace(",","."));
        }
    }
    fd.field('Kosten_x0020_Software').value = total;
    
    //ListOrLibrary6
    value = fd.control('ListOrLibrary6').widget.dataItems();
    total = 0;
    if(value){
        for (i = 0; i < value.length; i++){
            if(value[i].Kosten)
              total += parseFloat(value[i].Kosten.replace(/[^0-9,]/g, "").replace(",","."));
        }
    }
    fd.field('Kosten_x0020_pro_x0020_Jahr_x002').value = total;
    
    //ListOrLibrary7
    value = fd.control('ListOrLibrary7').widget.dataItems();
    total = 0;
    if(value){
        for (i = 0; i < value.length; i++){
            if(value[i].Kosten)
              total += parseFloat(value[i].Kosten.replace(/[^0-9,-]/g, "").replace(",","."));
        }
    }
    fd.field('Einsparungen_x0020_Gesamt').value = total;
    
    //Kosten_x0020_Gesamt
    fd.field('Externe_x0020_Kosten_x0020__x002').value = fd.field('Kosten_x0020_Hardware_x0020__x00').value + fd.field('Kosten_x0020_Software').value + fd.field('Kosten_x0020_pro_x0020_Jahr_x002').value 
    
    fd.field('Kosten_x0020_interner_x0020_IT_x').value = fd.field('Int_x002e__x0020_Progr_x002e__x00').value * 500;
    
    //Kosten_x0020_Gesamt
    fd.field('Kosten_x0020__x0028_Gesamt_x0029').value = fd.field('Kosten_x0020_Hardware_x0020__x00').value + fd.field('Kosten_x0020_Software').value + fd.field('Kosten_x0020_pro_x0020_Jahr_x002').value + fd.field('Kosten_x0020_interner_x0020_IT_x').value;
    
    //Einsparungen_x0020__x0028_Delta_
    fd.field('Einsparungen_x0020__x0028_Gesamt').value = fd.field('Einsparungen_x0020_Gesamt').value - fd.field('Kosten_x0020__x0028_Gesamt_x0029').value;
    
}

    var myVar = 0;
    
fd.spRendered(function() {
    
    // disable the field
    fd.field('Kosten_x0020_Hardware_x0020__x00').disabled = false;
    
    //refreshing the  total
    var myVar = setInterval(calcTotal, 2000);

}); 

fd.spBeforeSave(function(spForm) {
    var myVar = setInterval(calcTotal, 2000);
    calcTotal();
    clearInterval(myVar);
});

Best wishes
Tom

Hello @TWendt ,

what is wrong? The values are probably not saved I suppose. You are in DisplayForm. If you need to save it from Display form, u can use some button and via PnP Js to update current list item :slight_smile:

Please specify what you try to achieve.

Thank you
Regards
Stepan

1 Like

Dear @TWendt,
Yes, can you tell us what exactly is not working? The saving part will not work, as display form is not meant for modification, but with some adjustments - it's possible to add changes to the existing items like Stepan said, you'll need a separate pnpjs request.

Or do you just want the calculation to work without saving? Please, describe what you want to achieve and we'll try to help adjusting the code!

1 Like

Hi,
this is not a Javascript problem. It seems a problem with the display view. See the screenshots.

Edit-View

Display-View

The Field Kosten_x0020_Hardware_x0020__x00 are not displayed in the display view.

Best wishes
Tom

Hello @TWendt ,

It works for me. I have a text field "txtDelete" and when the form is initialized, in:

fd.spRendered(() => {
fd.field("txtDelete").disabled = true
})

then via developer console, I add the value like:

fd.field("txtDelete").value = "1500.00 €"

This is how it looks like in NewForm:

And in DisplayForm:
image

So it works for me.

Does developer console show any errors?

Hi,

I found the error. My mistake.

Best wishes
Tom

1 Like