7 Replies Latest reply on Jan 19, 2007 10:12 AM by peterent

    I've searched to no avail

    Hodgetr
      Ok so, I have a DataGrid that is binded to some XML data. The last column in the grid renders a Link button which when clicked should change the view state to the selected index's XML property I have set that corresponds to a view state i have already named. Here's a few lines...

      First I have this number to keep track of the selected item...
      <mx:Number id="selectedMapIndex">{myMapGrid.selectedIndex}</mx:Number>

      here's the code for the last column...
      <mx:DataGridColumn dataField="scrims" headerText="Goto Strat" width="50">
      <mx:itemRenderer>
      <mx:Component>
      <mx:LinkButton label="Strat" click="currentState='{mapInfo.map[selectedMapIndex].mapStrat}';" width="50"/>
      </mx:Component>
      </mx:itemRenderer>
      </mx:DataGridColumn>

      Again any help you can provide would be awesome. "mapInfo" is the xml object name, "map" is the item, "mapStrat" contains the text in xml that is identical to the name of my states.
        • 1. Re: I've searched to no avail
          peterent Level 2
          When you work with in-line itemRenderers using <mx:Component> its as if the MXML and ActionScript were in another file. That is, they have their own scope. You cannot simply refer to variables in the file. So within the <mx:Component>...</mx:Component> mapInfo et al have no meaning.

          You need to use outerDocument:

          outerDocument.currentState='{outerDocument.map[outerDocument.selectedMapIndex... etc etc

          Here's an easier way. The "click" event is a bubbling event. Meaning that if you don't catch it within the <mx:Component> it will go up to the next component, the DataGrid. So you can do this:

          <mx:DataGrid click="currentState={mapInfo.map[selectedMapIndex].mapStrat}" ... >

          The trouble with this approach is that ANY click within the DataGrid will be caught. So you need to test the event. The better thing to do is put the event handler into a function:

          <mx:DataGrid click="handleClick(event)" ... >

          private function handleClick( event:Event ) : void {
          if( event.currentTarget is LinkButton ) { /* put here your currentState = ... code */
          }
          }

          You'll have to test it and get any kinks out since I just wrote that off the top of my head.

          --peter
          • 2. Re: I've searched to no avail
            Hodgetr Level 1
            Interesting, I'm going to try that, I agree that handling an event when someone clicks anywhere on the table is a bad idea. However would it be possible to do something like say giving each XML object a id that is identical to the name of my view states then I could reference them like..

            <mx:DataGrid click="handleClick(event)" ... >

            private function handleClick( event:Event ) : void {
            if( event.currentTarget is LinkButton ) {
            currentState='{mapInfo.map[selectedMapIndex].@id}';
            }
            -----
            This would be the best solution that I can see, I don't think I would need to use the attribute() method to get the property would I? Thank you for your help. I love flex (comming from ASP/C#), and I don't think I'll ever use anything else, unless I get bored. :) Thanks again!
            • 3. Re: I've searched to no avail
              Hodgetr Level 1
              errors..

              When I attempt to use the 3rd method you described the if statement in the handleClick function says that...
              "1120: Access of undefined property LinkButton. "

              Here is the code in question... I do have a LinkButton rendered as a component in the datagrid.

              private function handleClick( event:MouseEvent ) : void {
              if( event.currentTarget is LinkButton ){
              /* put here your currentState = ... code */
              currentState='mapInfo.map[selectedMapIndex].@id';
              }
              }
              • 4. Re: I've searched to no avail
                peterent Level 2
                Having the id or some other attribute be the new state would be fine. But your syntax is incorrect. You don't want quotes and braces as that is MXML syntax; you want ActionScript syntax:
                currentState = mapInfo.map[selectedMapIndex].@id;

                You will also need to import the LinkButton class into your Script block:

                import mx.controls.LinkButton;

                Even though the LinKButton tag appears in the file, you need to import the class for the ActionScript code.
                • 5. Re: I've searched to no avail
                  Hodgetr Level 1
                  Still nothing happens... I fixed the syntax and I do have the LinkButton control imported. Does the datagrid have a click attribute that I can set? It doesn't seem to respond to and clicks.

                  <mx:DataGrid id="myMapGrid" dataProvider="{mapInfo.map}" click="handleClick(event)"/>
                  ...
                  <mx:DataGridColumn dataField="goto" headerText="Goto Strat" width="50">
                  <mx:itemRenderer>
                  <mx:Component>
                  <mx:LinkButton width="50" label="Strat" />
                  </mx:Component>
                  </mx:itemRenderer>
                  </mx:DataGridColumn>
                  ...
                  Here is the function..
                  private function handleClick(event:MouseEvent):void
                  {
                  if(event.currentTarget is LinkButton){
                  currentState=mapInfo.map[selectedMapIndex].@id;
                  }
                  }
                  ---

                  I don't think the function is even being called when I click on the LinkButton in the datagrid...
                  • 6. Re: I've searched to no avail
                    Hodgetr Level 1
                    Hmm, seems to work without the if statement in the function. So it is calling the function but once it hits the if statement it evaluates to false, even when I click on the LinkButton itself.
                    • 7. Re: I've searched to no avail
                      peterent Level 2
                      Set a breakpoint in the function with the debugger and examine the contents of the event parameter. You might want target instead of currentTarget.