【问题标题】:How to bind XMLListCollection into Model?如何将 XMLListCollection 绑定到模型中?
【发布时间】: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


    【解决方案1】:

    以下是您的一些误解:

    <root><colors>{collection}</colors></root>
    

    您可能希望这会将 collection 绑定到 &lt;colors/&gt; 节点的子节点,而它所做的是 E4X 字符串替换,因此这里没有创建绑定。

    var xml:XML = new XML(<color><value></value></color>);
    

    应该是:

    var xml:XML = <color><value/></color>;
    

    XML 在 AS3 中具有特殊的文字语法,您用来创建新 XML 的已经是 XML,所以本质上,您做了两次。没有文本值的节点被规范化为更短,因此您尝试以您的方式编写它们将无效。

    XMLListCollection 实际上是一个无用的类,甚至不知道它为什么存在。您可以指定XMLXMLList 作为DataGrid 的数据提供者。 Model 是另一个无用的类,因此,您可能只需松开它并用 dataGrid.dataProvider 替换它,您需要引用 DataGrid 的内容。

    【讨论】:

    • 我不知道你为什么说 XMLListCollection 是无用的......它允许 XML 用作数据提供者并生成所有更改事件等,类似于 ArrayCollection 允许数组的方式绑定为 dataProvider 并在添加和删除项目时更新列表。在这里没用的类是模型。我在这里看不到任何意义(并且从未找到它的用例)。在这种情况下,为什么不只是 server.send({collection.source as XMLList});?这假设服务器实际上需要所有额外的嵌套,而不仅仅是剥离它......
    • 否则,你可以使用 server.send(collection.source);
    • 谢谢,请在问题底部查看我的编辑。
    • 嗯,这很奇怪;我正在使用 v4.1,并且我的代码有效。另外,我使用的是免费的 mxmlc 编译器,而不是 FlashBuilder。也许它在高于 4.1 的版本中被破坏了?
    猜你喜欢
    • 2016-12-06
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    相关资源
    最近更新 更多