本文介绍了Handsontable限制细胞特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been searching for the answer to a very simple question, how to stop a user from entering 250 characters into a cell of a Handsontable? I have found I can recreate a validation, but that won't stop a user from entering more than 250 characters. I am looking for something like maxlength:

<input id="notes" maxlength="250" />



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可能没有更长的时间需要答案。但希望这会帮助别人。

This question is several months old, so Filjan may no longer need an answer. But hopefully this will help someone out.

您可以使用函数来代替使用正则表达式作为验证器。

Instead of using a regular expression as the validator, you can use a function.

定义像这样的列对我有用...

Defining the column like this worked for me...

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);
       }];

这篇关于Handsontable限制细胞特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:04