x-spreadsheet icon indicating copy to clipboard operation
x-spreadsheet copied to clipboard

获取单元格中数据时应有数据类型判断

Open coder-movers opened this issue 4 years ago • 3 comments

function resetTextareaSize() { const { inputText } = this; if (!/^\s*$/.test(inputText)) { const { textlineEl, textEl, areaOffset, } = this; const txts = inputText.split('\n'); // 这里默认为string类型,但当单元格中数据为number类型时会报异常 const maxTxtSize = Math.max(...txts.map(it => it.length)); const tlOffset = textlineEl.offset(); const fontWidth = tlOffset.width / inputText.length; const tlineWidth = (maxTxtSize + 1) * fontWidth + 5; const maxWidth = this.viewFn().width - areaOffset.left - fontWidth; let h1 = txts.length; if (tlineWidth > areaOffset.width) { let twidth = tlineWidth; if (tlineWidth > maxWidth) { twidth = maxWidth; h1 += parseInt(tlineWidth / maxWidth, 10); h1 += (tlineWidth % maxWidth) > 0 ? 1 : 0; } textEl.css('width', ${twidth}px); } h1 *= this.rowHeight; if (h1 > areaOffset.height) { textEl.css('height', ${h1}px); } } }

coder-movers avatar Sep 07 '21 01:09 coder-movers

你可以添加一个 toString() 在你自己的逻辑里

hondrytravis avatar Sep 07 '21 01:09 hondrytravis

我的数据是从数据库中取出,数据类型也是相对应的,感觉在处理逻辑时手动强转类型不是很好的解决办法

coder-movers avatar Sep 07 '21 01:09 coder-movers

那只能在设置数据的时候和获取数据的时候,对可以转化成 number 的值进行回转。数据库进来,把值是 number 的转换为 string,并且给个单独的属性标识符(在之后获取数据的时候要判断是否可以在转回 number) 如果可以在转回来

hondrytravis avatar Sep 07 '21 03:09 hondrytravis