titaniumtitanium-mobileappcelerator-titaniumtitanium-alloyappcelerator-alloy

Unable to set "id" property dynamically in Alloy


I am trying to set id of label using

<ScrollableView 
     id="scrollableView"  
     showPagingControl="false" 
     dataCollection="question" 
     dataTransform="transformFunction"
     dataFilter="dataFilterFunction">   
    <ScrollView id="rwr" width="100%" layout="vertical" scrollingEnabled="false">
        <View  width="100%" layout="vertical"  height="100%" >        
          <Label id='lbl{id}' color="black"  text="{id}{description}" />    

But it doesn't work this way. In fact I tried to set accessibilityLabel too but again it didn't work.

<Label id='lbl{id}' 
    color="black"  
    text="{id}{description}" 
    accessibilityLabel ="{difficulty_level}" />

I am certain id field has some unique value because when I set in the text it shows it in text. I tried to set static text even that didn't work.

<Label id='abc' color="black"  text="{id}{description}" />

However I can set hardcoded value for id field of ScrollableView which isn't dynamically created!


Solution

  • I believe the id attribute ends up being used by Alloy somehow when building the collection, and is unable to be manipulated by common data binding later.

    You haven't specified why you need to set the Id directly, but there is another way to access a specific view inside a collection: the itemId property. Like in this example:

    <ListSection 
      dataCollection=”$.Departures” 
      dataTransform=”transformDeparture”
     >
    
     <ListItem 
       title=”{title}”
       itemId=”{departureId}”
     /> 
    </ListSection>
    

    And then you can access the element programatically, for example, on a click event:

    function itemClick(e){
     var model = $.Departures.get(e.itemId);
    }
    

    You can find a lot more information about this pattern on this 7min read article by Rene Pot.

    Hope this helps on your problem.