0

我想要做的是让用户将他的数据输入到多个文本框中,然后一旦输入数据,它就会在运行时显示在数据网格上。问题是当我运行应用程序时,我单击了按钮,但没有将输入的信息添加到数据网格中。一旦按下按钮,我的文本框也应该被清除,但再次没有任何反应。这是我的代码:

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

     [Bindable]private var dgItems:ArrayCollection;
     [Bindable]public var temp:Object;


     public function addItem(evt:Event):void {
     //add item if the text input fields are not empty
      if(name_input.text != ""&& location_input.text !="") {
     //create a temporary Object
      temp = myDG.selectedItem;
      var temp:Object = new Object();
     //add the text from the textfields to the object
      temp = {name:name_input.text, location:location_input.text};

     //this will add the object to the ArrayColldection (wich is binded with the DataGrid)
      dgItems.addItem(temp);
     //clear the input fields
      name_input.text = "";
       location_input.text ="";
      }
     }
    ]]>
   </mx:Script>

 <mx:DataGrid  x="97" y="110"  dataProvider="{dgItems}" id="myDG">
  <mx:columns>
   <mx:DataGridColumn headerText="Column 1" dataField="name:"/>
   <mx:DataGridColumn headerText="Column 2" dataField="location:"/>

  </mx:columns>
 </mx:DataGrid>
 <mx:Button x="97" y="51" label="add" click="addItem(event)"/>
 <mx:TextInput x="117" y="300" id="location_input"/>
 <mx:TextInput x="117" y="340" id="name_input"/>
</mx:Application>

任何帮助是极大的赞赏。

4

1 回答 1

1

您的代码有很多错误。首先,您没有初始化 dgItems 数组集合,因此它的值为 null。当您尝试将“添加项目”添加到空对象时会出现错误。

其次,您在初始化 DataGrid 的 ArrayCollection dataProvider 之前尝试访问 dataGrid 的 selectedItem。列表中没有项目,不能有 selectedItem。

第三,您创建了两次新对象;一次使用“新对象”行,再次使用用于定义对象的内联语法:'{}

第四,DataGridColumn 上的数据字段属性都包含冒号,但对象的属性没有。

我希望这有帮助。

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

            // array collection was never initialized; you can't items to a null object 
            [Bindable]private var dgItems:ArrayCollection = new ArrayCollection();
// not needed
//          [Bindable]public var temp:Object;


            public function addItem(evt:Event):void {
                //add item if the text input fields are not empty
                if(name_input.text != ""&& location_input.text !="") {
                    //create a temporary Object
                    // this line of code serves no purpos
                    //                  temp = myDG.selectedItem;
                    var temp:Object // = new Object();
                    //add the text from the textfields to the object
                    temp = {name:name_input.text, location:location_input.text};

                    //this will add the object to the ArrayColldection (wich is binded with the DataGrid)
                    dgItems.addItem(temp);
                    //clear the input fields
                    name_input.text = "";
                    location_input.text ="";
                }
            }
        ]]>
    </mx:Script>

    <mx:DataGrid  x="97" y="110"  dataProvider="{dgItems}" id="myDG">
        <mx:columns>
            <!-- need to remove the colons from the data field -->
            <mx:DataGridColumn headerText="Column 1" dataField="name"/>
            <mx:DataGridColumn headerText="Column 2" dataField="location"/>

        </mx:columns>
    </mx:DataGrid>
    <mx:Button x="97" y="51" label="add" click="addItem(event)"/>
    <mx:TextInput x="117" y="300" id="location_input"/>
    <mx:TextInput x="117" y="340" id="name_input"/>
</mx:Application>

一旦我启动调试器,这些错误中的许多都很容易被发现。如果您还没有使用它,我建议您阅读一下它。

于 2010-08-10T00:56:56.167 回答