Professional Web Applications Themes

Selecting Item(s) on a DataGrid - Macromedia Flex General Discussion

Simple questions, couldn't find a simple answer however. I want to be able to select and item (and possibly later items) on a datagrid from the click of a button. I've tried set the selectedIndex property on the datagrid, but this does nothing. Does anyone have a solution to this? Thanks Jaz...

  1. #1

    Default Selecting Item(s) on a DataGrid

    Simple questions, couldn't find a simple answer however. I want to be able to
    select and item (and possibly later items) on a datagrid from the click of a
    button. I've tried set the selectedIndex property on the datagrid, but this
    does nothing. Does anyone have a solution to this?

    Thanks
    Jaz

    Jaz Chana Guest

  2. #2

    Default Re: Selecting Item(s) on a DataGrid

    Both selectedIndex and selectedIndices should work:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">

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

    var gridData:ArrayCollection = new ArrayCollection(
    new Array(
    {id:123, company:'Microsoft'},
    {id:456, company:'Adobe'},
    {id:789, company:'Yahoo!'}
    )
    );
    ]]>
    </mx:Script>

    <mx:Button label="Select Row(s)" click="dg.selectedIndices=[0, 1, 2]"/>

    <mx:DataGrid id="dg" dataProvider="{this.gridData}"
    allowMultipleSelection="true">
    <mx:columns>
    <mx:DataGridColumn headerText="ID" dataField="id"/>
    <mx:DataGridColumn headerText="Company" dataField="company"/>
    </mx:columns>
    </mx:DataGrid>

    </mx:Application>


    atta707 Guest

  3. #3

    Default Re: Selecting Item(s) on a DataGrid

    This code seem to work:



    <?xml version="1.0"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
    <![CDATA[
    private function clickHandler(e:Event):void{
    if(e.currentTarget == btn1){
    dg.selectedItems = [dg.dataProvider[0]];
    }
    if(e.currentTarget == btn2){
    dg.selectedItems = [dg.dataProvider[1]];
    }
    if(e.currentTarget == btn3){
    dg.selectedItems = [dg.dataProvider[0],dg.dataProvider[1]];
    }
    }
    ]]>
    </mx:Script>
    <mx:DataGrid id="dg" width="500" allowMultipleSelection="true">
    <mx:ArrayCollection>
    <mx:Object>
    <mx:Artist>Pavement</mx:Artist>
    <mx:Price>11.99</mx:Price>
    <mx:Album>Slanted and Enchanted</mx:Album>
    </mx:Object>
    <mx:Object>
    <mx:Artist>Pavement</mx:Artist>
    <mx:Album>Brighten the Corners</mx:Album>
    <mx:Price>11.99</mx:Price>
    </mx:Object>
    </mx:ArrayCollection>
    </mx:DataGrid>
    <mx:Button id="btn1" label="Select 'Slanted and Enchanted'"
    click="clickHandler(event)"/>
    <mx:Button id="btn2" label="Select 'Brighten the Corners'"
    click="clickHandler(event)"/>
    <mx:Button id="btn3" label="Select BOTH" click="clickHandler(event)"/>
    </mx:Application>

    Greg Lafrance Guest

  4. #4

    Default Re: Selecting Item(s) on a DataGrid

    Both those solutions worked on their own, but when I tried to integrate them
    with my current project I couldn't seem to get them to work. Until I moved my
    dg.selectedIndex statement underneath the currentState=null; statement. For
    some reason, i'm guessing because it resets the view, that statement was
    preventing the selectedIndex to be set.

    Jaz Chana Guest

Similar Threads

  1. Selecting item in listbox by data
    By FlashAsh99 in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: February 8th, 02:26 AM
  2. Replies: 2
    Last Post: February 5th, 12:11 AM
  3. Replies: 6
    Last Post: January 17th, 07:46 PM
  4. Selecting row in datagrid by clicking everyway on the item
    By Liuc in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: November 14th, 08:11 AM
  5. Replies: 1
    Last Post: September 22nd, 04:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139