cancel
Showing results for 
Search instead for 
Did you mean: 

How to Create Dynamic RadioButton Group by Index

Former Member
0 Kudos

Hi to all,

I would like to create an Questionaire.For that, I have created one Text View & Radio button Group by index at design time. which will fetch the values(ie Values are Questions & Ans options ) from R/3. since I need to fetch N number of values which must be mapped with Multiple Text views & Radio Button Groups i want to create Text view & Radio button Group by index UI Elements dynamically.How to do that?

Please bring me an solution to this problem.

Thanks in advance.

Regards,

Malar.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

A simple solution could look like this:

Context:

Questions (node)
-- text (attribute, string)
-- correctAnswer (attribute, int)
-- Answers (node, singleton=false)
---- text (attribute, string)

UI elements:

QuestionText (TextView), "text" -> Questions.text
AnswerSelector (RadioButtonGroupByIndex), "texts" -> Questions.Answers.text

Fill your context like this:

IQuestionsElement question = wdContext.nodeQuestions().createAndAddQuestionsElement();
question.setText("How do you like Web Dynpro?");
String[] answers = {
  "I like it", "Don't like it at all", "I don't care"
};
for (int i = 0; i < answers.length; ++i)
{
  IAnswersElement answer = question.nodeAnswers().createAndAddAnswersElement();
  answer.setText(answers<i>);
}
question.setCorrectAnswer(0);

Assign an action to the "onSelect" event of the radio button group to check if the selected option corresponds to the correct answer and you are done.

Things get more difficult, if you want to display all questions/answers at the same time and not only the lead-selected element of the Questions node.

In this case, use a RowRepeater (if available in your Web Dynpro version).

If this is not available, you have to create a separate context node for each question programmatically and also create a corresponding number of separate TextView/RadioButtonGroups and bind their properties to these nodes.

Armin

Former Member
0 Kudos

Hi,

In wdDoModifyView you can create UI elements at runtime

IWDTextView tv = view.createElement(IWDTextView.class,"TextView1");

IWDRadioButtonGroupByIndex rbg = view.createElement(IWDRadioButtonGroupByIndex.class,"RBG1");

IWDTranparentContainer tc = (IWDTransaprentContainer)view.getElement("RootUIElementContainer");

tc.addChild(tv);

tc.addChild(rbg);

Regards,

Murtuza

Former Member
0 Kudos

Hi,

Thanks for your solution. but still i am facing an problem, here i have included my code what i wrote inside modify()

if(firstTime){

int j=0;int id=0;

while(j<wdContext.nodeTest1().size())

{

int Iter=j;

IWDTransparentContainer tc = (IWDTransparentContainer)view.getElement("TransparentContainer");

IWDTextView tv =(IWDTextView) view.createElement(IWDTextView.class,"MyTextView1"+id);

tv.setText(wdContext.nodeQuestions().getQuestionsElementAt(id).getQues());

tc.addChild(tv);

for(;Iter<j4;Iter+)

{

Test2Elt=wdContext.nodeTest2().createTest2Element();

Test2Elt.setRadioTest(wdContext.nodeTest1().getTest1ElementAt(Iter).getRadioTest());

Test2Node.addElement(Test2Elt);

IWDRadioButton rb = (IWDRadioButton)view.createElement(IWDRadioButton.class,"RBG1"+Iter);

// rb.setSelectedKey(wdContext.nodeTest2().getTest2ElementAt(Iter).getRadioTest());

rb.setText(wdContext.nodeTest2().getTest2ElementAt(Iter).getRadioTest().toString());

tc.addChild(rb);

}

j=Iter;

id++;

}

}

Here my Problem is,

Here all the Radio Buttons are in disabled mode i have tried with rb.setSelectedKey(wdContext.nodeTest2().getTest2ElementAt(Iter).getRadioTest());

But that is not Working . Please bring me an solution

Regards,

Thenmalar

Former Member
0 Kudos

Hi,

You have not binded the selectedKey property of the RB that you have created.

Create a value attribute called sample

then write this code

IWDRadioButton rb = (IWDRadioButton)view.createElement(IWDRadioButton.class,"RBG1"+Iter);

<b>IWDAttributeInfo attr = wdContext.getNodeInfo().getAttribute("sample");

rb.bindSelectedKey(attr);</b>

Now, this would enable your RB.

Warm Regards,

Murtuza

Former Member
0 Kudos

Hi,

For the radio button to be enabled you have to bind the

selectedKey and keyToSelect.

Regards

Ayyapparaj

