6 Replies Latest reply on Mar 20, 2007 12:41 PM by ntsiii

    DataGrid and Boolean

    happydevs
      Hi everybody,

      I have 2 items in the datagrid coming from a PHP file through AMFPHP
      One of their value is supposed to be Bolean. The first is 0 and the second is 1

      In the DataGrid, both display the value true in my application
      while they have a correct value of 0 and 1 in the AMFPHP browser.

      What can be wrong in my code?
      quote:

      <mx:RemoteObject id="myservice" source="edifice.ProjetService" destination="amfphp" fault="faultHandler(event)" showBusyCursor="true">
      <mx:method name="getList" result="getListHandler(event)" fault="faultHandler(event)" />
      </mx:RemoteObject>


      <mx:Button x="290" y="357" label="get liste" click="myservice.getOperation('getList').send();"/>
      <mx:DataGrid x="10" y="10" width="745" id="projets_list" dataProvider="{dp}" change="changeHandler(event)">
      <mx:columns>
      <mx:DataGridColumn headerText="active" dataField="active"/>
      </mx:columns>

      <mx:Script>
      <![CDATA[

      [Bindable]
      private var dp:ArrayCollection;

      private function getListHandler(evt:ResultEvent):void
      {
      dp = new ArrayCollection( ArrayUtil.toArray(evt.result) );
      }

      }
      ]]>
      </mx:Script>


      Thank you very much for any help
      Julie
        • 1. Re: DataGrid and Boolean
          Brandon Ellis Level 1
          Hi,
          Make sure your remote values sent in are being sent as their correct data types. I did a little test with your code and the only way I could make it assign 'true' for both values was when the values looked like Bools but were actually Strings.

          1 and 0 passed in are integers coerced to Boolean and will return true and false.
          "1" and "0" passed in are Strings and will be coerced to Boolean but will both display true.

          If you check to see what data types you actually have, you should be fine.

          good luck.
          • 2. Re: DataGrid and Boolean
            ntsiii Level 3
            And you can use a labelFunction to format the data as you wish.
            Tracy
            • 3. Re: DataGrid and Boolean
              happydevs Level 1
              Hi again,

              Thank you for your replies, they brought me a step further.

              So I used labelFunction to transform my data

              and my variable is declared as a Boolean in the Class

              quote:

              package edifice
              {
              [RemoteClass(alias="edifice.Projet")]
              [Bindable]
              public class Projet
              {

              public var active:Boolean;

              }
              }



              Now, if I write the labelFunction like this :
              quote:

              var b:Boolean= Boolean (str_data);
              return b + " test";


              It works well



              but if I ask
              quote:

              var b:Boolean= Boolean (str_data);
              return b ;

              Like I need it to appear in the dataGrid, I have a
              Implicit forcing of Boolean to a different datatype String
              error (I'm not sure of the english version at all)


              Is this something with the way the dataGrid receives the data?

              What do you think can be my mistake?


              This is the code :
              quote:


              package edifice
              {
              [RemoteClass(alias="edifice.Projet")]
              [Bindable]
              public class Projet
              {

              public var active:Boolean;

              }
              }

              <mx:RemoteObject id="myservice" source="edifice.ProjetService" destination="amfphp" />
              <mx:method name="getList" result="getListHandler(event)" fault="faultHandler(event)" />
              </mx:RemoteObject>

              <mx:DataGrid x="10" y="10" width="745" id="projets_list" dataProvider="{dp}" change="changeHandler(event)">
              <mx:columns>
              <mx:DataGridColumn headerText="active" dataField="active" labelFunction="genericLabelFunction" />
              </mx:columns>
              </mx:DataGrid>


              <mx:Script>
              <![CDATA[


              [Bindable]
              private var dp:ArrayCollection;

              private function getListHandler(evt:ResultEvent):void
              {
              dp = new ArrayCollection( ArrayUtil.toArray(evt.result) );
              }

              private function genericLabelFunction(obj_data:Object, obj_dataGridColumn:DataGridColumn):String
              {
              var b:Boolean= Boolean (str_data);
              return b + " test";
              //return b ;
              }


              ]]>
              </mx:Script>


              Thank you for any help,
              Julie
              • 4. Re: DataGrid and Boolean
                ntsiii Level 3
                labelFunction myust return a String.

                Maybe:
                var b:Boolean= Boolean (str_data);
                var sReturn:String = (b) ? "1" : "0";
                return sReturn ;

                • 5. DataGrid and Boolean
                  happydevs Level 1
                  Thank you,

                  It returned '1' in each case

                  It's not a solution, but I declared the var as a Number everywhere.

                  Then I had a warning "Number was used were a Boolean was expected", so I changed it here :
                  quote:

                  <mx:CheckBox id="active" selected="{Boolean(selectedProjet.active)}" click="alertInactive()" />



                  So finally can we, or not, use a Boolean in a dataGrid ?

                  Thank you for your help, I learned a lot.

                  Julie
                  • 6. Re: DataGrid and Boolean
                    ntsiii Level 3
                    Yes, you can.

                    Actually, looking at your labelFunction again, you never set the variable str_data. you never even declare it, so something is way wrong. Where is that value comming from? if it is just a typo, then you need to specify which property of the obj_data contains the value you want.

                    It should be something like:
                    private function genericLabelFunction(obj_data:Object, obj_dataGridColumn:DataGridColumn):String
                    {
                    var b:Boolean= Boolean (obj_data.myBooleanProperty); //<<== note this!
                    return b + " test";
                    //return b ;
                    }

                    Tracy