Skip to main content
Participant
January 13, 2011
Question

Spark DataGrid Binding Problem

  • January 13, 2011
  • 3 replies
  • 2866 views

Hi all ,

I'm working on Flex Hero (build 18623)

I have a Spark DataGrid and a Button which delete the selected item on the DataGrid.

Obviously we would like the Button to be enabled only when there is a selection on the DataGrid so we binded the enable property of the Button to the selectedIndex of the DataGrid (Alternately we can use the selectedItem).

When the application starts the Button – as expected – is disabled .

Once we select any raw/item on the DataGrid the Button – as expected – becomes enabaled.

When we press the button the selected item on the DataGrid is deleted  - also as expected.

THE PROBLEM – after the deletion is completed although we find that the DataGrid selectedItem and selectedIndex are null and -1 respectively  which indicates that there is no selection on the DataGrid and it's good …

BUT the Button – from some reason is still enabled.

It seemed like binding is not working good from the DataGrid to the Button.

ANOTHER THING :

Check out the variable dg_selectedItemin the code below, which has Two Way Binding with the DataGrid selectedItem property. Also this variable isn't being updated after the deletion is completed – although the DataGrid selectedItem  property is null – the above mentioned variable is still holding the item/Object who was just deleted from the DataGrid.

I Also tried this on Flex Hero build 17689 and it's not working as well.

IMPORTANT : I also checked all the above using the mx:AdvancedDataGrid instead s:DataGrid and it's working good – the Button becomes disabled after the deletion.

So it seems to be a bug in the spark data grid.

Anyone has an idea how to solve this problem?

Thanks,

Shamir

Here is the code.

This topic has been closed for replies.

3 replies

Participant
September 14, 2011

I've also experienced this problem, and yeah the solution is basically to simply dispatch the VALUE_COMMIT Event.

Here's a post I wrote about this:

http://andrewvarga.com/blog/2011/09/10/datagrid-binding-issue/

Participant
August 19, 2011

Yeah, I'm having the same problem. To get around it I programatically dispatch a valueCommit event. Ugly, but when you've gotta have things working...

grid.dispatchEvent(new Event("valueCommit"));

Participant
January 13, 2011

Sorry, the code was not saved in the previous post...

<?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"

                     creationComplete="init()"

                     minWidth="955" minHeight="600">

      <fx:Declarations>

      </fx:Declarations>

      <fx:Script>

            <![CDATA[

                  import mx.collections.ArrayCollection;

                  import mx.collections.ListCollectionView;

                 

                  [Bindable]

                  private var dataProvider:ListCollectionView = new ArrayCollection();

                  [Bindable]

                  private var dg_selectedItem:Object;

                 

                  protected function testBut_clickHandler(event:MouseEvent):void

                  {

                        //testPM.deleteSelectedItem();

                        deleteSelectedItem();

                  }

                 

                  public function deleteSelectedItem():void

                  {

                        //var index:int = dataProvider.getItemIndex(dg_selectedItem);

                        var index:int = dataProvider.getItemIndex(dg.selectedItem);

                        dataProvider.removeItemAt(index);

                  }

                                   

                  private function init():void

                  {

                        dataProvider.addItem({name:'qqqqq',last:'qqqqqq',address:'qqqqqqqq'});

                        dataProvider.addItem({name:'wwwwww',last:'wwwwww',address:'wwwwwwwww'});

                        dataProvider.addItem({name:'eeeeeeeeeeee',last:'eeeeeeeeee',address:'eeeeeeeeeeeeee'});

                        dataProvider.addItem({name:'rrrrrrr',last:'rrrrrrrr',address:'rrrrrrrrrrrrrrrrr'});

                        dataProvider.addItem({name:'ssssssssss',last:'sssssssss',address:'ssssssss'});

                        dataProvider.addItem({name:'ccccccccccc',last:'cccccccc',address:'cccccccccc'});

                  }

                 

                 

                  protected function showDetails_Spark_clickHandler(event:MouseEvent):void

                  {

                       

                        trace("dg.selectedIndex == "+dg.selectedIndex);

                        trace("dg.selectedItem == "+dg.selectedItem);

                        trace("dg_selectedItem == "+dg_selectedItem);

                  }

                 

            ]]>

      </fx:Script>

      <s:layout>

            <s:VerticalLayout/>

      </s:layout>

      <s:Button id="testBut_Spark" label="delete selected Item Spark" click="testBut_clickHandler(event)"

                    enabled="{dg.selectedIndex > -1}"/>

     

      <s:Button id="showDetails_Spark"

                    label="show details Spark"

                    click="showDetails_Spark_clickHandler(event)"/>

     

      <s:DataGrid id="dg" dataProvider="{dataProvider}" selectedItem="@{dg_selectedItem}">

            <s:columns>

                  <s:ArrayList>

                        <s:GridColumn dataField="name" width="100"/>

                        <s:GridColumn dataField="last" width="100"/>

                        <s:GridColumn dataField="address" width="300"/>

                  </s:ArrayList>

            </s:columns>

      </s:DataGrid>

</s:Application>

Participant
May 22, 2011

I'm having this exact same problem. The Spark Datagrid doesn't seem to fire bindings for "selectedIndex" or "selectedItem" when those values become -1 and null, respectively.