Instead of keeping your dataprovider as an XMLList, which doesn't
inherently have any binding capabilities, convert the XMLList to an
ArrayCollection in your result handler function.  Then you can add
items, etc., and the grid will update as you would expect.

Jeff

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of mr_delphi_developer
Sent: Tuesday, May 06, 2008 11:23 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Using XMLList as dataprovider to DataGrid and
Adding rows



 In the code below, I'm having trouble figuring out how to add rows to
the grid. I have found only one tutorial and using it's techniques
doesn't seem to work.

Can anyone help out here?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute" initialize="initApp();">

        <mx:Script>
                <![CDATA[
                        import mx.rpc.events.ResultEvent;
                        import mx.rpc.events.FaultEvent;
                        import mx.controls.Alert;

     [Bindable]
     private var rosterInfo:XMLList;

     public function handleXML(event:ResultEvent):void
     {
       rosterInfo = event.result.option as XMLList;
     }

     public function handleFault(event:FaultEvent):void
     {
       Alert.show(event.fault.faultString, "Error");
     }

     public function initApp():void
     {
       if (ExternalInterface.available)
         ExternalInterface.addCallback("addPlayer", addPlayer);
     }

                        public function addPlayer(player_name:String,
jersey_: String,
age_:String):void
     {
       (RosterList.dataProvider as XMLList).addItem(
         {playerid: 0, playername: player_name, jersey: jersey_, age:
age_}
       );
     }
   ]]>
        </mx:Script>
  <mx:HTTPService result="handleXML(event);"
                    fault="handleFault(event);"
                    id="xmlRPC"
                    resultFormat="e4x"
                  url="flexget_roster.php"
                  useProxy="false">
           <mx:request xmlns="">
      <teamid>{teamid.text}</teamid>
             <teampin>{teampin.text}</teampin>
           </mx:request>
        </mx:HTTPService>
        <mx:Label x="56" y="32" text="Team ID" width="55" height="18"
                  textAlign="right" fontWeight="bold"/>
        <mx:Label x="56" y="58" text="TeamPin" width="55" height="18"
                  textAlign="right" fontWeight="bold"/>
        <mx:TextInput x="130" y="32" id="teamid" width="160"
                      height="22"/>
        <mx:TextInput x="130" y="58" id="teampin" width="160"
                      height="22"/>
        <mx:Button x="130" y="95" label="Get Roster"
                   click="xmlRPC.send();" width="160" height="22"/>
        <mx:DataGrid dataProvider="{rosterInfo}"
                     x="80"
                     y="141"
                     width="262"
                     height="306"
                     id="RosterList"
                     enabled="true"
                     editable="true" >
          <mx:columns>
            <mx:DataGridColumn headerText="playerid"
                         dataField="playerid"
                         visible="false"/>
      <mx:DataGridColumn headerText="Player Name"
                         dataField="playername" />
      <mx:DataGridColumn headerText="Jersey"
                         dataField="jersey"
                         width="60"/>
      <mx:DataGridColumn headerText="Age"
                         dataField="age"
                         width="40"/>
    </mx:columns>
  </mx:DataGrid>
</mx:Application>

mySQL data:

#
# Table structure for table 'players'
#

CREATE TABLE `players` (
  `playerid` int(11) NOT NULL auto_increment,
  `teamid` int(11) NOT NULL,
  `jersey` varchar(3) default NULL,
  `playername` varchar(255) NOT NULL,
  `age` tinyint(4) default NULL,
  KEY `playerid` (`playerid`)
) ENGINE=MyISAM AUTO_INCREMENT=116 DEFAULT CHARSET=latin1;

#
# Dumping data for table 'players'
#

INSERT INTO `players` (`playerid`, `teamid`, `jersey`, `playername`,
`age`) VALUES
  ( 1,5,'34','Nolan Ryan',8),
  ( 2,5,'21','Roger Clemens',8),
  ( 3,5,'51','Randy Johnson',9),
  ( 5,5,'40','David Wells',12),
  ( 6,5,'56','Brad Lidge',15),
  ( 7,5,'55','Chad Qualls',9),
  ( 8,5,'17','Roy Oswalt',10),
  ( 9,5,'46','Andy Pettitte',11),
  (10,5,'26','Mariano Rivera',13),
  (11,6,'34','Nolan Ryan',9),
  (12,6,'21','Roger Clemens',10),
  (13,6,'2', 'Randy Johnson',13),
  (14,6,'42','Bob Gibson',12),
  (15,6,'19','David Wells',13),
  (16,6,'4', 'Brad Lidge',12),
  (17,6,'55','Chad Qualls',15),
  (18,6,'68','Roy Oswalt',16),
  (19,6,'41','Andy Pettitte',13),
  (20,6,'26','Mariano Rivera',9),

#
# Table structure for table 'teams'
#

