【发布时间】:2012-03-24 07:55:32
【问题描述】:
当 XMLListCollection 在 DataGrid 中更新时,我希望它将 XML(绑定它)添加到模型中。我该怎么做?或者如果不可能,我将如何手动将 XML 添加到模型中?
这是我正在尝试做的一个粗略示例。将数据添加到 DataGrid 工作正常;它只是在发送到 HTTPService 时不会进入模型。
<mx:Model id="model">
<root><colors>{collection}</colors></root>
</mx:Model>
<mx:XMLListCollection id="collection" />
<mx:Label text="Input color:" />
<mx:TextInput id="color" />
<mx:Button label="Add Color" click="addColor();" />
<mx:DataGrid dataProvider="{collection}">
<mx:columns>
<mx:DataGridColumn dataField="color" headerText="Color" />
</mx:columns>
</mx:DataGrid>
<mx:Button label="Submit" click="submit();" />
<mx:Script><![CDATA[
// imports...
public function addColor():void {
var xml:XML = new XML(<color></color>);
xml.color = color.text;
collection.addItem(xml);
Alert.show(collection.toXMLString()); // xml looks fine and shows up in datagrid
// how to do something like this?
// model.appendChild(xml); // doesn't work
}
public function submit():void {
var serv:HTTPService = new HTTPService();
// setup serv...
serv.send(model); // server just gets "<colors></colors>"
}
]]></mx:Script>
谢谢!
编辑:
谢谢大家的回答!它最终导致了我的解决方案。我不得不使用 XMLListCollection,因为无论出于何种原因,向 DataGrid 添加 2 个或更多元素都无法与 XML 或 XMLList 一起使用(我尝试了 {variable},{variable.colors},{variable.colors.color},{variable.color } 等所有可能性)。我认为我可以使用模型,但我将其更改为下面的 XML,以便您可以使用 toXMLString() 看到输出。最后,我必须使用模型或 XML,因为这只是大型项目的一个小例子(我通过在下面添加 OtherData 来展示这一点)。再次感谢。这是一个工作示例:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:XML id="data">
<colors>
</colors>
</mx:XML>
<mx:XMLListCollection id="collection" source="{data.color}" />
<mx:XML id="model">
<root>
<otherData>{otherData.text}</otherData>
<colors>{data}</colors>
</root>
</mx:XML>
<mx:Label text="Input color:" />
<mx:TextInput id="color" />
<mx:Button label="Add Color" click="addColor();" />
<mx:DataGrid dataProvider="{collection}">
<mx:columns>
<mx:DataGridColumn dataField="color" headerText="Color" />
</mx:columns>
</mx:DataGrid>
<mx:Label text="Other data:" />
<mx:TextInput id="otherData" />
<mx:Button label="Submit" click="submit();" />
<mx:Script><![CDATA[
import mx.controls.*;
public function addColor():void {
var xml:XML = new XML(<color></color>);
xml.color = color.text;
collection.addItem(xml);
Alert.show(collection.toXMLString()); // xml looks fine and shows up in datagrid
}
public function submit():void {
Alert.show(model.toXMLString());
}
]]></mx:Script>
</mx:Application>
【问题讨论】:
标签: apache-flex actionscript datagrid model mxml