Jump to content

Conditional Label change of virtual field


Recommended Posts

Hello @Mushigee2266,

If only these 6 fields are Virtual fields, you may test this code in the DataPage Footer. Do not forget to disable the HTML editor on the Advanced tab before pasting the code.

<script>
document.addEventListener('DataPageReady', assignEventListeners);
 
function assignEventListeners() {
    document.querySelector('#InsertRecordWorkOut').addEventListener('change', labelChangeHandler); // douple-check the field name
    document.removeEventListener('DataPageReady', assignEventListeners)
    };
    

function labelChangeHandler(event) {
    const dropdownValue = event.target.value;
    const labelsArr =  document.querySelectorAll('[for^="cbParamVirtual"]:not([for="cbParamVirtual1"])');
  
    if(dropdownValue == 'MIANT'){ 
        labelsArr.forEach(label =>{
            label.innerText = 'NA';
         });
     }
    else{
        labelsArr.forEach((label, index) =>{
            label.innerText = `RD${index+2}`;
         });
    }
};

</script>
Link to comment
Share on other sites

Thanks for your effort. This code is not working. I have more 22 virtual fields . But i want to change RD2 to RD6 (I want to change cbVirtual1 to MAINT and cbVirtual2,cbVirtual3,cbVirtual4,cbVirtual5 to NA.

Field Name                     Label

cbParamVirtual2         RD2

cbParamVirtual3         RD3

cbParamVirtual4         RD4

cbParamVirtual2         RD5

Link to comment
Share on other sites

@Mushigee2266,

If you have more Virtual fields, please test this code:
 

<script>
document.addEventListener('DataPageReady', assignEventListeners);
 
function assignEventListeners() {
    document.querySelector('#InsertRecordWorkOut').addEventListener('change', labelChangeHandler); //double-check the field name, it is WorkOut in the example
    document.removeEventListener('DataPageReady', assignEventListeners)
    };
    

function labelChangeHandler(event) {
    const dropdownValue = event.target.value;
    const labelsArr =  document.querySelectorAll('[for="cbParamVirtual2"], [for="cbParamVirtual3"], [for="cbParamVirtual4"], [for="cbParamVirtual5"],[for="cbParamVirtual6"]');  //double-check the field names, they are from Virtual 2 to Virtual 6 in the example
    if(dropdownValue == 'MIANT'){ 
        labelsArr.forEach(label =>{
            label.innerText = 'NA';
         });
     }
    else{
        labelsArr.forEach((label, index) =>{
            label.innerText = `RD${index+2}`;
         });
    }
};
</script>

 

The code works for me. For example, I select the "A" value, the labels are RD 1- RD 6

QOvU6lK.png

When I select the "MIANT" value, the labels are changed:

FoAFUzl.png



Hope this works for you as well. 

Link to comment
Share on other sites

Thanks for your effort. This code is not working. I have more 22 virtual fields . But i want to change RD2 to RD6 (I want to change cbVirtual1 to MAINT and cbVirtual2,cbVirtual3,cbVirtual4,cbVirtual5 to NA.

Field Name                     Label

cbParamVirtual2         RD2

cbParamVirtual3         RD3

cbParamVirtual4         RD4

cbParamVirtual2         RD5

Link to comment
Share on other sites

Thank you very much code is working now only mistake was WORKOUT case. I have made switch statement by getting idea from this both are working your code and switch statement. Thanks a lot for your cooperation and support

here is my code


<script>
document.addEventListener('DataPageReady', assignEventListeners);
 
function assignEventListeners() {
    document.querySelector('#InsertRecordWORKOUT').addEventListener('change', labelChangeHandler); // douple-check the field name
    document.removeEventListener('DataPageReady', assignEventListeners)
    };
    

function labelChangeHandler(event) {
    const dropdownValue = event.target.value;

var textWO = document.getElementById('InsertRecordWORKOUT');


const labelsArr =  document.querySelectorAll('[for="cbParamVirtual1"],[for="cbParamVirtual2"],[for="cbParamVirtual3"],[for="cbParamVirtual4"],[for="cbParamVirtual5"],[for="cbParamVirtual6"]');
  
  switch(dropdownValue) {
  case 'MAINT':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'MAINT2':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'MiNUTES':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'STREN':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'STREN2':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'FJB':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'ROWPLANK':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case 'TiMeWaRp':
  labelsArr[0].innerText=textWO.value;
  labelsArr[1].innerText='NA';
  labelsArr[2].innerText='NA';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;
  case '3HUNDRED':
  labelsArr[0].innerText='PUSH UPS';
  labelsArr[1].innerText='SIT UPS';
  labelsArr[2].innerText='NVRSRRNDR';
  labelsArr[3].innerText='NA';
  labelsArr[4].innerText='NA';
  labelsArr[5].innerText='NA';
  Break;  
 default:
  labelsArr[0].innerText='RD1';
  labelsArr[1].innerText='RD2';
  labelsArr[2].innerText='RD3';
  labelsArr[3].innerText='RD4';
  labelsArr[4].innerText='RD5';
  labelsArr[5].innerText='RD6';

 
};

</script>
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...