4 Replies Latest reply on May 30, 2007 9:04 AM by oxide_reaction

    Load info from DataGrid

    oxide_reaction
      Hi,

      I'm new to flex and still learning how to do a lot of things.

      What I have is a DataGrid that is populated by an xml document. What I need to happen is when the user double clicks on an entry, it takes the information in the entry they selected and populates a text field.

      Here's the code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" horizontalAlign="center" verticalAlign="middle" initialize="retrieveEntriesService.send();">

      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var eventData:ArrayCollection;

      private function eventResultHandler(event:ResultEvent):void {
      eventData = event.result.entries.entry;
      }
      ]]>
      </mx:Script>

      <mx:Script>
      <![CDATA[
      private function doubleclickedEvent():void {
      thePages.selectedChild=editEntry;

      }
      ]]>
      </mx:Script>

      <mx:HTTPService id="retrieveEntriesService" url="data/entries.xml" result="eventResultHandler(event)"/>

      <mx:ViewStack id="thePages" resizeToContent="true">

      <mx:Panel id="main" label="Main" title="Main Menu" showEffect="Iris" hideEffect="Iris">
      <mx:VBox horizontalAlign="center" width="500">
      <mx:HBox horizontalAlign="center" width="100%">
      </mx:HBox>
      <mx:DataGrid width="100%" dataProvider="{eventData}" draggableColumns="false" sortableColumns="false" allowMultipleSelection="true"
      resizableColumns="false" doubleClickEnabled="true" doubleClick="doubleclickedEvent()">
      <mx:columns>
      <mx:DataGridColumn headerText="Entries" dataField="title"/>
      <mx:DataGridColumn headerText="Date" dataField="date"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:VBox>
      </mx:Panel>

      <mx:Panel id="editEntry" label="Edit Entry" title="Edit Entry" showEffect="Iris" hideEffect="Iris">
      <mx:VBox>
      <mx:Form>
      <mx:FormItem label="Title">
      <mx:TextInput id="editTitle"/>
      </mx:FormItem>
      <mx:FormItem label="Entry">
      <mx:RichTextEditor id="editBody">
      </mx:RichTextEditor>
      <mx:DateField id="editDate"/>
      </mx:FormItem>
      <mx:HBox width="100%" horizontalAlign="center" height="40" verticalAlign="bottom">
      <mx:Button label="Save" click="thePages.selectedChild=main"/>
      <mx:Button label="Cancel" click="thePages.selectedChild=main"/>
      </mx:HBox>
      </mx:Form>
      </mx:VBox>
      </mx:Panel>

      </mx:ViewStack>

      </mx:Application>

      and the xml document looks like

      <entry>
      <title>entry 1</title>
      <body>
      Velit dolor vel facilisis luptatum lorem, augue iusto, et ex augue esse lobortis. Dolore luptatum lobortis ipsum et illum duis delenit consequat illum, minim, wisi. Dolore duis molestie, tincidunt ullamcorper duis ex amet iusto dolore ut nisl vel ex exerci minim nulla, nulla euismod vel.
      </body>
      <date>02/17/07</date>
      </entry>
      <entry>
      <title>entry 2</title>
      <body>Velit dolor vel facilisis luptatum lorem, augue iusto, et ex augue esse lobortis. Dolore luptatum lobortis ipsum et illum duis delenit consequat illum, minim, wisi. Dolore duis molestie, tincidunt ullamcorper duis ex amet iusto dolore ut nisl </body>
      <date>01/19/07</date>
      </entry>

      Thanks

      (totally unrelated but why doesn't Adobe have a code tag for forum entries?)
        • 1. Re: Load info from DataGrid
          BLXWebMaster Level 1
          If you want to capture the data grid selected item and simply place it in a text field.

          Assign your DataGrid an ID; "dgSample"

          <![CDATA[
          private function doubleclickedEvent():void {
          thePages.selectedChild=editEntry;

          TextFieldName.text = dgSample.selectedItem.title;

          }
          • 2. Re: Load info from DataGrid
            oxide_reaction Level 1
            BLXWebMaster,

            I tried what you said but now I get an error:

            TypeError: Error #1009: Cannot access a property or method of a null object reference.
            at test2/::doubleclickedEvent()
            at test2/__myDataGrid_doubleClick()


            any ideas?

            Thanks
            • 3. Re: Load info from DataGrid
              ctzn99 Level 1
              assuming you've assigned your datagrid the id "dgSample" then change

              TextFieldName.text = dgSample.selectedItem.title;

              to

              editTitle.text = dgSample.selectedItem.title;

              It didn't work because in your sample there is no object named "TextFieldName". BLXWebMaster was most likely just using that name variable name as an example, not an absolute.
              • 4. Re: Load info from DataGrid
                oxide_reaction Level 1
                quote:

                Originally posted by: ctzn99
                assuming you've assigned your datagrid the id "dgSample" then change

                TextFieldName.text = dgSample.selectedItem.title;

                to

                editTitle.text = dgSample.selectedItem.title;

                It didn't work because in your sample there is no object named "TextFieldName". BLXWebMaster was most likely just using that name variable name as an example, not an absolute.



                When I followed BLXWebMaster's directions, I had changed the variable to "editTitle" before I ran it. Thanks for the help but that's not the problem.