2016-03-01 64 views
0

我需要修改貨幣過濾器,以便能夠指定小數位數......我需要0,2和4小數位在貨幣在不同的地方......我在想「 flexCurrency:4「,但找不到必要的文檔以瞭解如何覆蓋貨幣過濾器。我在想象着角的過濾器看起來是這樣的:在VueJS需要一個靈活的貨幣過濾器

.filter('flexCurrency', flexCurrencyFilter) 

function flexCurrencyFilter($filter) { 
     return function (input, decPlaces) { 
      decPlaces = decPlaces || 0; 

      // Check for invalid inputs 
      if (isNaN(input) || !input || Math.abs(input) === 0 || (Math.abs(input) > 0 && Math.abs(input) < 1)) { 
       return '-'; 
      } 
      var out = input; 

      //Deal with the minus (negative numbers) 
      var minus = out < 0; 
      out = Math.abs(out); 
      out = $filter('number')(out, decPlaces); 

      // Add the minus and the symbol 
      if (minus) { 
       return '($' + out + ')'; 
      } else { 
       return '$' + out; 
      } 
     }; 
    } 

回答

0

貨幣過濾器是在source code,只是適應它採取額外的ARG。這應該工作:

flexCurrency (value, currency, decimals) { 
    value = parseFloat(value) 
    if (!isFinite(value) || (!value && value !== 0)) return '' 
    currency = currency != null ? currency : '$' 
    var stringified = Math.abs(value).toFixed(decimals) 
    var _int = stringified.slice(0, -1 - decimals) 
    var i = _int.length % 3 
    var head = i > 0 
     ? (_int.slice(0, i) + (_int.length > 3 ? ',' : '')) 
     : '' 
    var _float = stringified.slice(-1 - decimals) 
    var sign = value < 0 ? '-' : '' 
    return sign + currency + head + 
     _int.slice(i).replace(digitsRE, '$1,') + 
     _float 
    }, 
+0

這可能需要調整以處理0小數位 – Jeff

+0

是否可以在vue中編輯源代碼?其他框架警告這一點。 – jgravois

+0

我不會編輯源代碼,我只是爲您的目的創建一個新的過濾器。沒有辦法根據說法來擴展或修改過濾器,但將其適配爲創建新過濾器就沒有問題。 – Jeff