2015-05-04 213 views
1

我認爲我發現Handsontable格式數字輸入的百分比格式(或者接受無效輸入取決於您在有效數字上的位置)的缺陷。handsontable百分比格式化前導小數點

如果使用前導小數點小數點 - 百分比格式化將不起作用 - 但輸入被接受爲有效字符串。這是不是與numeral.js一致,其中下面輸入正確轉換(DOC表明數字格式應工作):

var string = numeral(.001).format('0,0.00000%'); 
// outputs 0.1000% 

我已經找到了解決這個問題的唯一方法是讓調用添加beforeChanged事件在觸發任何進一步事件之前,對值進行parseFloat。我有一種感覺,這會在複製大量數據時放慢速度。任何人都可以確認這是一個缺陷或我配置錯誤的東西?

JS小提琴下面從Handsontable頁面 - 取消註釋beforeChange以查看具有beforeChange事件的修復。只需鍵入以小數點開始的任何數字。

http://jsfiddle.net/deenairn/kwfkLqn4/4/

document.addEventListener("DOMContentLoaded", function() { 

function getCarData() { 
    return [{ 
     car: "Mercedes A 160", 
     year: 2011, 
     price_usd: 7000, 
     price_eur: 7000 
    }, { 
     car: "Citroen C4 Coupe", 
     year: 2012, 
     price_usd: 8330, 
     price_eur: 8330 
    }, { 
     car: "Audi A4 Avant", 
     year: 2013, 
     price_usd: 33900, 
     price_eur: 33900 
    }, { 
     car: "Opel Astra", 
     year: 2014, 
     price_usd: 5000, 
     price_eur: 5000 
    }, { 
     car: "BMW 320i Coupe", 
     year: 2015, 
     price_usd: 30500, 
     price_eur: 30500 
    }]; 
} 

var 
container = document.getElementById('example1'), 
    hot; 

hot = new Handsontable(container, { 
    data: getCarData(), 
    colHeaders: ['Car', 'Year', 'Price ($)', 'Price (€)'], 
    columns: [{ 
     data: 'car' 
     // 1nd column is simple text, no special options here 
    }, { 
     data: 'year', 
     type: 'numeric' 
    }, { 
     data: 'price_usd', 
     type: 'numeric', 
     format: '$0,0.00', 
     language: 'en' // this is the default locale, set up for USD 
    }, { 
     data: 'price_eur', 
     type: 'numeric', 
     format: '0,0.00 $', 
     language: 'de' // i18n: use this for EUR (German) 
     // more locales available on numeraljs.com 
    }], 
    beforeChange: 
    function(changes, source) { 
     alert(changes); 
    } 
}); 

function bindDumpButton() { 
    if (typeof Handsontable === "undefined") { 
     return; 
    } 

    Handsontable.Dom.addEvent(document.body, 'click', function (e) { 

     var element = e.target || e.srcElement; 

     if (element.nodeName == "BUTTON" && element.name == 'dump') { 
      var name = element.getAttribute('data-dump'); 
      var instance = element.getAttribute('data-instance'); 
      var hot = window[instance]; 
      console.log('data of ' + name, hot.getData()); 
     } 
    }); 
} 
bindDumpButton(); 

}); 
+0

我很確定你是對的。將此問題添加到他們的github項目中以獲得更多的牽引力。 – ZekeDroid

+0

記錄了這個缺陷 - 將會看到現在會發生什麼https://github.com/handsontable/handsontable/issues/2430 –

回答

0

張貼在這裏:https://github.com/handsontable/handsontable/issues/2430,但不妨在這裏發佈,以及:

我相信這個問題是在numericRenderer [在handsontable.full.js,在那裏它檢查是否(helper.isNumeric(value)),然後再應用格式。如果值包含前導小數(例如「.3」),則此語句的計算結果爲false。因此,您可以通過添加前導零來解決此問題:

(helper.isNumeric('0'+ value))對於前導小數計算結果爲true,但對於非數字計算仍爲false(「0.abaca 「)。

+0

從版本0.15.0開始,這個問題現在已經解決了。上面的解決方案也適用於那些<0.15.0的工作,就像添加一個beforeChange處理程序一樣,以確保將前導0添加到不帶前導0的十進制值,這是我當時選擇的解決方案。 –