Former Member
0 Kudos

Hi,

I created a sample Value attribute of type string as you said. And assign the value

wdTest2Elt.setSample(wdContext.nodeTest1().getTest1ElementAt(Iter).getRadioTest()); like this .after that i proceed with your code what you have mentioned.

But still the same problem persists. since all the values i am getting from R/3 dynamically i couldnot bind with DDIC for the type of sample.

Regards,

Malar

Former Member
0 Kudos

Hi,

IWDRadioButton rb = (IWDRadioButton)view.createElement(IWDRadioButton.class,"RBG1"+Iter);

IWDAttributeInfo attr = wdContext.getNodeInfo().getAttribute("sample");

rb.bindSelectedKey(attr);

<b>rb.setKeyToSelect("Key_1");

wdContext.currentContextElement.setSample("Key_1");</b>

Regards,

Murtuza

Former Member
0 Kudos

Hi Murtuza,

Thanks for ur solution...

anyway again i got an same error. Option button not enabled...

so i have created a dynamic context node & map it with the RadioButton Group by Index...Now it s working fine...

if(firstTime){

//Test2Node.invalidate();

for(int i=0;i<wdContext.nodeQuestions().size();i++)

{

IWDNodeInfo nodeinfo=wdContext.getNodeInfo();

/* This will add a ValueNode */

IWDNodeInfo customerNode=nodeinfo.addChild("Questionaire"+i,null,true,true,true,false,false,true,null,null,null);

/* This will add a Attribute to the above Node */

IWDAttributeInfo custAttr=customerNode.addAttribute("Quest"+i,"com.sap.dictionary.string");

IWDAttributeInfo custAttr1=customerNode.addAttribute("Options"+i,"com.sap.dictionary.string");

IWDNode NodeQuest = wdContext.getChildNode("Questionaire"+i,0);

NodeQuest.getCurrentElement().setAttributeValue("Quest"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getQues());

// NodeQuest.getCurrentElement().setAttributeValue("Options"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getOptionA());

IWDTransparentContainer tc = (IWDTransparentContainer)view.getElement("TransparentContainer");

IWDTextView tv =(IWDTextView) view.createElement(IWDTextView.class,"MyTextView1"+i);

tv.setText(wdContext.nodeQuestions().getQuestionsElementAt(i).getQues());

tc.addChild(tv);

IWDNodeElement NodeElt= NodeQuest.createElement();

NodeElt.setAttributeValue("Options"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getOptionA());

NodeQuest.addElement(NodeElt);

IWDNodeElement NodeElt1= NodeQuest.createElement();

NodeElt1.setAttributeValue("Options"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getOptionB());

NodeQuest.addElement(NodeElt1);

IWDNodeElement NodeElt2= NodeQuest.createElement();

NodeElt2.setAttributeValue("Options"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getOptionC());

NodeQuest.addElement(NodeElt2);

IWDNodeElement NodeElt3= NodeQuest.createElement();

NodeElt3.setAttributeValue("Options"+i,wdContext.nodeQuestions().getQuestionsElementAt(i).getOptionD());

NodeQuest.addElement(NodeElt3);

IWDRadioButtonGroupByIndex rbg = (IWDRadioButtonGroupByIndex)view.createElement(IWDRadioButtonGroupByIndex.class,"RBG1"+i);

rbg.bindTexts("Questionaire"i".Options"+i);

//IWDRadioButton rb = (IWDRadioButton)view.createElement(IWDRadioButton.class,"RBG1"+Iter);

//rb.bindText("Test2.RadioTest");

tc.addChild(rbg);

}

This s the code....

Thank you. anyway

Regards,

Thenmalar

Former Member
0 Kudos

Hi,

But One Problem is , I get Five Option Buttons Instead of four the First option value is Empty & selected, All the remaining Option Buttons for the particular question having the value but not selected...

so i guess there is some problem with the selection property only ....

Please bring me the Solution.....

Regards,

Thenmalar

Former Member
0 Kudos

Some remarks: It is sufficient to have unique node names, so you can name the attributes without using the index i.

The node that holds the radio-button-group items (Questionaire) should have cardinality 0:N, selection 0:1.


/* This will add a ValueNode */
IWDNodeInfo customerNode = nodeinfo.addChild
(
  "Questionaire" + i,
  null,
  true, /* singleton */
  false, /* mandatory */
  true, /* multiple */
  false, /* mandatorySelection */
  false, /* multipleSelection */
  false, /* initializeLeadSelection */
  null,
  null,
  null
);

Armin