从datagrid绑定数据

时间:2011-06-13 18:12:26

标签: flash actionscript-3 datagrid mxml flash-builder

我有一个由两个数据网格和一个按钮组成的表单。 Datagrid 1,“myStaticDataGrid”,具有我添加的值供用户选择。我希望按钮的click事件将当前选择的myStaticDataGrid发送到第二个数据网格“myDataGrid”。如果我使用文本框和数据网格,但是我无法找到正确的语法来从myStaticDataGrid中获取选择数据。

这是我尝试使用两种数据网格方法:

<s:Form id="myForm">
//The values from this grid are determined once the button is clicked.
  <s:FormItem id="myDataGrid">
    <s:DataGrid id="bdgFormData">
      <s:typicalItem>
        <s:DataItem formData="Description" xmlData="Value"/>
      </s:typicalItem>
      <s:ArrayCollection id="values"> </s:ArrayCollection>
    </s:DataGrid>
  </s:FormItem>


//The values from this grid are determined at runtime.
<s:FormItem id="myStaticDataGrid">
    <s:DataGrid id="userSelects">
        <s:typicalItem>
            <s:DataItem selects="Typical Item" codes="0000"/>
        </s:typicalItem>

        <s:ArrayCollection id="selects">
            <s:DataItem selects="Y" codes="1"/>
            <s:DataItem selects="N" codes="0"/>
        </s:ArrayCollection>
    </s:DataGrid>
</s:FormItem>

<s:FormItem label="Add Selects">
    <s:Button label="Go" click="addData(event)"/>
</s:FormItem>

我发送数据的AS事件:

protected function addData(event:MouseEvent):void
{

  //Put selected data at the top of the grid.
  items.addItemAt(lstFormData.typicalItem,0)

}

我的问题是我在哪里绑定网格数据?

这是我将文本框数据发送到数据网格的方式:

<s:FormItem label="myDataUtil">
  <s:Label text="Value"/>
  <s:TextInput text="@{lstFormData.typicalItem.formData}"/>
</s:FormItem>

1 个答案:

答案 0 :(得分:1)

我为你鞭打了这个,不幸的是我在这里只有flex 3所以你必须为你需要的任何东西进行转换,但是你应该知道它是如何工作的。

<?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  <mx:Script>
  <![CDATA[
  import mx.collections.ArrayCollection;
  [Bindable]
  public var originalData:ArrayCollection

  [Bindable]
  public var changingData:ArrayCollection;

  public function init( ):void{
    this.changingData = new ArrayCollection( )
    this.originalData = new ArrayCollection( )
    for( var i:int = 0;i<100;i++){
      var obj:Object = new Object( )
      obj.label = 'slot '+ i;
      obj.value = 's'+i;
      originalData.addItem( obj );
    }
  }

  public function onSelect( e:Event ):void{
    this.changingData.addItem(( e.currentTarget as DataGrid).selectedItem )
  }

  ]]> 
  </mx:Script>

<mx:DataGrid id="myStaticDataGrid" dataProvider="{originalData}"  click="onSelect(event)"/>
<mx:DataGrid id="bdgFormData" dataProvider="{changingData}" x="240" y="0"/>

</mx:Application>

以下是没有绑定数据的示例

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            public function onSelect( e:Event ):void{
                var targetProvider:ArrayCollection = ( bdgFormData.dataProvider as ArrayCollection )
                if( !targetProvider ){
                    targetProvider = new ArrayCollection()
                }
                targetProvider.addItem(( e.currentTarget as DataGrid).selectedItem )
                bdgFormData.dataProvider = targetProvider
            }

    ]]> 
</mx:Script>

<mx:DataGrid id="myStaticDataGrid" click="onSelect(event)" >
    <mx:dataProvider>
        <mx:ArrayCollection >
            <mx:Object label="AIR" />
            <mx:Object label="ColdFusion" />
            <mx:Object label="Dreamweaver" />
            <mx:Object label="Flash" />
            <mx:Object label="Flex" />
            <mx:Object label="Photoshop" />
        </mx:ArrayCollection>
    </mx:dataProvider>
</mx:DataGrid>
<mx:DataGrid id="bdgFormData" x="240" y="0"/>