Flex xmllist to datagrid into a single row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kronus
    New Member
    • May 2008
    • 16

    Flex xmllist to datagrid into a single row

    I have made a lot of datagrids work in my time, but I seem to be missing something.

    I'm receiving an xmllist and all I'm trying to do is set the dataprovider. It should be straight forward, but it's placing all the children of '<userName>some Name</userName>' into a single row.

    Here's the xmllist that I'm receiving:
    Code:
    <users><userName>rob</userName>
    <userName>testd3</userName>
    <userName>mickey</userName>
    <userName>john</userName>
    <userName>kip</userName>
    <userName>pauly</userName></users>
    Here's the AS3 code receiving the xmllist and setting a provider:
    Code:
    			private function gotUsersOfGroups(x:XMLList):void
    			{
    				var myString:String = '<users>';
    				myString += x.children().child("userName");
    				myString += '</users>';
    				trace(myString);
    				var myXml:XML = new XML(myString);
    
    				myUserList.dataProvider = myXml;
    			}
    Pretty simple, right? I've tried myXml.userName, myXml.children( ), myXml.children. child("userName ") and nothing seems to work.

    What am I missing?

    As usual, thanx n advance
  • kronus
    New Member
    • May 2008
    • 16

    #2
    I figured it out and it was something simple. I had to send my XML to an XMLListCollecti on. I renamed the variables so that the code could be easy to follow. And I had to take out the Datagrid columns section of the mxml

    Code:
    			private function gotUsersOfGroups(x:XMLList):void
    			{
    				var xmlListString:String = '<users>';
    				xmlListString += x.children().child("userName");
    				xmlListString += '</users>';
    				var convertUserStringToXml:XML = new XML(xmlListString);
    				var usersOfGroupsXmlListCol:XMLListCollection = new XMLListCollection(convertUserStringToXml.userName);
    
    
    				myUserList.dataProvider = usersOfGroupsXmlListCol;
    				trace(usersOfGroupsXmlListCol);
    			}
    
                <mx:DataGrid id="myUserList" height="100%" width="65%" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" styleName="blueContainer1" backgroundColor="#2C4054" color="#FFFFFF" />
    But now, I cannot name the column header. How do I do that without bringing back the datagrid columns?

    Thanx n advance

    Comment

    • kronus
      New Member
      • May 2008
      • 16

      #3
      I was able to use the labelFunction for the datagridcolumn to populate the datagridcolumn, along with the use of the headerText to give my column a header.

      First, the new mxml
      Code:
              <mx:DataGrid id="myUserList" height="100%" width="65%" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" styleName="blueContainer1" backgroundColor="#2C4054" color="#FFFFFF">
              	<mx:columns>
              		<mx:DataGridColumn headerText="Users" labelFunction="usersOfGroupLabelFunction" />
              	</mx:columns>
              	
              </mx:DataGrid>
      And the two functions that make it possible for me to have my cake and eat it too:
      Code:
      			private function gotUsersOfGroups(x:XMLList):void
      			{
      				var xmlListString:String = '<Users>';
      				xmlListString += x.children().child("userName");
      				xmlListString += '</Users>';
      				trace(xmlListString);
      				var convertUserStringToXml:XMLList = new XMLList(xmlListString);
      				var usersOfGroupsXmlListCol:XMLListCollection = new XMLListCollection(convertUserStringToXml);
      
      				var sortA:Sort = new Sort();
      				sortA.fields= [new SortField(null, true)];
      				usersOfGroupsXmlListCol.sort=sortA;
          			usersOfGroupsXmlListCol.refresh();
      
      				myUserList.dataProvider = usersOfGroupsXmlListCol.child("userName").valueOf();
      				myUserList.validateNow();
      			}
      			
      			private function usersOfGroupLabelFunction(item:Object, uogDataGridCol:DataGridColumn):String
      			{
      				return item.text();
      			}
      Two things:
      1) Since this particular xml data was different in construct than all the other xml data that I was retrieving, then I had to treat it differently. Namely in that the other xml data had <sometag name="something " /> but this one was <sometag>someth ing</sometag> and apparently my Flex 3 no longer likes item.valueOf but item.text worked just fine. Go figure?
      2) I threw in a Sort function that I found somewhere on the net while trying to figure this out and I knew that if the sort function was working, then I had a true XMLListCollecti on.

      Thanx 4 giving me a sounding board

      Comment

      Working...