Search This Blog

Tuesday, April 19, 2011

Flex downloading data of type ‘Date’ from DB to DataGrid; null value problem; null value is automatically converted to current_date instead of empty string;

The problem occurs while downloading data from DB into Flex’s DataGrid. If we have a Date column and it’s filled with null values, these values will automatically get the current date (time) value in the process of conversion (when new Date() object is created). To avoid this we can override labelFunction in DataGrid and prevent current_data values from being displayed.

In the DataGridColumn add labelFunction:

<mx:DataGridColumn headerText="STOPDATE" dataField="STOPDATE" labelFunction="removeNullValuesLabelFunction"/>

Define removeNullValuesLabelFunction in main <mx:Script>:

public function removeNullValuesLabelFunction(item:Object, column:DataGridColumn):String
            {
                var currentDate:Date = new Date();
                var nmbrOfSeconds:int = 3;
                currentDate.setTime( currentDate.time - ( nmbrOfSeconds * 1000 ) );
                if (item.STOPDATE > currentDate)
                {
                    return "";
                }
                else
                {
                    return String(item.STOPDATE);
                }
            }

4 comments:

  1. Hi Bartosz,

    First up I really liked your blog design.
    Regarding the solution you've posted in the above blog post, I need help with something similar to your logic with a slight modication.
    In your case, in the labelFunction, you show "" (an empty string). What I need to do is if the value is invalid, I need to retain the already existing value in the cell.

    Can you help me out with this?

    Thanks in Advance!

    ReplyDelete
  2. @srvikram13
    well, if I understood you correctly then you'd need to check the data for the "fault" in the "if (....)" section, correct the data to have them displayed, and return the string.

    I know you said you need to " retain the already existing value". There can be problems however, cause flex has some error corrections in the service section (which I haven't touched, so I can't help you with that) and can give you a null value in the grid. There is no sense in messing with labelFunction() at all then because the problem lies deeper - in the data service. If that's the case, try to rewrite the PHP code that downloads data and see what happens. Hope that helps you,
    Bert.

    ReplyDelete
  3. Hi Bartosz,

    Thanks for the quick reply!

    Actually what I am developing is an online bidding site with functionality similar to that of a stock trading application.
    Also, one more thing. I am using Flash CS5 for the same & not Flex.

    Have a look at the code below to get an idea of what I want.


    public function compareBids(item:Object, column:DataGridColumn):String
    {
    if (item.latestbid< presentBid)
    {
    return "green"+String(item.latestbid);
    }else
    {
    return "red"+String(item.latestbid);
    }
    }

    What I want is I need the value of 'presentBid', which is essentially the value currently in the DataGrid cell.

    I have also tried using a CellRenderer instead of a labelfunction. But, to no avail.

    Thanks for all your help.

    ReplyDelete
  4. OK. I'm a little busy with duties at work. That's how I download data from another column in the other place in my code. Hope that will help.

    var testIDForViewingLogs:int = 0; //we'll get log data according to this id
    var testName:String = "no_name";
    //get dataGrid from global variables
    var dataGrid = getDataGrid();
    var dataProvider = getDataProvider();

    var grid1:DataGrid = DataGrid(DataGridListData(listData).owner);
    var data1 = data;
    var item = null;
    for (var i:int = 0; i < dataProvider.length; i++)
    {
    item = dataProvider.getItemAt(i);

    if (grid1.isItemSelected(item))
    {
    testIDForViewingLogs = item.TESTID;
    testName = item.TESTNAME;
    }
    }

    protected function getDataGrid()
    {
    return FlexGlobals.topLevelApplication.dataGrid;
    }

    protected function getDataProvider()
    {

    return FlexGlobals.topLevelApplication.dataGrid.dataProvider;
    }

    ReplyDelete

If you like this post, please leave a comment :)