Add Functions for Currency and Date Formatting

Start by adding functions for formatting date and currency according to the preferences of the person using the app.

  1. In the App UIs pane, click <yourguid>-orders. For example, akhan-orders.
  2. On the <yourguid>-orders tab, click the JavaScript subtab.
    In the App UI panel, <guid>-orders, akhan-orders in this image example, is selected and outlined in red, and the akhan-orders page displayswith the JavaScript tab opened amd outlined in red.
    Caution: Make sure you select the JavaScript subtab for the App UI and not the manage-orders page. The code snippet needs to be added to the App UI JavaScript file (and not the manage-order page JavaScript).
  3. In the JavaScript subtab, delete the entire code block, so that the JavaScript page is empty.
  4. Copy and paste this custom function that uses utility classes to format date and currency values into the JavaScript page.
    define([
      'ojs/ojconverterutils-i18n',
      'ojs/ojconverter-number',
      'ojs/ojconverter-datetime',
      'oj-sp/common-util/common-util'
    ],
        function ( IntlConverterUtils, NumberConverter, DateTimeConverter, utilityClass) {
      'use strict';
      var AppModule = function AppModule() {
        // get the default date format pattern from user profile
        var dateOptions = {
         formatStyle: "date",
          pattern: utilityClass.getDateFormatPattern()
        };
        // create just one instance of converter per application
        this.dateConverter = new DateTimeConverter.IntlDateTimeConverter(
          dateOptions
        );
        // create map to cache currency converters
        this.currencyConverterMap = new Map();
      };
     
    AppModule.prototype.formatDate = function (date) {
      return this.dateConverter.format(date);
    };
     
    AppModule.prototype.formatCurrency = function (currCode, value) {
      // if caller provides the currency code then use it otherwise get the default one from the user profile
      currCode = currCode ? currCode : utilityClass.getCurrencyCode();
     
      // if converter doesn't exists in map create a new one and update cache
      if (!this.currencyConverterMap.get(currCode)) {
        var numberOptions = {
          style: "currency",
          currency: currCode
        };
     
        // update the cache with new converter
        this.currencyConverterMap.set(currCode, new NumberConverter.IntlNumberConverter(numberOptions));
      }
      return this.currencyConverterMap.get(currCode).format(value || 0);
    };
     
    return AppModule;
    });
    The code you entered should look like this:
    The AppUI page, in this image example, the akhan-orders page, displayswith the JavaScript code you entered.
  5. Close the <yourguid>-orders tab.