当传递具有单个元素的数据提供者(数组)时,转发器无法正常工作

发布于 2024-10-19 14:19:19 字数 1656 浏览 7 评论 0原文

我在 Accordian 中使用 Repeater,它似乎在 userArray 中看不到单个元素。如果我向 userArray 添加另一个条目,那么中继器就可以正常工作。

想法??

    private function currUsersServiceHandler(event:ResultEvent):void{

                    if (event.result.currentUsers != null)
                    {
                        if (event.result.currentUsers.user is ArrayCollection) // if more than one elements are present
                        {
                            usersArray = event.result.currentUsers.user;
                        } 
                        else if (event.result.currentUsers is ObjectProxy)
                        { //FIXIT usersArray populate by following line has some issue
                            usersArray = new ArrayCollection(ArrayUtil.toArray(event.result.currentUsers));
                        }
                    }
                }

        <mx:HTTPService id="currUsersService" url="currUsers.xml" result="currUsersServiceHandler(event)"/>

<mx:Accordion includeIn="UserList" x="10" y="10" width="554" height="242" >
        <mx:Repeater id="rep" dataProvider="{usersArray}">
            <mx:Canvas width="100%" height="100%" label="{rep.currentItem.firstName}" >
                    <mx:HBox>
                        <s:Label text="{rep.currentItem.firstName}"/>
                        <s:Label text="{rep.currentItem.lastName}"/>
                      <mx:/HBox>
            </mx:Canvas>
        </mx:Repeater>  
</mx:Accordian>

编辑:

我刚刚注意到另一件事,即手风琴确实显示一个选项卡(当数组有一个元素时),但它没有标有我设置的名字。如果我输入另一个用户,则会出现两个选项卡,并且两个选项卡都标有我正在设置的名称。第一个选项卡也会出现标签。

I am using a Repeater in an Accordian which does not appear to see a single element in userArray. If I add another entry to userArray then the Repeater works fine.

Thoughts??

    private function currUsersServiceHandler(event:ResultEvent):void{

                    if (event.result.currentUsers != null)
                    {
                        if (event.result.currentUsers.user is ArrayCollection) // if more than one elements are present
                        {
                            usersArray = event.result.currentUsers.user;
                        } 
                        else if (event.result.currentUsers is ObjectProxy)
                        { //FIXIT usersArray populate by following line has some issue
                            usersArray = new ArrayCollection(ArrayUtil.toArray(event.result.currentUsers));
                        }
                    }
                }

        <mx:HTTPService id="currUsersService" url="currUsers.xml" result="currUsersServiceHandler(event)"/>

<mx:Accordion includeIn="UserList" x="10" y="10" width="554" height="242" >
        <mx:Repeater id="rep" dataProvider="{usersArray}">
            <mx:Canvas width="100%" height="100%" label="{rep.currentItem.firstName}" >
                    <mx:HBox>
                        <s:Label text="{rep.currentItem.firstName}"/>
                        <s:Label text="{rep.currentItem.lastName}"/>
                      <mx:/HBox>
            </mx:Canvas>
        </mx:Repeater>  
</mx:Accordian>

Edit:

There is another thing I have just noticed i.e. that the accordian does show a single tab (when Array has a single element) but it's not labeled with the first name which I am setting. If I enter another user, two tabs appear and both are labeled with names I am setting. The first tab appears labeled too then.

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

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

发布评论

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

评论(1

够运 2024-10-26 14:19:19

对我来说,这不适用于 dataProvider 中的 1 个项目,但适用于两个项目,这是没有意义的。

也就是说,这种方法让我觉得很复杂,而且我倾向于根本不使用中继器。我建议采用不同的方法。

首先创建一个组件来显示您的数据。您可以重用现有的代码。从概念上讲是这样的:

        <mx:Canvas width="100%" height="100%"  >
<mx:Script><[[ 
 public var user : Object;
]]></mx:Script>
                <mx:HBox>
                    <s:Label text="{user.firstName}"/>
                    <s:Label text="{user.lastName}"/>
                  <mx:/HBox>
        </mx:Canvas>

然后在原始组件中用 ActionScript 创建该组件的新实例:

for each(var myUserObject : Object in usersArray){
  var newUserDisplayObject : UserDisplayObject = new UserDisplayObject();
  newUserDisplayObject.user = myUserObject;
  newUserDisplayObject.label = myUserObject.firstName
  accordian.addChild(newUserDisplayObject);
}

It makes no sense to me that this would not work with 1 item in the dataProvider, but would work with two.

That said, tThis approach strikes me as convoluted and I tend to stay away from using repeaters at all. I would suggest a different approach.

First create a component to display your data. You can reuse you're existing code. Conceptually something like this:

        <mx:Canvas width="100%" height="100%"  >
<mx:Script><[[ 
 public var user : Object;
]]></mx:Script>
                <mx:HBox>
                    <s:Label text="{user.firstName}"/>
                    <s:Label text="{user.lastName}"/>
                  <mx:/HBox>
        </mx:Canvas>

Then in your original component create the new instance of the component in ActionScript:

for each(var myUserObject : Object in usersArray){
  var newUserDisplayObject : UserDisplayObject = new UserDisplayObject();
  newUserDisplayObject.user = myUserObject;
  newUserDisplayObject.label = myUserObject.firstName
  accordian.addChild(newUserDisplayObject);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文