简体   繁体   English

具有最多行数和每行最多字符的Textarea

[英]Textarea with max number of lines and max characters per line

I'm facing one issue where I need to develop textarea where number of lines must be 10 and maximum character per line - 15. Once the 16th character will be entered it must move to the next line. 我遇到一个需要开发textarea问题,该行的行数必须为10,每行最大字符数-15。 一旦输入了第16个字符,它就必须移至下一行。

Also we can't enter more than 10 lines. 另外,我们不能输入超过10行。 Here's what I am trying to do. 这就是我想要做的。

Onchange listener. Onchange侦听器。 I'm trying to restrict the user to enter 15 chars, but it's not happening. 我正在尝试限制用户输入15个字符,但这没有发生。 Below is my code. 下面是我的代码。 Any suggestions will be helpfull. 任何建议都会有所帮助。

Ext.create('Ext.form.FormPanel', {
    title      : 'Sample TextArea',
    width      : 400,
    bodyPadding: 10,
    renderTo   : Ext.getBody(),
    items: [{
        xtype     : 'textareafield',
        grow      : true,
        name      : 'message',
        fieldLabel: 'Message',
        anchor    : '100%',
        listeners : {
            change : function(val,b){
                debugger;
                var text = val.getValue();
                var lines = text.split(/(\r\n|\n|\r)/gm); 
                 for (var i = 0; i < lines.length; i++) {
                    if (lines[i].length > 10) {
                        lines[i] = lines[i].substring(0, 10);
                    }
                }
                     text = lines.join('');
                //alert(val.value)
            }
        }
    }]
});

Here I have asked for number of charactes to each line and number of line is text area. 在这里,我要求每行的字符数和行数是文本区域。

As a disclaimer, this type of solutions (on change listeners) are prone to complex problems: there are many ways to input content, such as drag-n-drop stuff into the textfield, copy/paste, etc. But here is a somehow working solution with some comments: 作为免责声明,这种类型的解决方案(针对更改侦听器)容易出现复杂的问题:输入内容的方法有很多,例如将内容拖放到文本字段,复制/粘贴等。有一些评论的工作解决方案:

Ext.create('Ext.form.FormPanel', {
    title      : 'Sample TextArea',
    width      : 400,
    bodyPadding: 10,
    renderTo   : Ext.getBody(),
    items: [{
        xtype     : 'textareafield',
        grow      : true,
        name      : 'message',
        fieldLabel: 'Message',
        anchor    : '100%',
        numLines : 15,
        numCharsPerLine: 10,
        listeners : {
            change : function(val,b){
                var text = val.getValue();
                //console.log(text);
                var lines = text.split(/\r\n|\n|\r/); 
                 for (var i = 0; i < lines.length; i++) {
                    if (lines[i].length > this.numCharsPerLine) {
                        lines[i] = lines[i].substring(0, this.numCharsPerLine);
                    }
                }
                if(lines.length < this.numLines && lines[lines.length-1].length == this.numCharsPerLine) lines[lines.length-1] += "\n" ;
                lines = lines.slice(0,this.numLines); // limit line number
                 text = lines.join('\n');
                 //console.log(text);
                 this.setRawValue(text)
                //alert(val.value)
            }
        }
    }]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM