VyTre
VyTre

Reputation: 146

ExtJs: TextArea how to return value as an array instead of as a string?

On ExtJs 6.2, I have a form with a TextArea. When user inputs text on several lines, for example :

line 1
line 2

The binded value is currently a string :

value= "line 1↵line 2"

But I need to send value (to server) as an array, when the store is submitting.

How to tell to the textarea to return input text as an array ?

value : ["line1", "line2"]

without to have to split string as array on the server-side.

Edit: I dont't want just to split value. Because I would to update the default behavior of the textarea to avoid to have to apply the split (in ViewController) each time that I using it.

Upvotes: 1

Views: 1469

Answers (3)

Stuart
Stuart

Reputation: 3258

I have created a fiddle with how I would approach it (assuming I have understood your requirements correctly!)

https://fiddle.sencha.com/#view/editor&fiddle/1uq7

This example turns an array into a string when coming into the textarea, and splits it on the way out.

Ext.define('ArrayTextArea', {
    extend: 'Ext.form.field.TextArea',

    alias: 'widget.arraytextarea',

    setValue: function(val){
        // if it's an array then join with a newline
        if(Ext.isArray(val)){
            val = val.join('\n');
        }

        return this.callParent([val]);
    },
    getValue: function(){
        var val = this.callParent(arguments);

        // split the value by newline char
        return val.split('\n');
    }

});

Upvotes: 2

Tejas
Tejas

Reputation: 902

You can use standard way provided by sencha http://docs.sencha.com/extjs/5.0.0/api/Ext.form.field.TextArea.html#placeholder-simple-getValue Check this and reply.

Upvotes: 0

Marko Nikolic
Marko Nikolic

Reputation: 31

document.getElementById('textarea').value.split('↵')

Upvotes: 2

Related Questions