首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可处理的百分比格式前导小数点

可处理的百分比格式前导小数点
EN

Stack Overflow用户
提问于 2015-05-04 21:55:23
回答 1查看 1.8K关注 0票数 1

我认为我发现了Handsontable将数字输入格式化为百分比的方式的缺陷(或者接受无效输入,这取决于您对有效数字的立场)。

如果使用前导小数点小数点-百分比格式将不起作用-但输入将被接受为有效字符串。这与以下输入被正确转换的numeral.js不一致(文档建议使用数字格式):

代码语言:javascript
复制
var string = numeral(.001).format('0,0.00000%');
// outputs 0.1000%

我发现解决这个问题的唯一方法是添加一个beforeChanged事件,以便它在触发任何其他事件之前对该值调用parseFloat。我有一种感觉,当复制大量数据时,这会减慢速度。有没有人能确认这是一个缺陷,或者我的配置不正确?

下面的JS beforeChange摘自Handsontable页面-删除注释掉的beforeChange以查看beforeChange事件的修复。只需键入以小数点开头的任何数字。

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

代码语言:javascript
复制
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();

});
EN

回答 1

Stack Overflow用户

发布于 2015-07-10 05:11:54

在这里发布:https://github.com/handsontable/handsontable/issues/2430,但也可以在这里发布:

我认为问题出在handsontable.full.js中的numericRenderer中,它在应用格式之前检查if (helper.isNumeric(值))。如果value包含前导小数(例如“.3”),则此语句的计算结果为false。因此,您可以通过添加前导零来解决此问题:

(helper.isNumeric('0‘+ value))对于前导小数计算为true,但对于非数字("0.abaca")仍然计算为false。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30031975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档