Custom Converter - Numbers or Text
Feb 11, 2014
http://www.bleedyellow.com/blogs/DominoHerald/entry/custom_converter_numbers_or_text?lang=en_us
What I need to do is let the user put in either strings or numbers. If a string, leave it alone. If it's a number, I want it to be formatted with thousands separators. Turns out to be pretty easy, now that I have a basis for it (my last post of a few minutes ago). I need this because the entry is for the number of miles of coverage, if "Unlimited", than it's that, otherwise it's whatever the miles entered are. I could have created an "unlimited" button, but in this case I thought letting the user enter whatever would be better. In my sample here, I am appending a bit of text just to show it worked.

<xp:inputText
            id="inputText1"
            value="#{viewScope.passVariable}">
            <xp:this.converter>
                <xp:customConverter getAsObject="#{
javascript:value;}">
                    <xp:this.getAsString><![CDATA[#{
javascript:try{
var dIn:float=parseFloat(value);
if(isNaN(dIn)){
//this is a text string
return value +  " converted";
} else {
 return dIn.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// based on:
http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
}
} catch(e){
        print (e);
}}]]></xp:this.getAsString>
                </xp:customConverter>
            </xp:this.converter>
</xp:inputText>

Sometimes, getting a handle on something really helps out. I know I'm on the simple end of what custom converters can do, but they are so much clearer in my brain now.
It's in my snippet above, but I based this on this post on StackOverflow.
Cheers,
Brian
This is an archive of my previous blog http://www.bleedyellow.com/blogs/DominoHerald attachments are in the download control