Handsontable Limit Cell Characters

我一直在寻找一个非常简单的问题的答案,如何阻止用户将250个字符输入到Handsontable的单元格中? 我发现我可以重新创建validation,但这不会阻止用户输入超过250个字符。 我正在寻找像maxlength这样的东西:

 var date_validator_regexp = /(^$|^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/][0-9]{4}$)/; var limit_validator_regexp = /(^[\s\S]{0,250}$)/; $("#gridUpdateNotes").handsontable({ startRows: 1, startCols: 2, colHeaders: ["Date", "Notes"], columnSorting: false, enterBeginsEditing: false, autoWrapRow: true, autoWrapCol: true, minSpareRows: 1, colWidths: [140, 450], removeRowPlugin: true, copyPaste: true, afterValidate: function (isValid, value, row, prop, source) { if (isValid == false && prop === "Notes") { alert("The Notes cannot have more than 250 characters."); } }, columns: [ { data: "NoteDate", type: "date", dateFormat: "mm/dd/yy", allowInvalid: false, validator: date_validator_regexp }, { data: "Notes", allowInvalid: false, validator: limit_validator_regexp } ] }); 

这个问题已有几个月了,因此Filjan可能不再需要答案了。 但希望这会帮助某人。

您可以使用函数,而不是使用正则表达式作为validation器。

像这样定义列对我有用…

 cmlCols = [ { data: "Notes", validator: function (value, callback) { if (value.length > 255) { alert('Must be 255 character or less. Extra characters will be removed'); this.instance.setDataAtCell(this.row, this.col, value.substring(0, 255), null); } callback(true); }]; 

我认为创建一个新的单元格编辑器似乎是一种更好的方法来解决这个问题:

 (function (Handsontable) { 'use strict'; var MaxLengthEditor = Handsontable.editors.TextEditor.prototype.extend(); MaxLengthEditor.prototype.prepare = function () { Handsontable.editors.TextEditor.prototype.prepare.apply(this, arguments); this.TEXTAREA.maxLength = this.cellProperties.maxLength; }; Handsontable.editors.MaxLengthEditor = MaxLengthEditor; Handsontable.editors.registerEditor('maxlength', MaxLengthEditor); })(Handsontable);