2

I'm trying to output a sentence containing 4 variables, with their values emboldened using the following code:

<mx:Text width="100%" y="307">
    <mx:htmlText>
        <![CDATA[Showing data from <b>{labelStartTime.text} {labelStartDate.text}</b> to <b>{labelEndTime.text} {labelEndDate.text}</b>]]>
    </mx:htmlText>
</mx:Text>

However, this just outputs the variable names, rather than their values. I'm sure I'm missing something simple, but I'd appreciate any pointers.

Cheers.

2 Answers 2

3

I know of a workaround:

Actionscript:

private var variable:String = "Variable String";
private var str:String = "<i><b>" + Variable + "</b></i>";

Mxml:

<Text htmlText="{str}" />
3

I don't think it's possible to add bindings directly in a CDATA section like that, you do have a couple of options though :

  1. Use a function, taking advantage of BindingUtils.bindSetter :
    import mx.binding.utils.BindingUtils;
    //called on creationComplete
    private function init():void
    {
        BindingUtils.bindSetter(setHtmlText, labelStartTime, "text");
        BindingUtils.bindSetter(setHtmlText, labelStartDate, "text");
        BindingUtils.bindSetter(setHtmlText, labelEndTime, "text");
        BindingUtils.bindSetter(setHtmlText, labelEndDate, "text");
    }
    
    private function setHtmlText(val:String):void
    {
        myText.htmlText = "Showing data from <b>" + 
                            labelStartTime.text + " " + 
                            labelStartDate.text + "</b> to <b>" + 
                            labelEndTime.text + " " +
                            labelEndDate.text + "</b>";
    }
  2. Or simply encode the tags and insert them directly into the attribute :
    <mx:Text id="myText" width="100%" y="307" 
             htmlText="Showing data from &lt;b&gt;{labelStartTime.text} {labelStartDate.text}&lt;/b&gt; to &lt;b&gt;{labelEndTime.text} {labelEndDate.text}&lt;/b&gt;"/>
    This isn't really recommended as it makes the markup incredibly difficult to read, but you might get away with it for something small like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.