Page 1 of 1

manipulate multiple mapping values via javascript

Posted: Tue Oct 28, 2014 4:18 pm
by Jerry7185911

I have mapped two values from my database response to a single control. For example, firstname and lastname. I want to combine these using javascript in the mapping action editor:

function (value, element) {

return value.firstname + " " + value.lastname;

dot notation does not seem to work, and accessing value only returns the 2nd value that is mapped (lastname). Is there a way to do this?

thanks


manipulate multiple mapping values via javascript

Posted: Tue Oct 28, 2014 4:32 pm
by Evgene Karachevtsev

Hello Jerry,

value.firstname and value.lastname are not defined in the code where you add it. In value you will have the value of lastname or firstname, just strings. You should add this code in the mapping of the whole object and fill in result the desired element using js.
Please see this discussion https://getsatisfaction.com/apperyio/...


manipulate multiple mapping values via javascript

Posted: Tue Oct 28, 2014 4:42 pm
by Bruce Stuart

I'd respectfully add however knowing Evegene is extremely qualified ... That you could place a hidden control on the page, have the first and last names as hidden controls, and then in a post mapping event .... Select run JavaScript ...and then in that event: ( replace the control names below with yours )

var scombinedname ;
Scombinedname = Appery( 'controlnameforfirstname').val() + "." + Apppery('controlnameforlastname').val();
Appery('controlnameforfirstandlast'):val( scombinedname )


manipulate multiple mapping values via javascript

Posted: Tue Oct 28, 2014 4:44 pm
by Bruce Stuart

And perhaps you also might ned to re-show the control with

Appery('combinedcontrolname').show();


manipulate multiple mapping values via javascript

Posted: Wed Oct 29, 2014 6:40 am
by Jerry7185911

thank you both for your help.

So i have played around with all these approaches - and had trouble using both controls and localstorage because i am mapping to a list item with multiple rows. I'm sure it could be done, but I was more interested in the explanation in the link provided regarding how multiple mappings are handled. Obviously, why have multiple mappings if only the last one is relevant? well, apparently all the mappings are passed as separate calls to the javascript in the order the mappings are added. So in my case, I was able to add firstname and lastname mappings to the same destination control - and I added this javascript:

var result = "";
if (element.text().trim().length === 0)
{
result = value;
}
else
{
result = element.text() + " at " + value;
}
return result;

For the first call, the control was always empty (though I had to use the trim() because it contained 29 spaces every time - not sure why. it may have been an artifact of having a default value and then removing it). Then the second time, it had content/length, so I could handle the value as the second mapping value.

Hopefully this helps others understand how to use multiple mappings - especially in a list item situation where controls and localstorage can get complicated.

thanks again!


manipulate multiple mapping values via javascript

Posted: Wed Oct 29, 2014 6:41 am
by Jerry7185911

Bruce - I'm sure this would work perfectly with a single row result. thanks for the suggestion.