简体   繁体   English

Handsontable - 我在复制/粘贴德语数值时发现了一个错误

[英]Handsontable - I found a bug when copying/pasting a german numeric value

I want to use the Javascript grid framework Handsontable for my next project, but I found a small bug, which is a blocking point for using this lib. 我想在我的下一个项目中使用Javascript网格框架Handsontable,但我发现了一个小bug,这是使用这个lib的阻塞点。 :-( :-(

I have to show a table with german prices, eg "18,50 €". 我必须显示一张德国价格表,例如“ 18,50€”。 The german language has a , (comma) as decimal point. 德语以小数点(,)表示。 By default handsontable has only the language "en" for the embedded "numeral.js" helper library. 默认情况下,handsontable仅对嵌入式“ numeral.js”帮助程序库使用语言“ en”。 So added the german (de) language definition for numeral.js. 因此,为numeric.js添加了德语(de)语言定义。 The cell format is defined as "0.00 $" and it will show correctly as "18,50 €" in the grid. 单元格格式定义为“0.00 $”,它将在网格中正确显示为“18,50€”。

But when I select a cell with eg "1,50 €" (single click on the cell), then press "Ctrl+C" and paste it again with Ctrl+V into another cell. 但是当我选择一个例如“1,50€”的单元格(单击单元格)时,按“Ctrl + C”并再次使用Ctrl + V将其粘贴到另一个单元格中。 The new cell has the wrong value "15,00 €", because the copied value in the clipboard is "1.5" which then will be pasted as "15". 新单元格的值为“15,00€”,因为剪贴板中复制的值为“1.5”,然后将其粘贴为“15”。

HTML: HTML:

<div id="exampleGrid"></div>

Javascript: 使用Javascript:

(function () {
    var language = {
        delimiters: {
            thousands: '.',
            decimal: ','
        },
        abbreviations: {
            thousand: 'k',
            million: 'm',
            billion: 'b',
            trillion: 't'
        },
        ordinal: function (number) {
            return '.';
        },
        currency: {
            symbol: '€'
        }
    };
    if (typeof window !== 'undefined' && this.numeral && this.numeral.language)  {
        this.numeral.language('de', language);
    }
}());

$("#exampleGrid").handsontable({
    data: [ [1.5], [] ],
    rowHeaders: true,
    colHeaders: true,
    columns: [
        { type: 'numeric', format: '0.00 $', language: 'de'}
    ]
});

I have created a jsfiddle for demonstrating the problem: http://jsfiddle.net/hU6Kz/4873/ 我创建了一个用于演示问题的jsfiddle: http//jsfiddle.net/hU6Kz/4873/

I work with version 0.23.0. 我使用版本0.23.0。 I tried a lot to workaround this bug (hooks, custom validators, ...) but nothing helps, because I didn't get the original value "1.5" inside the event callbacks. 我尝试了很多来解决这个bug(钩子,自定义验证器......),但没有任何帮助,因为我没有在事件回调中得到原始值“1.5”。 I only get the wrong value "15". 我只得到错误的值“15”。 You can see this, when using "beforeChange" hook as additional option in the handsontable constructor: 当在handsontable构造函数中使用“ beforeChange”钩子作为附加选项时,您可以看到以下内容:

beforeChange: function (changes, source) {
        for (var i = changes.length - 1; i >= 0; i--) {
            console.log('Changes: source:' + source + ' row:' + changes[i][0] + ' col:' + changes[i][1] + ' old:' + changes[i][2] + ' new:' + changes[i][3]);
        }
    }

When I do the copy and paste of "1,50 €" then the console shows: 当我复制并粘贴“ 1,50€”时,控制台将显示:

Changes: source:paste row:1 col:0 old:null new:15

My idea was to recognize the 1.50 and change it to 1,50 before handsontable updates the cell, but the new value is already "15", so no chance to see the original copied value. 我的想法是识别1.50并在handontable更新单元格之前将其更改为1,50,但新值已经是“15”,因此没有机会看到原始复制的值。 :-( :-(

Perhaps someone of you has an idea. 也许你们中的某个人有一个想法。

Thanks in advance. 提前致谢。

Update: 更新:

In the source code I found the function 'validateChanges' which does the following: 在源代码中,我找到了执行以下操作的函数“ validateChanges”:

if (numeral.validate(changes[i][3])) {
    changes[i][3] = numeral().unformat(changes[i][3]);
}

When the current numeral language is 'de' and call this, the result ist "15". 当当前数字语言为'de'并将其命名为“ 15”时。 So here is the problem, but how to solve it? 所以这是问题,但如何解决呢?

numeral().unformat('1.5 €')

And now I understand, why I didn't get the original copied value when the hook "beforeChange" is called. 现在我明白了,为什么在调用钩子“ beforeChange”时没有得到原始的复制值。 Because this 'validateChanges' is called before the hook is triggered. 因为此“ validateChanges”是在挂钩被触发之前调用的。

And now? 现在?

Replace on line 24646: 在第24646行替换:

if (languages[currentLanguage].delimiters.decimal !== '.') {
  string = string.replace(/\./g, '').replace(languages[currentLanguage].delimiters.decimal, '.');
}

With this: 有了这个:

if (languages[currentLanguage].delimiters.decimal !== '.') {
  string = string.replace(languages[currentLanguage].delimiters.decimal, '.');
}

The first string replace removes the decimal point from the raw data. 第一个字符串替换从原始数据中删除小数点。

Note: I'm not that familiar with this js library, this could lead to unintended consequences. 注意:我不太熟悉这个js库,这可能会导致意想不到的后果。 Discuss the problem with the project maintainers on their github page. 在他们的github页面上与项目维护者讨论问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 粘贴到handontable时拦截剪贴板信息 - Intercept clipboard information when pasting to handsontable CSS 复制粘贴时发生变化? - CSS changes when copying and pasting? Handsontable-粘贴到单元格时禁用自动向上滚动 - Handsontable - Disable auto scroll up when pasting into a cell 粘贴行时,Handsontable afterCreateRow会触发多次 - Handsontable afterCreateRow fires multiple times when pasting rows 复制和粘贴列表项时如何添加换行符? - How can I add line breaks when copying & pasting list items? 从Microsoft Word复制和粘贴时,CKEditor会剥离标签。 如何诊断为什么会这样? - CKEditor is stripping a tags when copying and pasting from microsoft word. How can I diagnose why this is happening? Handsontable错误:在jquery ui内部启动时,对话框不可编辑 - handsontable bug: when launched inside jquery ui dialog boxes are unedittable 可处理-数字列类型 - Handsontable - numeric columns type 用鼠标处理粘贴。 使用ctrl + v进行粘贴时,将event.target.value设置为我粘贴的任何字符串,但是当使用鼠标进行粘贴时,不会 - Handling paste by mouse. When pasting with ctrl + v the event.target.value is set to whatever string I pasted but when pasting but mouse it is not Javascript/Jquery - 将文本复制并粘贴到 textarea 时删除换行符 - Javascript/Jquery - Removing break line when copying and pasting text to textarea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM