使用IHierarchical对象的Flex AdvancedDataGrid

时间:2012-03-07 10:36:38

标签: flex advanceddatagrid

我的advancedDataGrid的模型类有问题。这是我的模型类:

package module.testPlanModule
    {
        import flash.events.Event;
        import flash.utils.IDataInput;
        import flash.utils.IDataOutput;
        import flash.utils.IExternalizable;

        import mx.collections.IHierarchicalData;
        import mx.utils.UIDUtil;

        [Bindable]
        [RemoteClass(alias="business.project.version.testPlan.TestSuite")]
        public class TestSuite implements IExternalizable, IHierarchicalData
        {
            private var _uuid:String;

            private var _ID:String;
            private var _testCasesList:Array;

            public function TestSuite()
            {
                _uuid = UIDUtil.createUID();
                _ID = "TESTSUITE-"+Math.random();
                _testCasesList = [];
            }

            public function get testCasesList():Array
            {
                return _testCasesList;
            }

            public function set testCasesList(value:Array):void
            {
                _testCasesList = value;
            }

            public function get ID():String
            {
                return _ID;
            }

            public function set ID(value:String):void
            {
                _ID = value;
            }

            public function get uuid():String
            {
                return _uuid;
            }

            public function set uuid(value:String):void
            {
                _uuid = value;
            }

            public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function dispatchEvent(event:Event):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function hasEventListener(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function willTrigger(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function canHaveChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return true;
                }
                else if(node is TestCase){
                    return true;
                }
                else
                    return false;
            }

            public function hasChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return !(node.testCasesList.length == 0);
                }
                else if(node is TestCase){
                    return !(node.allocatedTests.length == 0);
                }
                else
                    return false;
            }

            public function getChildren(node:Object):Object
            {
                if(node is TestSuite){
                    return node.testCasesList;
                }
                else if(node is TestCase){
                    return node.allocatedTests;
                }
                else
                    return null;
            }

            public function getData(node:Object):Object
            {
                return node;
            }

            public function getRoot():Object
            {
                // TODO Auto Generated method stub
                return null;
            }

            public function writeExternal(output:IDataOutput):void
            {
                output.writeUTF(_uuid);
                output.writeUTF(_ID);
                output.writeObject(_testCasesList);
            }

            public function readExternal(input:IDataInput):void
            {
                _uuid = input.readUTF();
                _ID = input.readUTF();
                _testCasesList = input.readObject();
            }
        }
    }








    package module.testPlanModule
    {
        import flash.events.Event;
        import flash.utils.IDataInput;
        import flash.utils.IDataOutput;
        import flash.utils.IExternalizable;

        import mx.collections.IHierarchicalData;
        import mx.utils.UIDUtil;

        [Bindable]
        [RemoteClass(alias="business.project.version.testPlan.TestCase")]
        public class TestCase implements IExternalizable, IHierarchicalData
        {
            private var _uuid:String;

            private var _ID:String;
            private var _allocatedTests:Array;

            public function TestCase()
            {
                _uuid = UIDUtil.createUID();
                _ID = "TESTCASE-"+Math.random();
                _allocatedTests = [];
            }

            public function get ID():String
            {
                return _ID;
            }

            public function set ID(value:String):void
            {
                _ID = value;
            }

            public function get allocatedTests():Array
            {
                return _allocatedTests;
            }

            public function set allocatedTests(value:Array):void
            {
                _allocatedTests = value;
            }

            public function get uuid():String
            {
                return _uuid;
            }

            public function set uuid(value:String):void
            {
                _uuid = value;
            }

            public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function dispatchEvent(event:Event):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function hasEventListener(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function willTrigger(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function canHaveChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return true;
                }
                else if(node is TestCase){
                    return true;
                }
                else
                    return false;
            }

            public function hasChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return !(node.testCasesList.length == 0);
                }
                else if(node is TestCase){
                    return !(node.allocatedTests.length == 0);
                }
                else
                    return false;
            }

            public function getChildren(node:Object):Object
            {
                if(node is TestSuite){
                    return node.testCasesList;
                }
                else if(node is TestCase){
                    return node.allocatedTests;
                }
                else
                    return null;
            }

            public function getData(node:Object):Object
            {
                return node;
            }

            public function getRoot():Object
            {
                // TODO Auto Generated method stub
                return null;
            }

            public function writeExternal(output:IDataOutput):void
            {
                output.writeUTF(_uuid);
                output.writeUTF(_ID);
                output.writeObject(_allocatedTests);
            }

            public function readExternal(input:IDataInput):void
            {
                _uuid = input.readUTF();
                _ID = input.readUTF();
                _allocatedTests = input.readObject();
            }
        }
    }











    package module.testPlanModule
    {
        import flash.events.Event;
        import flash.utils.IDataInput;
        import flash.utils.IDataOutput;
        import flash.utils.IExternalizable;

        import mx.collections.IHierarchicalData;
        import mx.utils.UIDUtil;

        [Bindable]
        [RemoteClass(alias="business.project.version.test.TestBase")]
        public class TestBase implements IExternalizable, IHierarchicalData
        {
            private var _uuid:String;

            private var _ID:String;
            private var _state:String;
            private var _result:String;
            private var _allocatedUser:String;
            private var _linkedRequirements:Array;

            public function TestBase()
            {
                _uuid = UIDUtil.createUID();
                _ID = "TEST-"+Math.random();
                _state = "not passed";
                _result = "na";
                _allocatedUser = "";
                _linkedRequirements = [];
            }

            public function get ID():String
            {
                return _ID;
            }

            public function set ID(value:String):void
            {
                _ID = value;
            }

            public function get state():String
            {
                return _state;
            }

            public function set state(value:String):void
            {
                _state = value;
            }

            public function get result():String
            {
                return _result;
            }

            public function set result(value:String):void
            {
                _result = value;
            }

            public function get allocatedUser():String
            {
                return _allocatedUser;
            }

            public function set allocatedUser(value:String):void
            {
                _allocatedUser = value;
            }

            public function get linkedRequirements():Array
            {
                return _linkedRequirements;
            }

            public function set linkedRequirements(value:Array):void
            {
                _linkedRequirements = value;
            }

            public function get uuid():String
            {
                return _uuid;
            }

            public function set uuid(value:String):void
            {
                _uuid = value;
            }

            public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function dispatchEvent(event:Event):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function hasEventListener(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
            {
                // TODO Auto Generated method stub

            }

            public function willTrigger(type:String):Boolean
            {
                // TODO Auto Generated method stub
                return false;
            }

            public function canHaveChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return true;
                }
                else if(node is TestCase){
                    return true;
                }
                else
                    return false;
            }

            public function hasChildren(node:Object):Boolean
            {
                if(node is TestSuite){
                    return !(node.testCasesList.length == 0);
                }
                else if(node is TestCase){
                    return !(node.allocatedTests.length == 0);
                }
                else
                    return false;
            }

            public function getChildren(node:Object):Object
            {
                if(node is TestSuite){
                    return node.testCasesList;
                }
                else if(node is TestCase){
                    return node.allocatedTests;
                }
                else
                    return null;
            }

            public function getData(node:Object):Object
            {
                return node;
            }

            public function getRoot():Object
            {
                // TODO Auto Generated method stub
                return null;
            }

            public function writeExternal(output:IDataOutput):void
            {
                output.writeUTF(_uuid);
                output.writeUTF(_ID);
                output.writeUTF(_state);
                output.writeUTF(_result);
                output.writeUTF(_allocatedUser);
                output.writeObject(_linkedRequirements);
            }

            public function readExternal(input:IDataInput):void
            {
                _uuid = input.readUTF();
                _ID = input.readUTF();
                _state = input.readUTF();
                _result = input.readUTF();
                _allocatedUser = input.readUTF();
                _linkedRequirements = input.readObject();
            }
        }
    }

这是我的AdvancedDatagrid:

  <mx:AdvancedDataGrid id="testPlanADG" displayItemsExpanded="true" 
                                                 width="95%" height="95%" 

                                                 contentBackgroundAlpha="0.0" chromeColor="0xdbeaff"
                                                 openDuration="500"

                                                 verticalScrollPolicy="on"
                                                 horizontalScrollPolicy="auto"
                                                 variableRowHeight="true">
                    <mx:columns>
                        <mx:AdvancedDataGridColumn  id="IdCol" dataField="ID" headerText="ID" 
                        />
                        <mx:AdvancedDataGridColumn dataField="state" headerText="State"/>
                        <mx:AdvancedDataGridColumn dataField="result" headerText="Result"/>
                        <mx:AdvancedDataGridColumn dataField="allocatedUser" headerText="User affected"/>
                    </mx:columns>
                </mx:AdvancedDataGrid>

问题是只显示TestSuite的ID而没有其他内容。 为了填充ADG,我做了这个:

_model = new ArrayCollection();
// here I populate the model
var testSuite1:TestSuite = new TestSuite();
testSuite1.ID = "testSuite1";
var testCase1:TestCase = new TestCase();
testCase1.ID = "testCase1";
var testBase1:TestBase = new TestBase();
testBase1.ID = "testBase1";
var testBase2:TestBase = new TestBase();
testBase2.ID = "testBase2";
testCase1.allocatedTests = [testBase1,testBase2];
// ...
_testPlanHierarchy = new HierarchicalData();
_testPlanHierarchy.source = _model;
testPlanADG.dataProvider = _testPlanHierarchy;

我可以看到我的两个测试套件的ID显示为叶子节点,没有别的。我真的不明白。

1 个答案:

答案 0 :(得分:1)

您可以选择显示或隐藏根节点,并且您需要告诉ADG要为子节点查看哪个字段,因为您没有字段称为子节点。该字段必须是ListCollectionViewnor类型的一个子类,例如ArrayCollection。也许只是添加一个名为children的字段来包装你已经拥有类型Array的字段将解决你的问题。