Sanjay Kumar
Sanjay Kumar

Reputation: 379

Dynamically add Text Field and drop-down in form on button click event in wicket.?

i am trying to add Text Field and Drop Down box in the form on button click event using wicket framework. using JavaScript to generate Text Field and drop-down dynamically with unique wicket:id on html page.at java page how can i store the values of dynamically generated Text Field in a string type array at event of submit button.

Java Script at HTML to generate Text Field and Drop-Down Box

<SCRIPT type="text/javascript">
var intTextBox=0; 
function f()
{

   intTextBox = intTextBox + 1; 
  var testdiv = document.getElementById('testdiv'); 
  var testdiv1 = document.getElementById('testdiv1'); 
  var newTBDiv = document.createElement('div');
  var newTBDiv1 = document.createElement('div');
  var data0="A";
  var data1="B";
  var data2="C";
  var data3="D";
  var data4="E";
  var data5="F";

newTBDiv.setAttribute('id','strText'+intTextBox); 
newTBDiv.innerHTML = "Field "+intTextBox+":<input type='text' wicket:id='id"+intTextBox + "' size='"+50+"' maxlength='"+100+"''/>"; 
newTBDiv1.innerHTML = "<select wicket:id='dropdownid"+intTextBox+"' style=width:10em;> <option>"+data0+"</option> <option>"+data1+"</option> <option>"+data2+"</option> <option>"+data3+"</option> <option>"+data4+"</option> <option>"+data5+"</option></select>"; 
testdiv.appendChild(newTBDiv); 
testdiv1.appendChild(newTBDiv1);
return true;
}
</SCRIPT>

in Java file i am trying to do

  String textFielddata[] = new String[intTextfieldcount];
  String textFieldtype[] = new String[intTextfieldcount];
  int count=0;
  for(int i=0;i<intTextfieldcount;i++)
            {
                count=count+1;

                form.add(new TextField<String>"id"+count).setRequired(true));
                form.add(new new DropDownChoice<String>("dropdownid"+count).setRequired(true));
                //downControl.add(new TextField<String>("id"+count,new PropertyModel<String>(this,"id"+count))).setOutputMarkupId(true);
                textFielddata[i]=form.getDefaultModelObjectAsString("id"+count);
                textFieldtype[i]=form.getDefaultModelObjectAsString("dropdownid"+count);
System.out.println(textFielddata[i]);
System.out.println(textFieldtype[i]);
            }

textFieldData[] and textFieldtype[] is string type array, inttextfieldcount is integer type and its value is equal to number of dynamically generated Text Field and drop-down box

output: id1 dropdownid1

id2 dropdownid2

The Problem is it is not showing the data from dynamically generated Text Field and select value of Drop-Down box

or suggest me any other way in Wicket to implement this functionality without using JavaScript

Upvotes: 0

Views: 1749

Answers (2)

Sanjay Kumar
Sanjay Kumar

Reputation: 379

   final MarkupContainer rowPanel = new WebMarkupContainer("rowPanel");
 rowPanel.setOutputMarkupId(true);

 form.add(rowPanel);

// List all rows

 ArrayList rows = new ArrayList(2);
 rows.add(new String());
 rows.add(new String());
 final ListView lv = new ListView("rows", rows) {



 @Override
   protected void populateItem(ListItem item) {
     int index = item.getIndex() + 1;
     item.add(new Label("index", index + "."));

     TextField text = new TextField("text", item.getModel()));
     item.add(text);
   }
 };
 rowPanel.add(lv);

 AjaxSubmitLink addLink = new AjaxSubmitLink("addRow", form) {
   @Override
   public void onSubmit(AjaxRequestTarget target, Form form) {
     lv.getModelObject().add(new String());
     if (target != null)
       target.addComponent(rowPanel);
   }
 };
 addLink.setDefaultFormProcessing(false);
 rowPanel.add(addLink);

Upvotes: 0

iluwatar
iluwatar

Reputation: 1803

You can create the dropdown and the textfield normally and just make them hidden initially. Then use the button clicked event handler to show the components via ajax.

Upvotes: 2

Related Questions