• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating fields dynamically

 
Vighnesh Mu
Greenhorn
Posts: 29
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a requirement where I need to generate a web page in that number of fields is not fixed,so I have to generate them dynamically using a plus button to add extra fields by users. Can somebody please assist me how can I go on this one and how to collect the values entered in those fields in java and persist them to DB as I can't fix number of table also.

Thanks,
Vighnesh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you thought of so far? How might you accomplish this?
 
Vighnesh Mu
Greenhorn
Posts: 29
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply.

I thought to use simple jsp tags or struts 2 tags but didn't come to know how can I do this. I hope you got my requirement correctly, my requirement is that "I need to have 3 fixed fields as a group and need to provide a plus button. on click of that plus button it should generate 3 more fields and so on" can you tell me how can I accomplish this?

Thanks,
Vighnesh
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a high level, 2 means come to mind:

1) Using just JSP: When the button is clicked, the page is submitted so that the JSP can be re-ecexcuted putting the desired number of fields on the page. Use <c:if> for making descisions, and <c:forEacj> for looping.

2) Using JavaScript: When the button is clicked, script creates the new fields on the fly without any need to go back t the server.

The first method is terribly old-fashioned.
 
Vighnesh Mu
Greenhorn
Posts: 29
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please see the attachment which shows my requirement. how to do this?

Thanks,
Vighnesh
requirement.jpg
[Thumbnail for requirement.jpg]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you two approaches. If you are looking for someone to write the code for you, you're in the wrong place.
 
Vighnesh Mu
Greenhorn
Posts: 29
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Without using javascript can't I do this other than the one way you provided? Because I can't use javascript as per my requirement. Is there any way to do this in struts 2? If yes can you help some sites where I can get examples or the way to implement this?

Thanks
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Doing this in javascript is simple such as

<html>
<head>
<script language="Javascript" type="text/javascript">

var counter = 1;
var limit = 3;
function addInput(divName){
if (counter == limit) {
alert("Reached adding limit" + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.innerHTML = "Entry " + (counter + 1) + "
<input type='text' name='myInputs[]'>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}

</script>
</head>


<body>
<form method="POST">
<div id="dynamicInput">
Entry 1
<input type="text" name="myInputs[]">
</div>
<input type="button" value="Add another text input" onClick="addInput('dynamicInput');">
</form>
</body>
</html>

But even I want to know how the same can be achieved in struts2.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without JavaScript to issue Ajax request, you are limited to refreshing the page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic