Setting a default multiline value to a text area

Discuss all problems you have with Servoy here. It might help to mention the Servoy version and Operating System version you are using

Setting a default multiline value to a text area

Postby gabid » Tue Mar 19, 2013 11:38 am

Hello,

I'm dynamically creating a form with Solution Model. I have a case when I try to add a text area linked to a form variable (also a solution model) with a default value. The default value is taken from a dataset. If it contains multi lines, as normal if entered by a text area, then that value is not assigned to the variable and hence, not displayed by the text area.

The default value as shown in the developer debugger is:
Code: Select all
"line 1
line 2
3"


My code is:
Code: Select all
var txtAnswerText = form1.newVariable("vTextAnswer_"+i,JSVariable.TEXT);
txtAnswerText.defaultValue = "'" + dataset.getValue(i,5)+"'";
var new_editbox = form1.newTextArea(txtAnswerText,200,i*25+startY,375,50);


Is there a way to parse the variable or assign it differently?

Thanks!
Gabriel
User avatar
gabid
 
Posts: 44
Joined: Fri Aug 15, 2008 10:25 am
Location: Craiova, Romania

Re: Setting a default multiline value to a text area

Postby Andrei Costescu » Tue Mar 19, 2013 12:11 pm

Look at working with solutionModel as if you were doing things at designtime.
If you set the default value of a variable at design time, you will not actually have newlines in it.

For example you won't have at design time
Code: Select all
var x = 'a
b
c';
but something like
Code: Select all
var x = 'a\nb\nc';
or
Code: Select all
var x = 'a\
b\
c';

So the actual already-evaluated newline character(s) you have in that value must be converted to "\n"(or \r\n) before being assigned to the default value of a variable - didn't try the "\" approach with solutionModel but that one might work as well. When that variable is first initialized (for runtime usage) after being created through solutionModel, it's default value will be evaluated just as it happens with design time defined variables.
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: Setting a default multiline value to a text area

Postby gabid » Tue Mar 19, 2013 2:39 pm

Hi Andrei,
I believe that I am initializing the variable properly. As you can see from the debugger screenshot the value has the [slash]n set:

As I said, the value is entered in db from another text_area field.

Please advice me if you know a way to assign that value from db to a form variable using SM, using other method than mine.

Thanks,
Gabriel
Attachments
multiline_issue.png
multiline_issue.png (17.22 KiB) Viewed 3812 times
User avatar
gabid
 
Posts: 44
Joined: Fri Aug 15, 2008 10:25 am
Location: Craiova, Romania

Re: Setting a default multiline value to a text area

Postby gabid » Tue Mar 19, 2013 2:53 pm

I actually made it work by replacing \n with \\n:

Code: Select all
var txtEditText = form1.newVariable("vTextAnswer_"+record["question_id"],JSVariable.TEXT);
var txtWithNewLines = utils.stringReplace(record["answer_text"],"\n","\\n");
txtEditText.defaultValue = "'" + txtWithNewLines + "'";
var new_editbox = form1.newTextArea(txtEditText,200,i*25+startY,375,50);


Thanks for your help!
Gabriel
User avatar
gabid
 
Posts: 44
Joined: Fri Aug 15, 2008 10:25 am
Location: Craiova, Romania

Re: Setting a default multiline value to a text area

Postby Andrei Costescu » Tue Mar 19, 2013 3:03 pm

Try this instead:
Code: Select all
var txtAnswerText = form1.newVariable("vTextAnswer_"+i,JSVariable.TEXT);
txtAnswerText.defaultValue = ("'" + dataset.getValue(i,5)+"'").replace(/\r\n/g,"\\r\\n").replace(/\n/g,"\\n");
var new_editbox = form1.newTextArea(txtAnswerText,200,i*25+startY,375,50);

That view shows that your variable actually contains the character with code 10 for new line - not two chars '\' and 'n' as I suggested above.
The short form above (from the same view) is just a convenient way of showing it on one line.
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: Setting a default multiline value to a text area

Postby Andrei Costescu » Tue Mar 19, 2013 3:03 pm

Ah you beat me to it :).
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm


Return to Discuss possible Issues and Bugs

Who is online

Users browsing this forum: No registered users and 11 guests

cron