jQuery技术: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 = /(^[sS]{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); }]; 

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

    需要了解更多jQuery教程分享Handsontable Limit Cell Characters,都可以关注jQuery技术分享栏目—计算机技术网(www.ctvol.com)!

     (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); 

      以上就是jQuery教程分享Handsontable Limit Cell Characters相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/982864.html

      (0)
      上一篇 2021年12月14日
      下一篇 2021年12月14日

      精彩推荐