How to avoid NaN in Datagrid?

133 views
Skip to first unread message

vikram.rk

unread,
Aug 8, 2007, 4:30:08 AM8/8/07
to Flex India Community
Hi,

I have a datagrid whose data provider is an array of the class defined
below:

class MyData
{
public var name : String;
public var percentMarks : Number;
}

When I get this data from the server (through a web service), some of
the "percentMarks" will be null and this is coming up in the datagrid
as NaN. Is there a way to avoide this?

Thanks
Vikram

Raghunath Rao

unread,
Aug 8, 2007, 4:49:17 AM8/8/07
to flex_...@googlegroups.com
Hi Vikram,

Just over the top what i can think of is using getters
and setters. This
should do the trick...

class MyData
{
private var _name : String;
private var _percentMarks : Number;

public function set percentMarks(value:Number){
if(value){ _percentMarks = value;}
else { _percentMarks = 0;}
}

public function set name(value:String){
_name = value;
}


public function get percentMarks(){
return _percentMarks;
}

public function get name(){
return _name;
}


}


Its always a best practice to have private properties
in a class and
expose them as getters and setters.
Are you able to display the names properly?? Its only
the numbers which
are causing problems right?

Thanks
Raghu



____________________________________________________________________________________Ready for the edge of your seat?
Check out tonight's top picks on Yahoo! TV.
http://tv.yahoo.com/

raghuonflex.vcf

Raghunath Rao

unread,
Aug 8, 2007, 5:06:07 AM8/8/07
to flex_...@googlegroups.com
Hi Vikram,

Sorry... I forgot the return types for the getters

class MyData
{
private var _name : String;
private var _percentMarks : Number;

public function set
percentMarks(value:Number):void{


if(value){ _percentMarks = value;}
else { _percentMarks = 0;}
}

public function set name(value:String):void{
_name = value;
}


public function get percentMarks():Number{
return _percentMarks;
}

public function get name():String{
return _name;
}


}

Thanks
Raghu



____________________________________________________________________________________Ready
for the edge of your seat?
Check out tonight's top picks on Yahoo! TV.
http://tv.yahoo.com/

____________________________________________________________________________________
Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/

raghuonflex.vcf

vikram.rk

unread,
Aug 8, 2007, 5:58:46 AM8/8/07
to Flex India Community
Hi Raghu,

Thanks for the reply. I have no issues with the names.

Having getters and setters for Number wont solve my problem. I cant
have 0 as a default value, as 0 is a valid "percentMarks" for me. I
have to show the actual value, if exists, else show nothing.

Is there a way to achieve this in datagrid?

Thanks
Vikram

On Aug 8, 2:06 pm, Raghunath Rao <raghuonf...@yahoo.com> wrote:
> Hi Vikram,
>
> Sorry... I forgot the return types for the getters
>
> class MyData
> {
> private var _name : String;
> private var _percentMarks : Number;
>
> public function set
> percentMarks(value:Number):void{
> if(value){ _percentMarks = value;}
> else { _percentMarks = 0;}
> }
>
> public function set name(value:String):void{
> _name = value;
> }
>
> public function get percentMarks():Number{
> return _percentMarks;
> }
>
> public function get name():String{
> return _name;
> }
>
> }
>
> Thanks
> Raghu
>

> ___________________________________________________________________________­_________Ready


> for the edge of your seat?
> Check out tonight's top picks on Yahoo! TV.http://tv.yahoo.com/
>
> ___________________________________________________________________________­_________
> Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.http://autos.yahoo.com/green_center/
>

> raghuonflex.vcf
> 1KDownload

vikram.rk

unread,
Aug 8, 2007, 6:12:10 AM8/8/07
to Flex India Community
Hi Raghu,

Thanks for the reply. I dont have any issues with the name.

I cant have a getter to return a value 0 when teh Number is NaN as 0
is a valid value for me. I want to show nothing in teh datagrid cell
when teh value is NaN. Is there a way to achieve this?

Thanks
Vikram

On Aug 8, 2:06 pm, Raghunath Rao <raghuonf...@yahoo.com> wrote:

> Hi Vikram,
>
> Sorry... I forgot the return types for the getters
>
> class MyData
> {
> private var _name : String;
> private var _percentMarks : Number;
>
> public function set
> percentMarks(value:Number):void{
> if(value){ _percentMarks = value;}
> else { _percentMarks = 0;}
> }
>
> public function set name(value:String):void{
> _name = value;
> }
>
> public function get percentMarks():Number{
> return _percentMarks;
> }
>
> public function get name():String{
> return _name;
> }
>
> }
>
> Thanks
> Raghu
>