CREATE TABLE `teams` (
  `id` int(1) NOT NULL auto_increment,
  `teamname` varchar(20) NOT NULL default '',
  `division` char(2) NOT NULL default '',
  `email` varchar(45) NOT NULL default '',
  `active` tinyint(4) NOT NULL default '1',
  `PIN` int(4) NOT NULL default '1990',
  `link` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `teamname` (`teamname`)
) ENGINE=MyISAM AUTO_INCREMENT=32 DEFAULT CHARSET=latin1;

#
# Dumping data for table 'teams'
#

INSERT INTO `teams` (`id`, `teamname`, `division`, `email`, `active`,
`PIN`, `link`) VALUES
  (1,'Blue Jays','AA','[EMAIL PROTECTED]',1,1990,NULL),
  (2,'Orioles','AA','[EMAIL PROTECTED]',1,2000,NULL),
  (3,'Yankees','AA','[EMAIL PROTECTED]',1,2010,NULL),
  (4,'Penguins','A','[EMAIL PROTECTED]',1,2160,NULL),
  (5,'Roadrunners','A','[EMAIL PROTECTED]',
1,2170,NULL),
  (6,'Cardinals','A','[EMAIL PROTECTED]',1,2180,NULL),
  (7,'OPEN','','',1,1990,NULL);

HTML page to allow adding of Players to Roster:

<html>
<body>
<script language="javascript">
//<!--

function addPlayer()
{
  var player_name = document.getElementById('player_name').value;
  var age_ = document.getElementById('age_').value;
  var jersey_ = document.getElementById('jersey_').value;
  getFlexApp('RosterInfo').addPlayer(player_name, jersey_, age_); }

//-->
</script>
<center>
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
                id="RosterInfo" width="421" height="561"
               
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/
swflash.cab">
                <param name="movie" value="RosterEdit.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#869ca7" />
                <param name="allowScriptAccess" value="always" />

                <embed src="RosterEdit.swf" quality="high"
bgcolor="#869ca7"
                                width="421" height="561"
name="RosterInfo" align="middle"
                                play="true"
                                loop="false"
                                quality="high"
                                allowScriptAccess="always"
                                type="application/x-shockwave-flash"
                               
pluginspage="http://www.adobe.com/go/getflashplayer";>
                </embed>
  </object>
  <br>
  <br>
  <table class="Roster">
    <tr>
      <td>
        Enter Data then Click on the Add Player button
      </td>
    </tr>
    <tr>
      <td>
        <table style="border-spacing:5px;" width="100%">
          <tr>
            <td style="border-style:none;padding:0px;">Player Name:</
td>
            <td style="border-style:none;padding:0px;">
              <input id="player_name" type="text" />
            </td>
          </tr>
          <tr>
            <td style="border-style:none;padding:0px;">Jersey Number:</
td>
            <td style="border-style:none;padding:0px;">
              <input id="jersey_" type="text" />
            </td>
          </tr>
          <tr>
            <td style="border-style:none;padding:0px;">Age:</td>
            <td style="border-style:none;padding:0px;">
              <input id="age_" type="text" />
            </td>
          </tr>
          <tr>
            <td colspan="2" style="border-style:none;padding:0px;">
              <input type="button" id="butAddPlayer"
onclick="addPlayer()" value="Add Player" />
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
  </center>
</body>
</html>

PHP source for flexroster.php:
<?php
  function get_flex_roster($teamid, $teampin) {
    include("/home/dbconnect.inc"); // has $dbuser, $dbpw, etc values...
    mysql_connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
    $pinsql = "SELECT PIN from teams WHERE id = $teamid";
    $pinquery = mysql_query($pinsql);
    if ((mysql_num_rows($pinquery) == 0) || (mysql_result($pinquery,
0) <> $teampin)) {
      $options = array("You did not enter a valid PIN for the team
selected, try again.");
    } else {
      $teamsql = "SELECT playerid, playername, jersey, age FROM players
WHERE teamid = $teamid ORDER BY jersey, age";

      $teamquery = mysql_query($teamsql);
      $str = "";
      $i = 0;
      if (mysql_num_rows($teamquery) > 0) {
        while ($result = mysql_fetch_assoc($teamquery)) {
          $options[] = $result;
        }
      } else {
        $options = array("There are no players for this team, to add
players, click the Add Player button.");
      }
    }
    return $options;
  }
?>

PHP source for flex_getroster.php
<?php
  require('flexroster.php');
  $options = get_flex_roster($_REQUEST["teamid"],
$_REQUEST["teampin"]);
  $results[] = "<options>";
  foreach ($options as $roster)
  {
  $results[] = "<option>
                  <playerid>" . $roster['playerid'] . "</playerid>
                  <playername>" . $roster['playername'] . "</
playername>
                  <jersey>" . $roster['jersey'] . "</jersey>
                  <age>" . $roster['age'] . "</age>
                </option>";
  }
  $results[] = "</options>";
  print implode("\n", $results);
?> 


------------------------------------

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups
Links



Reply via email to