开发者

Selection in Flex Datagrid does not pass the valueObject to selectionChangeHandler function

开发者 https://www.devze.com 2023-03-27 22:41 出处:网络
I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.

I have a TabNavigator, and each tab is a Module. One of the modules is labelled Units and the full code of the module is posted in this post.

There are several problems: 1) Forms are not populated with data from the datagrid selection. 2) Selecting a row and clicking delete gives the very-common error: TypeError: Error #1009: Cannot access a property or method of a null object reference. A trace on the valueObject unit within the selectionChangeHandler function gives NULL. Why?

Note: In other modules (other tabs of the TabNavigator), I have DropDownLists populated with units. This means that the valueObject Unit is defined in the other modules. However, valueObjects should be private to modules, and not shared. I am unsure where the problem comes.

Full module code:

<?xml version="1.0" encoding="utf-8"?>
<s:Module xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
          xmlns:unitservice="services.unitservice.*"
          xmlns:valueObjects="valueObjects.*"
          width="724"
          height="674">
    <fx:Style source="assets/CAaNDFlex.css"/>
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.rpc.events.ResultEvent;

            import spark.events.GridSelectionEvent;

            protected function unitsDg_creationCompleteHandler(event:FlexEvent):void
            {
                getUnitsResult.token=unitservice.getUnits();
            }

            protected function addBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsAdd";
                unit=new Unit();
            }

            protected function unitsDg_selectionChangeHandler(event:GridSelectionEvent):void
            {
                trace(event.currentTarget.selectedItem); //Unit object detected
                trace(event.currentTarget.selectedItem as Unit); //NULL 
                trace(unit); // unit is NULL. Why?
                currentState="unitsDetails";
            }


            protected function button_clickHandler(event:MouseEvent):void
            {
                trace(unit); // unit is NULL. Why?
                unit.unitName=unitNameTextInput.text;
                if (unit.unitID == 0)
                {
               开发者_JAVA百科     createUnitResult.token=unitservice.createUnit(unit);
                }
                else
                {
                    updateUnitResult.token=unitservice.updateUnit(unit);
                }
            }

            protected function updateBtn_clickHandler(event:MouseEvent):void
            {
                currentState="unitsUpdate";
            }

            protected function createUnitResult_resultHandler(event:ResultEvent):void
            {
                currentState="unitsDetails";
                unit.unitID=event.result as int;
                unitsDg.dataProvider.addItem(unit);
                unitsDg.setSelectedIndex(unitsDg.dataProvider.getItemIndex(unit));
                unitsDg.ensureCellIsVisible(unitsDg.selectedIndex);
            }

            protected function deleteBtn_clickHandler(event:MouseEvent):void
            {
                deleteUnitResult.token = unitservice.deleteUnit(unit.unitID);

            }

            protected function deleteUnitResult_resultHandler(event:ResultEvent):void
            {
                unitsDg.dataProvider.removeItemAt(unitsDg.selectedIndex);
                currentState="units";
            }

        ]]>
    </fx:Script>
    <s:states>
        <s:State name="units"/>
        <s:State name="unitsDetails"/>
        <s:State name="unitsAdd"/>
        <s:State name="unitsUpdate"/>
    </s:states>
    <fx:Declarations>
        <s:CallResponder id="getUnitsResult"
                         result="unit = getUnitsResult.lastResult as Unit"/>
        <unitservice:UnitService id="unitservice"
                                           fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                           showBusyCursor="true"/>
        <valueObjects:Unit id="unit" />
        <s:CallResponder id="createUnitResult"
                         result="createUnitResult_resultHandler(event)"/>
        <s:CallResponder id="updateUnitResult"/>
        <s:CallResponder id="deleteUnitResult" result="deleteUnitResult_resultHandler(event)"/>



        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Binding destination="unit" source="unitsDg.selectedItem as Unit"/>

    <s:DataGrid id="unitsDg" x="10" y="37"
                creationComplete="unitsDg_creationCompleteHandler(event)" requestedRowCount="4"
                selectionChange="unitsDg_selectionChangeHandler(event)">
        <s:columns>
            <s:ArrayList>
                <s:GridColumn dataField="unitName"
                              headerText="unitName">
                </s:GridColumn>
                <s:GridColumn dataField="unitID"
                              headerText="unitID">
                </s:GridColumn>
            </s:ArrayList>
        </s:columns>
        <s:typicalItem>
            <fx:Object unitID="unitID1"
                       unitName="unitName1">
            </fx:Object>
        </s:typicalItem>
        <s:AsyncListView list="{getUnitsResult.lastResult}"/>
    </s:DataGrid>
    <s:Button id="addBtn" x="10" y="0" label="Add" click="addBtn_clickHandler(event)"
              styleName="actionButton"/>
    <s:Form includeIn="unitsAdd,unitsUpdate"
            x="10"
            y="176"
            defaultButton="{button}">
        <s:FormItem label="unitName">
            <s:TextInput id="unitNameTextInput"
                         text="{unit.unitName}"/>
        </s:FormItem>
        <s:Button id="button"
                  label="Add"
                  click="button_clickHandler(event)"
                  label.unitsUpdate="Update"/>
    </s:Form>
    <s:Button id="updateBtn" x="138" y="0" label="Update" click="updateBtn_clickHandler(event)"/>
    <s:Button id="deleteBtn" x="266" y="0" label="Delete" click="deleteBtn_clickHandler(event)"/>
    <s:Form includeIn="unitsDetails" x="10" y="176">
        <s:FormItem label="unitName">
            <s:Label id="unitNameLabel" text="{unit.unitName}"/>
        </s:FormItem>
    </s:Form>
</s:Module>


The selectedObject is not successfully casting to Unit, which means it probably wasn't Unit or a subclass prior to the cast. Casting it to Unit won't make it a Unit unless it was one before.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号