> ___________________________________________________________________________­_________Ready


> for the edge of your seat?
> Check out tonight's top picks on Yahoo! TV.http://tv.yahoo.com/
>
> ___________________________________________________________________________­_________
> Park yourself in front of a world of choices in alternative vehicles. Visit the Yahoo! Auto Green Center.http://autos.yahoo.com/green_center/
>

> raghuonflex.vcf
> 1KDownload

Raghunath Rao

unread,
Aug 8, 2007, 7:03:57 AM8/8/07
to flex_...@googlegroups.com
Hi Rahul,

In that case... you have 2 options.

1) One way is to take in the percentageValue in the Class as a String. This will work like the name and so not display it if it is unavailable.
If you need it as a number somewhere else, just cast it :)
 private var _percentMarks:String;

    public function set percentMarks(value:String):void{
         _percentMarks = value;
    }

    public function get percentMarks():String{
         return _percentMarks;
    }

2) If you want to retain it as a Number in the class, i think the only way is to write an itemRenderer for your DG as below

<mx:DataGrid id="myDG" width="500" height="400"
        <mx:columns
            <mx:DataGridColumn headerText="Name" dataField="name"/
            <mx:DataGridColumn headerText="Percent Marks" dataField="percentMarks"
                <mx:itemRenderer
                    <mx:Component
                        <mx:Label
                            <mx:Script
                            <![CDATA[
                                public override function set data(value:Object):void{
                                    super.data = value;
                                    if(!isNaN(value.percentMarks)){
                                        this.text = String(value.percentMarks);
                                    }
                                    else{
                                        this.text = "";
                                    }
                                }
                            ]]
                        </mx:Script
                        </mx:Label                      
                    </mx:Component
                </mx:itemRenderer
            </mx:DataGridColumn
        </mx:columns
    </mx:DataGrid


You will have to alter the code in the class as...

public function set percentMarks(value:Number):void{
     if(value){ _percentMarks = value;}
     else { _percentMarks = NaN;}
I think its better to use the first method. I'll be interested if anyone has any other solutions.
-- 

Raghunath Rao
Engineer, Flex Framework
Adobe Systems, Bangalore
--~--~---------~--~----~----------------------~-------~--~
The opinions and views expressed here are purely my own 
and are in no way endorsed by my company (Adobe Systems)
-~----------~----~----~----~------~----~-----------------~


Moody friends. Drama queens. Your life? Nope! - their life, your story.
Play Sims Stories at Yahoo! Games.

raghuonflex.vcf

Raghunath Rao

unread,
Aug 8, 2007, 8:50:14 AM8/8/07
to flex_...@googlegroups.com
Hey Vikram,

It just struck me... A label function would do the
trick for you.
Sorry for the oversight :)

Raghu

--

Raghunath Rao
Engineer, Flex Framework
Adobe Systems, Bangalore
--~--~---------~--~----~----------------------~-------~--~
The opinions and views expressed here are purely my
own
and are in no way endorsed by my company (Adobe
Systems)
-~----------~----~----~----~------~----~-----------------~


____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search
that gives answers, not web links.
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC

raghuonflex.vcf

vikram.rk

unread,
Aug 9, 2007, 12:25:15 AM8/9/07
to Flex India Community
Thanks a lot Raghu.

On Aug 8, 5:50 pm, Raghunath Rao <raghuonf...@yahoo.com> wrote:
> Hey Vikram,
>
> It just struck me... A label function would do the
> trick for you.
> Sorry for the oversight :)
>
> Raghu
>
> --
>
> Raghunath Rao
> Engineer, Flex Framework
> Adobe Systems, Bangalore
> --~--~---------~--~----~----------------------~-------~--~
> The opinions and views expressed here are purely my
> own
> and are in no way endorsed by my company (Adobe
> Systems)
> -~----------~----~----~----~------~----~-----------------~
>

> ___________________________________________________________________________­_________


> Yahoo! oneSearch: Finally, mobile search
> that gives answers, not web links.http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
>

> raghuonflex.vcf
> 1KDownload

Reply all
Reply to author
Forward
0 new messages