フォームの値を取得して計算するClient Scriptの書き方について

Toshikazu
Tera Contributor

表題について、フォーム上のIntegerのタイプのフィールドから値を取得して計算し、

別のIntegerタイプのフィールドに値を設定するClient Scriptを書こうとしています。

 

私がJavascript初心者であることもあり、うまく動作せずに困っています。

ご協力いただけないでしょうか。

 

具体的には、フォーム上からIntegerの数値を取得し、消費税を計算させてIntegerのフィールドに格納したいです。

 

テーブル:Forecast

Type:onChange

フィールド:Planned Amount(Excludint Tax)

 

入力したいフィールド:Consumption Tax(消費税)

 

***以下Script***

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    var price = 0.1 * newValue;
    g_form.setValue('u_planned_consumption_tax', price + '');  
}
 
Toshikazu_1-1704899734189.png

 

6件の返信6

@Toshikazu  If you want to keep decimal value then you can not store into integer type field, This is the functionality of integer type field, it will not allow you to store decimal value.

 

If you can not change the field type, create new field and migrate old data from your integer type field to new field and then going forward maintain the calculation in new field. 

 

Thanks,

Harsh

 

 

Hitoshi Ozawa
Giga Sage
Giga Sage

少し古いスレッドですが解決されていないために返信します。

消費税の端数は「切り捨て」「切り上げ」「四捨五入」のどれかにします。

次の例では切り捨てにします。JavaScriptのfloorメソッドを使い整数に変換します。

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    try {
        var price = 0.1 * newValue;
        g_form.setValue('u_planned_consumption_tax', Math.floor(price));
    } catch (err) {
        g_form.addErrorMessage(err.message);
    }
}