4 Replies Latest reply on Feb 4, 2008 10:23 AM by ntsiii

    Help with datagrid and xml

      Hey guys,
      i'm quite new to flex 2 and really need your help...
      I'm trying to build a simple app that reads an xml with the following tags:

      Now the reading part and displaying as a datagrid i did using the datagrid and i changed the headers of the columns using datagridcolumn headertext tag with no problem... now comes to hard part:
      I need to add a text input box that a user type in the id and the grid will only show the phone and ext of that specific user in the datagrid.. heres what i tried and failed:
      I created a textinput, defined a change event to go to a changehandler function written in AS that loops trough the array length and compare the id insereted with the <id> tag and if it found one it defines a var called index and then i created a labelfunction that returns that like namesArray.getItemAt(index).phone and in the datagrid the phone columns is set for that labelfunction.. the problem is that Flex ignores any changes in index var and always display the first phone tag in the xml...

      any help would be appriciated!
        • 1. Re: Help with datagrid and xml
          ntsiii Level 3
          Create a [Bindable] instance(global) var typed as XML, and bind the DG to that.
          [Bindable]private var _xmlSelected:XML

          In your TextInput change handler, use the the .text value in an e4x expression aginst the XML data:
          private function onChange(event:Event):void
          var sText:String = event.target.text;
          _xmlSelected = myXML.person.(id==sText)[ 0 ]; //[ 0 ]because e4x expressins always return an XMLList, and you want a node.

          That should do it.

          • 2. Re: Help with datagrid and xml
            ntsiii Level 3
            Also, DataGrid might not make sense for this use. Why use it with only a single row's worth of data?
            • 3. Re: Help with datagrid and xml
              Tropics2 Level 1
              Hey Tracy,
              Thanks for the reply but after sitting on it for like 3 hours yesterday i managed to solve it in a diffrent way ;)
              What i did was creating a datagrid and datagrid columns and giving the columns the datafield of the XML nodes without giving the datagrid itself the arraycollection as a dataprovider.
              Then when the user type the id in the text input box and the loop find the current nod it send it to the datagrid as dataprovider.
              For example if my array collection is called AC i set grid.dataprovider=AC (where i is the loop counter).
              Then some CSS styles to the whole app and it really looks and works great now
              • 4. Re: Help with datagrid and xml
                ntsiii Level 3
                Sure. If you use e4x xml, you can skip the conversion to ArrayCollection. It is a matter of taste, mostly.