7 Replies Latest reply on Aug 6, 2007 8:15 AM by Handycam

    Can't get datagrid selection

    Handycam Level 1
      i seem to be unable to get the selectedItems for this DataGrid:

      <mx:DataGrid id="choiceList" dataProvider="{_choices}" width="100%" height="100%" wordWrap="true" variableRowHeight="true">
      <mx:DataGridColumn dataField="@ln1" headerText="Field 1"/>

      i use trace("Choices ="+choiceList.selectedItems);

      but only get the Choices=

      If I use selectedIndices, I get the number of the item selected.

      What's up with that?
        • 1. Re: Can't get datagrid selection
          You have to specifiy the dataFields by name in your trace statement.

          For example:

          trace('Choices = ' +choiceList.selectedItem.@ln1);
          • 2. Can't get datagrid selection
            Handycam Level 1
            Thanks, makes sense.

            But then I try to assign it to a variable,

            public var _chosen0:Array;

            as in

            app._chosen0 = choiceList.selectedItems.@ln1; (note: the data grid has allowMultipleSelections)

            I get an error:

            ReferenceError: Error #1081: Property @ln1 not found on Array and there is no default value.
            • 3. Re: Can't get datagrid selection
              ntsiii Level 3
              You are getting confused.
              choiceList.selectedItems returns an array of objects, so you can't just trace it as in your first post.

              I think jfillman missed that you were using selectedItems(array), as his example uses selectedItem(object).

              To assign it to an array, do: app._chosen0 = choiceList.selectedItems;

              To look at the data, do somethng like:
              trace(app._chosen0[0].@In1 ); //this gets the first element of the array, and the In1 attribuet of the xml element.

              • 4. Re: Can't get datagrid selection
                Handycam Level 1
                Yeah, that's the first thing I had. Originally, it was a list control and it worked. But as a DG it's not. If I do exactly as you said:

                app._items0 = choiceList.selectedIndices;
                app._chosen0 = choiceList.selectedItems;

                And I select 2 items from my list
                I only get " , " and "3,5" in the console. So the selectedItems is empty. Selected indices is correct.

                That's why I'm confused.
                • 5. Re: Can't get datagrid selection
                  Handycam Level 1
                  Just to show you exactly whats going on, I build a new, simpler app from scratch. Try running this in debug mode, select an item from the grid and click the button. Trace shows nothing. Why?

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="getData.send()">
                  import mx.rpc.events.*;
                  private var myData:XMLList;
                  private var myResults:Array;
                  private function checkData(event:ResultEvent):void {
                  myData = event.result.stock;
                  private function getList(event:MouseEvent):void {
                  myResults = dg1.selectedItems;
                  <mx:DataGrid dataProvider="{myData}" id="dg1">
                  <mx:DataGridColumn headerText="Column 1" dataField="name"/>
                  <mx:DataGridColumn headerText="Column 2" dataField="author"/>
                  <mx:Button id="btn" label="ClickME" click="getList(event)"/>
                  <mx:HTTPService id="getData" url="books.xml" resultFormat="e4x" result="checkData(event)"/>

                  The XML:

                  <?xml version="1.0" encoding="iso-8859-1"?>

                  <name>The Picasso Code</name>
                  <author>Dan Blue</author>
                  <description>Cubist paintings reveal a secret society of people who really look like that</description>
                  <name>Here With the Wind</name>
                  <author>Margaret Middle</author>
                  <description>In this edition, nobody in the south really gives a damn</description>
                  <name>Harry Potluck and the Chamber of Money</name>
                  <author>J.K. Roughly</author>
                  <description>Young wizard finds the real pot-of-gold and retires</description>
                  • 6. Re: Can't get datagrid selection
                    chris.huston.t10 Level 3
                    You need to specify what you want to trace.
                    private function getList(event:MouseEvent):void {
                    myResults = dg1.selectedItems;

                    I tested your app with the above code and the console window displayed the selected items appropriately. The second trace outputs the name of the first item in the selectedItems array. Note that the first item in the array will be the last item that has been selected, not the first item selected.

                    If you are using selectedItems, it also makes sence to set your dataGrid to allowMulitpleSelctions='true'. Otherwise, you can just look at the selectedItem property which makes scripting a bit simpler.

                    • 7. Re: Can't get datagrid selection
                      Handycam Level 1