Flex中的动态问卷 [英] Dynamic questionnaire in Flex

查看:84
本文介绍了Flex中的动态问卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Flex 3中为调查表创建一个模板,该模板读取动态XML文件并创建调查表.示例XML:

I want to make a template for questionnaires in Flex 3, that reads dynamic XML file and creates a questionnaire. An exemplary XML:

<test>

<question>
  <q>Who was born earlier?</q>
  <answer value="true">Vincent van Gogh</answer>
  <answer value="false">Piet Mondrian</answer>
</question>

<question>
  <q>What color is Dutch national flag?</q>
  <answer value="false">blue, red and green</answer>
  <answer value="false">green, red and white</answer>
  <answer value="true">blue, red and white</answer>
</question>

<question>
  <q>Which season is the coldest?</q>
  <answer value="false">fall</answer>
  <answer value="true">winter</answer>
  <answer value="false">spring</answer>
  <answer value="false">summer</answer>
</question>

</test>

问题和答案的数量可能会有所不同.计划是使用带单选按钮的嵌套转发器(一个用于回答问题,然后在另一个内部回答).我可以将所有question.q保存到一个ArrayCollection,但是如果每个问题中只有几个带有相同"answer"标签的答案,该如何处理?以及如何访问每个属性的值"属性,以检查用户是否选择了正确答案?

The amount of questions and answers may vary. The plan was to use nested repeaters with radio buttons (one for the questions and then another inside for the answers). I can save all question.q to an ArrayCollection, but how should I handle my answers, if there are few of them with the same "answer" tag within each question? And how can I access "value" property of each, to check if the user chose correct answer?

推荐答案

您需要根据XML创建域模型.不要跳过此步骤,因为它比您想象的更容易且更直接.首先创建一个简单的类:

You need to create a domain model from the XML. Don't skip this step because it's easy to do and more straightforward than you think. Start by creating a simple class:

public class Question {
   public var question : String;
   public var answers : ArrayCollection = new ArrayCollection();

   public Question( node : XML ) {
      question = node.q.text();
      for each( var answer : XML in question.answer ) {
          answers.addItem( new Answer( answer ) );
      }
   }
}

public class Answer {
   public var text : String;
   public var correct : Boolean;

   public Answer( node : XML ) {
      text = node.text();
      correct = Boolean(node.@value);
   }
}

像这样填充节点的ArrayCollection:

Populate an ArrayCollection of your nodes like so:

var questions = new ArrayCollection();
for each( var node : XML in xml.question ) {
   questions.addItem( new Question( node ) );
}

大致如此.然后,您的问题数组可以用作转发器的dataProvider.并且question.answer可以用作内部中继器的中继器.

That's roughly it. Then your questions array can serve as the dataProvider for the repeater. And question.answers can serve as the repeater for the inner repeater.

这篇关于Flex中的动态问卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