Skip to Content

How to get number of rows in text area when I paste something in SAPUI5?

Hi,

       In my application I am using text area as columns in a table. In that I am getting data in the text area cells  from another page.

To count the number of rows entered in the text area I am using below code and placing this count value as dynamic in text area.

var count = 0;

                for(var i=0; i<dataName.length; i++){

                if(dataName[i] !== undefined){

                    if(count < dataName[i].split(/\r|\r\n|\n/).length){

                                count = dataName[i].split(/\r|\r\n|\n/).length;

                     }

                   }

                }

oTV = new sap.ui.commons.TextArea({

                      width : '100%',

                      rows : count,

                       value : dataName[int2-2],

                      editable : false,

                    });

Every thing is working fine when I press enter to go the next line.

But the problem is when I enter any data without using enter key or when I paste some data into that text area I am getting count value only one


I even used   count = dataName[i].split("\n").length; still I am getting count value "1". It's taking all the code entered in text area as one line.

How can we count the no of rows in text area without pressing enter key?

Pleased help me to solve this.

Thanks&Regards

Sridevi.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Dec 17, 2013 at 08:51 AM

    You can use jQuery to detect changes on the textarea, this includes typing, pasting,...

    $('#textareaID').bind('input propertychange', function() {

     

    for(var i=0; i<dataName.length; i++){

                    if(dataName[i] !== undefined){

                        if(count < dataName[i].split(/\r|\r\n|\n/).length){

                                    count = dataName[i].split(/\r|\r\n|\n/).length;

                         }

                       }

                    }




    });


    replace textareaID with the id of the textarea.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      Please try the code I sent you. It should detect any change to the textfield, also when you don't press the enter key.  It binds a listener to the textarea. Make sure that above listener is already binded to the textarea before the data is put in it.

      I don't know exactly how your code is, but I think you need to do something like this:

      var count = 0;

      $(function() {       

              $('#textareaID')

                   .bind('input propertychange', function() {

                      for(var i=0; i<dataName.length; i++){

                          if(dataName[i] !== undefined){

                              if(count < dataName[i].split(/\r|\r\n|\n/).length){

                                      count = dataName[i].split(/\r|\r\n|\n/).length;

                           }

                         }

                      }

                   });

      });

      All you should do is change textareaID with the ID of your textarea (or whatever selector you need). If you don't know jQuery, please do some research on jQuery selectors so you know what to put there.

      Kind regards