Jump to content
  • 0

Changing the currency sign based on the field in the Authentication table


Inscriptor

Question

Is it possible to dynamically change the currency sign in my DataPage based on which user is logged in?

For example, if the user is from Europe, i want all the currency signs to be '€', but if he is from USA, I want all currency signs in the DataPage to be '$'.

I already tried it in Localizations, and there are no options to do that, and I don't want to make separate DataPages for EU and USA users.

thank you in advance!

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 1

It is possible to change the currency sign dynamically, but only with a JavaScript solution.

For the Report DataPage, set the fields formatting to US dollar ('$'). Then the following code should be put in the Footer of the DataPage:

Note that if you are not using all the elements in the Report as described in the code, just delete the parts using that elements.

<script>
document.addEventListener('DataPageReady', () => {
  
        /* Declaring the function for replacing the specific text value in some array of HTML elements */
        const replacing = (obj,whatToReplace,withWhatToReplace) => {
                     obj.forEach(record => {
                     const modified = record.textContent.replaceAll(whatToReplace, withWhatToReplace);
                     record.textContent = modified;
                     });
        }
  
       /* Selecting all cells where we want to replace signs */
       let table = document.querySelectorAll('.cbResultSetTableCellNumberDate');              // Table cells
       let htmlBlocks = document.querySelectorAll('.cbResultSetTableCell');                   // HTML blocks inside the table
       let calculated = document.querySelectorAll('.cbResultSetCalculatedField');             // Calculated values in the table
       let sumAggregate = document.querySelectorAll('.cbResultSetTotalsDataCellNumberDate');  // Aggregates Total
       let aggregate = document.querySelectorAll('.cbResultSetGroup1LabelCellNumberDate');    // Aggregates in Group-level aggregations 
  
       const dollar = '$';
       const euro = '€';
       const pound = '£';

       /* Calling the Function 'replacing' for the declared elements, with arguments, based on the authentication field */
       switch('[@authfield:Localization]'){
            case 'Europe':
                 replacing(table, dollar, euro);
                 replacing(htmlBlocks, dollar, euro);
                 replacing(aggregate, dollar, euro);
                 replacing(sumAggregate, dollar, euro);
                 replacing(calculated, dollar, euro);
                 break;
            case 'Britain':
                 replacing(table, dollar, pound);  
                 replacing(htmlBlocks, dollar, pound);
                 replacing(aggregate, dollar, pound);
                 replacing(sumAggregate, dollar, pound);
                 replacing(calculated, dollar, pound);
                 break;
            case 'USA':                             // Note that this case is redundant if using the dollar as default sign
                 replacing(table, dollar, dollar); 
                 replacing(htmlBlocks, dollar, dollar);
                 replacing(aggregate, dollar, dollar);
                 replacing(sumAggregate, dollar, dollar);
                 replacing(calculated, dollar, dollar);
                 break;
       }
});

</script>

In the case you are using the "In Line Insert", then the part with declarations of variables "table" and "htmlBlocks" should be a replaced with the following code:

/* Delete the variable "htmlBlocks" and replace the declaration of variable "table" with the code */

let tableParent = document.querySelectorAll('tr[data-cb-name="data"]')
let table = [];
tableParent.forEach(el => {
        let cells = el.querySelectorAll('.cbResultSetTableCellNumberDate');
        table.push.apply(table, cells);
        let htmlBlocks = el.querySelectorAll('.cbResultSetTableCell');
        table.push.apply(table, htmlBlocks);
}); 

Also, delete the "replacing(htmlBlocks, currency, currency)" from Switch statement in this case.

The resulting Report will look something like this if the user with European localization is logged in:

Report Changing Currency.png

Link to comment
Share on other sites

  • 1

For the Submission Form, the code is a little bit different:

<script>
document.addEventListener('DataPageReady', () => {
  
        /* Select the input element of the Calculated Value */
        document.querySelector('input[name="InsertRecordSum_Of_Values"]').addEventListener('change',()=>{

               const replacing = (obj,whatToReplace,withWhatToReplace) => {
                          obj.forEach(record => {
                          const modified = record.textContent.replaceAll(whatToReplace, withWhatToReplace);
                          record.textContent = modified;
                          });
               }

               const dollar = '$';
               const euro = '€';
               const pound = '£';
          
               /* Select all Calculated Fields */
               const calculated = document.querySelectorAll('.cbFormCalculatedField');
      
               switch('[@authfield:Localization]'){
                    case 'Europe':
                          replacing(calculated, dollar, euro);
                          break;
                    case 'Britain':
                          replacing(calculated, dollar, pound);
                          break;
                     case 'USA':
                          replacing(calculated, dollar, dollar);
                          break;
                }

       });
});

</script>

The result is the following when the user with European localization is logged:

 

submission currency.png

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
Answer this question...

×   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...
×
×
  • Create New...