Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改前清除输入_Javascript_Jquery_Handsontable - Fatal编程技术网

Javascript 更改前清除输入

Javascript 更改前清除输入,javascript,jquery,handsontable,Javascript,Jquery,Handsontable,我使用的是带有浮动列的handsontable 0.35.1。其目的是允许用户从电子表格(以及在excel中打开的CSV)中复制粘贴。问题是,它附带了一些我需要扔掉的垃圾。未正确验证的输入示例如下: 1,000.00 USD 100.00 '10000.00 ' //note there are trailing spaces 我想找到一种方法,我可以在输入被编写之前操作它。到目前为止,我发现的唯一方法是使用beforeChange,但问题是验证。系统更改输入,但似乎已经验证。如果我一次又

我使用的是带有浮动列的handsontable 0.35.1。其目的是允许用户从电子表格(以及在excel中打开的CSV)中复制粘贴。问题是,它附带了一些我需要扔掉的垃圾。未正确验证的输入示例如下:

1,000.00
USD 100.00
'10000.00  '  //note there are trailing spaces
我想找到一种方法,我可以在输入被编写之前操作它。到目前为止,我发现的唯一方法是使用beforeChange,但问题是验证。系统更改输入,但似乎已经验证。如果我一次又一次地模糊,它就会起作用

这是我的建议。复制步骤:输入数字a123——应更正为123并验证为正确的数字

我尝试使用beforeValidation替代,但它没有按照我的预期工作。

我更新了示例,该示例在1个函数中都能工作

function trimFloat(value) {
    return value.trim().replace(/[^0-9\.\-\+]/g, '');
}

options = {
  columns: [
    { type: 'date' },
    { type: 'numeric', numericFormat: {pattern: '0,0.00'}, trimWhitespace: true }
  ],
  colHeaders: ["Date", "Float"],
  beforeChange: function(changes, source){
    let that = this;
    _.each(changes, function(change){
      if (_.isString(change[3])) {
            let value = trimFloat(change[3]);
          //prevent endless loop
          if (value !== change[3]) {
            change[3] = trimFloat(change[3]);
                        that.setDataAtCell(change[0], change[1], change[3]);       
          }
      }
    })
  }
}
$('#hot-sheet').handsontable(options)
我更新了示例,它可以在1个函数中工作

function trimFloat(value) {
    return value.trim().replace(/[^0-9\.\-\+]/g, '');
}

options = {
  columns: [
    { type: 'date' },
    { type: 'numeric', numericFormat: {pattern: '0,0.00'}, trimWhitespace: true }
  ],
  colHeaders: ["Date", "Float"],
  beforeChange: function(changes, source){
    let that = this;
    _.each(changes, function(change){
      if (_.isString(change[3])) {
            let value = trimFloat(change[3]);
          //prevent endless loop
          if (value !== change[3]) {
            change[3] = trimFloat(change[3]);
                        that.setDataAtCell(change[0], change[1], change[3]);       
          }
      }
    })
  }
}
$('#hot-sheet').handsontable(options)

您可以使用
beforeposte
callback清除输入

  options = {
    columns: [
      { type: 'date' },
      { type: 'numeric', numericFormat: {pattern: '0,0.00'} }
  ],
  colHeaders: ["Date", "Float"],
  beforePaste: (data, coords) => {
    data.forEach((col, colIndex) => {
        col.forEach((row, rowIndex) => {
        let value = row.trim().replace(/[^0-9\.\-\+]/g, '')
        data[colIndex][rowIndex] = value
      })
    })
    return true
  },
}
$('#hot-sheet').handsontable(options)
这是小提琴


注意:您不能创建新的数据数组,您必须更新数据数组,而不是创建新的数据数组

您可以使用
beforeposte
回调清除输入

  options = {
    columns: [
      { type: 'date' },
      { type: 'numeric', numericFormat: {pattern: '0,0.00'} }
  ],
  colHeaders: ["Date", "Float"],
  beforePaste: (data, coords) => {
    data.forEach((col, colIndex) => {
        col.forEach((row, rowIndex) => {
        let value = row.trim().replace(/[^0-9\.\-\+]/g, '')
        data[colIndex][rowIndex] = value
      })
    })
    return true
  },
}
$('#hot-sheet').handsontable(options)
这是小提琴


注意:您不能创建新的数据数组,您必须更新数据数组,而不是创建新的数据数组

beforePaste只能解决复制粘贴问题。这实际上已经足够了,是的,但还不完整。我希望能够“清理”用户提供的任何输入BeforePaste将只解决复制粘贴问题。这实际上已经足够了,是的,但还不完整。我希望能够“清理”用户提供的任何输入。这是可行的,但仍然不理想。“清理”输入的整个逻辑运行两次。一次更改输入,一次验证。我想让HOT考虑用预先更改方法验证值作为输出。因此,默认验证在输入更改后运行。@我看到了我的更新,这个版本在更改前的
中都可以运行,但仍然不理想。“清理”输入的整个逻辑运行两次。一次更改输入,一次验证。我想让HOT考虑用预先更改方法验证值作为输出。因此,默认验证在输入更改后运行。@如果我看到我的更新,此版本都在更改前的