将 Xml 值分配给动态创建的组件

发布于 2024-09-25 19:30:18 字数 537 浏览 1 评论 0原文

xml 值存储在“arr”数组集合中。根据存储的数组的长度,创建下面描述的组件并将这些值动态分配给相关组件。

例如:

AdvanceSearch.mxml 是一个组件,另一个组件是 advanceSearchAtom.mxml。 “advanceSearchAtom”内有一些子组件,如文本框、组合框等。我们可以在'AdvanceSearch.mxml'中添加许多advanceSearchAtom.mxml。

var container : AdvanceSearch;

var adv : advanceSearchAtom;

for(var i:int=0;i<arr.length;i++) {
  adv = new advanceSearchAtom();

  adv.field.text = arr[i].field; 
  adv.value.selectedIndex = arr[i].value;

  container.addChild(adv); 
}

如果有人遇到此类问题,请告诉我。是否有明显的相关链接。提前致谢

xml values are stored in 'arr' array collection. depending on the length of the array stored, the below described components are created and assign those values to relevant components dynamically.

For Example:

AdvanceSearch.mxml is one component, and another components as advanceSearchAtom.mxml. within 'advanceSearchAtom' has some sub components as textbox, combo box etc,. we can add many advanceSearchAtom.mxml inside 'AdvanceSearch.mxml'.

var container : AdvanceSearch;

var adv : advanceSearchAtom;

for(var i:int=0;i<arr.length;i++) {
  adv = new advanceSearchAtom();

  adv.field.text = arr[i].field; 
  adv.value.selectedIndex = arr[i].value;

  container.addChild(adv); 
}

Please let me know if anybody come across this type of problem. if any relevant link is appreciable. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-10-02 19:30:19

您没有提到它,但我想问题是您在以下几行中收到空引用错误(1009):

adv.field.text = arr[i].field; 
adv.value.selectedIndex = arr[i].value;

我对吗?

这是因为 fieldvalue 尚未创建。根据默认的组件实例化,Flex 仅在需要时(即要显示时)创建子组件。

您可以侦听 AdvanceSearchAtom 组件的 creationComplete 事件并从那里更新值;或者在 AdvanceSearchAtom 类中拥有 Binadble 公共变量,将它们绑定到 field.textvalue.selectedIndex 并分配xml 值赋给循环中的这些变量。

使用创建完成:

public var container:AdvanceSearch;
public var searchItems:Array = [];
public var arr:Array;

//assuming that arr has been initialized with xml values.
var adv:AdvanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
  adv = new AdvanceSearchAtom();
  adv.addEventListener(FlexEvent.CREATION_COMPLETE, onAtomCreated);
  container.addChild(adv); 
  searchItems.push(adv);
}

public function onAtomCreated(e:Event):void
{
   var adv:AdvanceSearchAtom = e.currentTarget as AdvanceSearchAtom;
   if(!adv)
      return;
   var index:Number = searchItems.indexOf(adv);
   adv.field.text = arr[index].field;
   adv.value.selectedIndex = arr[index].value;
}

使用数据绑定:

在AdvanceSearchAtom.mxml 内部

<mx:TextInput id="field" text="{textValue}"/>
<mx:ComboBox id="value" selectedIndex="{comboIndex}"/>

AdvanceSearchAtom.mxml 的脚本块中

[Bindable]
public var textValue:String;

[Bindable]
public var comboIndex:Number;
var adv:AdvanceSearchAtom;

在 AdvanceSearch 类中:

for(var i:int=0;i<arr.length;i++) {
  adv = new AdvanceSearchAtom();
  container.addChild(adv); 
  adv.field.text = arr[i].field; 
  adv.value.selectedIndex = arr[i].value;
}

You didn't mention it, but I guess the problem is that you're getting null reference error (1009) on the following lines:

adv.field.text = arr[i].field; 
adv.value.selectedIndex = arr[i].value;

Am I right?

This is because field and value are not yet created. As per the default component instantiation, Flex creates the children only when it is required - i.e., when it is to be displayed.

You can either listen to the creationComplete event of the AdvanceSearchAtom component and update the values from there; or have Binadble public variables in the AdvanceSearchAtom class, bind them to field.text and value.selectedIndex and assign xml values to those variables in the loop.

Using creation complete:

public var container:AdvanceSearch;
public var searchItems:Array = [];
public var arr:Array;

//assuming that arr has been initialized with xml values.
var adv:AdvanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
  adv = new AdvanceSearchAtom();
  adv.addEventListener(FlexEvent.CREATION_COMPLETE, onAtomCreated);
  container.addChild(adv); 
  searchItems.push(adv);
}

public function onAtomCreated(e:Event):void
{
   var adv:AdvanceSearchAtom = e.currentTarget as AdvanceSearchAtom;
   if(!adv)
      return;
   var index:Number = searchItems.indexOf(adv);
   adv.field.text = arr[index].field;
   adv.value.selectedIndex = arr[index].value;
}

Using data binding:

Inside AdvanceSearchAtom.mxml

<mx:TextInput id="field" text="{textValue}"/>
<mx:ComboBox id="value" selectedIndex="{comboIndex}"/>

In the script block of AdvanceSearchAtom.mxml

[Bindable]
public var textValue:String;

[Bindable]
public var comboIndex:Number;
var adv:AdvanceSearchAtom;

In the AdvanceSearch class:

for(var i:int=0;i<arr.length;i++) {
  adv = new AdvanceSearchAtom();
  container.addChild(adv); 
  adv.field.text = arr[i].field; 
  adv.value.selectedIndex = arr[i].value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文