2014-03-28 107 views
1

我有一個表單接受來自多個字段的貨幣,並根據我輸入的內容計算其他貨幣。對於這個大部分下面的代碼工作(例如是發票金額字段)xpages貨幣不完整輸入失敗

<xp:inputText value="#{FInvoiceDoc.InvoiceAmount}" id="InvoiceAmount"> 
<xp:this.converter> 
    <xp:convertNumber type="currency"></xp:convertNumber></xp:this.converter> 
    <xp:eventHandler event="onchange" submit="false" id="eventHandler5"> 
    <xp:this.script><![CDATA[var rate = XSP.getElementById("#{id:ExchangeRate}").value; 
    var stAmount = XSP.getElementById("#{id:InvoiceAmount}").value; 
    var stTvat = XSP.getElementById("#{id:TVAT}").value; 
    var stTst = XSP.getElementById("#{id:TST}").value; 
    var stToop = XSP.getElementById("#{id:Toop}").value; 
    var tmp; 
// get the dojo currency code 
    dojo.require("dojo.currency"); 
// get the numeric values using parse 
    amount = dojo.currency.parse(stAmount,{currency:"USD"}); 
    total = rate * amount; 
    XSP.getElementById("#{id:EstUSAmount}").innerHTML = dojo.currency.format(total,{currency:"USD"}); 
    XSP.getElementById("#{id:InvoiceAmount}").value = dojo.currency.format(amount,{currency:"USD"}); 
    if (amount != 0) { 
    tvat = dojo.currency.parse(stTvat,{currency:"USD"}); 
    tst = dojo.currency.parse(stTst,{currency:"USD"}); 
    toop = dojo.currency.parse(stToop,{currency:"USD"}); 
    tmp = (tvat/(amount-tvat-tst-toop)) * 100; 
    XSP.getElementById("#{id:VP}").innerHTML = tmp.toFixed(2); 
    tmp = (tst/(amount-tvat-tst-toop)) * 100; 
    XSP.getElementById("#{id:STP}").innerHTML = tmp.toFixed(2); 
    tmp = (toop/(amount-tvat-tst-toop)) * 100; 
    XSP.getElementById("#{id:OoPP}").innerHTML = tmp.toFixed(2); 
    } 
    ]]></xp:this.script> 
    </xp:eventHandler> 
</xp:inputText> 

就像我說的,這個作品在大多數情況下,但如果我只輸入像部分號碼

200.2

代替

200.20

那麼它失敗。大多數數據輸入人員不希望鍵入最後的「0」,因此它是合法的。

順便說一句,如果我輸入它只有200沒有美分,它沒關係。

這就好像聲明;

amount = dojo.currency.parse(stAmount,{currency:「USD」});

需要2位數的便士數量或沒有便士,但不喜歡只有領先的美分數字。

任何方法?

+0

閱讀此:http://www-10.lotus.com/ldd/ddwiki.nsf/dx/converters-on-an-xpage.htm,並測試不同的轉換器。 –

回答

1

貨幣格式是非常嚴格的,需要一定數量的小數位給定貨幣。正如你可以看到here根據貨幣可以有不同的小數位數。但對於大多數貨幣來說,它需要兩位小數。但是,你可以欺騙解析器。只需使用標準小數位數來嘗試它,如果失敗,請再次嘗試使用一位小數位。您的代碼應該是這樣的,那麼:

var amount = dojo.currency.parse(stAmount,{currency:"USD"}); 
if (!amount && amount!==0) { 
    amount = dojo.currency.parse(stAmount,{currency:"USD",places:"1"}); 
} 

這將接受輸入,比如

12 
12.3 
12.34 
$12 
$12.3 
$12.34 

,但它仍然是非常挑剔的。它不接受貨幣允許的空格或更多小數位。

如果你想爲你的用戶提供更多的靈活性,只需要使用美元貨幣,我會選擇dojo.number或其他數字解析,並在輸入欄外顯示「$」。然後,你可以接受更多的格式,並可以添加像舍入一樣的功能。

+0

謝謝!我喜歡你的榜樣比我想出的更好。 – cjames728

0

是的,我玩過轉換器。事情是我希望轉換器保持「貨幣」。我從筆記數據庫收到的數據始終是正確的,格式正確。它的操作,我有一個問題。由於這一切都在客戶端,我創建了一個可以在整個頁面中重用的函數。

<script> 
function isNumeric(n) { 
    n = parseFloat(n); 
    return !isNaN(n) || n != n; 
} 
// We call this if its a currency amount. If they only entered one number after 
// the decimal, then we just append a 0. We're expecting a string and we send 
// the string back. 
function cMask(Amount) 
{ 
    if (Amount == "") { return "0.00" } 
    a = Amount.split("."); 
    if (a.length == 2) 
    { 
     if (a[1] != null) 
     { 
      if (a[1].length == 1) 
      { 
       Amount = Amount + "0"; 
      } 
     } 
    } 
// get the dojo currency code 
    dojo.require("dojo.currency"); 
    b = dojo.currency.parse(Amount,{currency:"USD"}); 
    if (isNumeric(b)) {return Amount} else {return "0.00"} 
} 
</script> 

然後它只是改變初始可變負載線的問題。

var stAmount = cMask(XSP.getElementById("#{id:InvoiceAmount}").value); 

似乎工作,我剛剛教自己如何爲我的網頁創建可重用的客戶端JavaScript。