5 Replies Latest reply on Sep 6, 2010 7:37 AM by BhaskerChari

    Datagrid, Where Statement ?

    djh88ukwb Level 1

      Hello All.

       

      I have a datagrid that pulls all its data from a database.

       

      It has two colums - String and Visibile.

       

      I would like to create a function, that acts as a "Where" statement.  Its function should look at the datagrid and set the value of a label.text to the String from the datagrid whos Visible value is yes.  Only one string can have a visible value of Yes, the rest are no.

       

      So basically all its got to do is look thru the datagrid and set the text value of a label to the string of the item whos visible = Yes.

       

      Im not sure if this can be done with a "Where" statement.

       

      I have tried looking about on Google, but no luck.

       

      Im not sure if i have explained this well enough.  Let me know if i havent.

       

      Thanks

        • 1. Re: Datagrid, Where Statement ?
          BhaskerChari Level 4

          Hi djh88ukwb,

           

          There is no such as where what you're talking but the idea and the functionality you required can be done...by specifying a labelFunction for your DataGrid column. Look at Flex help manual how to write a labelFunction for your DataGrid its very simple..

           

          private function myLabelFunction(item:Object, column:DataGridColumn):String
          {

               //Write your check and logic here
          }

           

          <mx:DataGridColumn headerText="Totals" labelFunction="myLabelFunction" dataField="Totals"/>

           

          Thanks,

          Bhasker

          1 person found this helpful
          • 2. Re: Datagrid, Where Statement ?
            djh88ukwb Level 1

            Hmm, im not sure this will do what i want to achieve.

             

            The application i am trying to develop, takes in an array from a database, of String aswell as a value of either Yes or No.  There can only be one string in the array with the value of Yes.

             

            The values in the database are put in by a lot of people.  And the database picks a random String to have the value of Yes.

             

            So the flex function needs to look thru the Array and see which String has the value of Yes, and then take the String value and put it as the Label text value.

             

            Would a do while / if statement do the trick ?  The only thing is, the lenght of the array can vary, it may be 5 records in lenght, or it may be 500.

             

            Any help would be great, again thanks for your reply, but after looking on Google i dont think that is what im after.

            • 3. Re: Datagrid, Where Statement ?
              BhaskerChari Level 4

              Hi djh88ukwb,

               

              If you can post the sample code depicting your problem that way it could be more reasonable and helpful to resolve your problem.

               

               

              Thanks,

              Bhasker

              • 4. Re: Datagrid, Where Statement ?
                djh88ukwb Level 1

                Hello,  Thanks again for the reply.

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                               xmlns:s="library://ns.adobe.com/flex/spark"
                               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
                    <fx:Declarations>
                        <!-- Place non-visual elements (e.g., services, value objects) here -->
                    </fx:Declarations>
                   
                    <fx:Script>
                        <![CDATA[
                            import mx.collections.ArrayCollection;
                            import mx.collections.XMLListCollection;
                            import mx.controls.Alert;
                            import mx.events.FlexEvent;
                            import mx.events.ListEvent;
                            import mx.messaging.ChannelSet;
                            import mx.messaging.channels.AMFChannel;
                            import mx.messaging.events.MessageAckEvent;
                            import mx.messaging.events.MessageEvent;
                            import mx.messaging.messages.AsyncMessage;
                            import mx.rpc.events.FaultEvent;
                            import mx.rpc.events.ResultEvent;
                            import mx.rpc.xml.SimpleXMLEncoder;
                            import mx.utils.ObjectUtil;
                            import org.osmf.events.TimeEvent;       
                            import spark.events.TextOperationEvent;
                           
                            [Bindable] private var StringArr:ArrayCollection = new ArrayCollection(
                                    [{id:"1", string:"String 1", vis:"no"},
                                    {id:"2", string:"String 2", vis:"no"},
                                    {id:"3", string:"String 3", vis:"no"},
                                    {id:"2", string:"String 4", vis:"no"},
                                    {id:"2", string:"String 5", vis:"yes"},
                                    {id:"2", string:"String 6", vis:"no"},]);
                           
                            protected function getString():void
                            {
                               
                            }
                        ]]>
                    </fx:Script>
                   
                   
                   
                   
                    <mx:DataGrid x="10" y="34" dataProvider="{StringArr}" editable="true">
                        <mx:columns>
                            <mx:DataGridColumn headerText="ID" dataField="id"/>
                            <mx:DataGridColumn headerText="String" dataField="string"/>
                            <mx:DataGridColumn headerText="Vis" dataField="vis"/>
                        </mx:columns>
                    </mx:DataGrid>
                    <s:Label x="10" y="184" id="VisString" width="210"/>
                    <s:Button x="237" y="184" label="Get String" click="getString()"/>
                </s:Application>

                 

                here is my code, for the sake of the example, i have removed the connection to the database and just added an array into the code.  But in real the array is pulled from the database.

                 

                Now what i want, when the user clicks the "Get String" button, i want the function "getString" to look at the datagrid's array and find the String that has the value vis:Yes.  I then want it to return the String associated with that into the text value of VisString.

                 

                So for example, when you click "Get String" the function sees that string 5 has the visible Yes, so the text value of  VisString should change to "String 5"

                 

                Hope you now understand what  i mean ?

                • 5. Re: Datagrid, Where Statement ?
                  BhaskerChari Level 4

                  Hi ,

                   

                  Just replace this function with this one and run the application...

                   

                  protected function getString():void
                              {
                               for(var intLoop:int=0;intLoop<StringArr.length-1;intLoop++)
                               {
                                if(StringArr.getItemAt(intLoop).vis == "yes")
                                {
                                 VisString.text = StringArr.getItemAt(intLoop).string; 
                                }
                               }
                              }

                   

                   

                  Thanks,

                  Bhasker