Basically, there are times when you need to dynamically add a additional
rows. Lets say that you have a tool to track meetings and you want to be
able to add attendees. The problem is that you don't know if there was 1
attendee or 10. So if you statically include, say, 3 rows of fields (Name,
email address, main contact phone number), what if there were 4? How would
you add another row of fields? In HTML, this is pretty easy. (Again, the
code is listed below.) Is there a way to do this with Struts?
This might be a silly question but do you have to stick with the tag
libraries? For example, can I use <html:textfield> or something for the
other fields in my form, but use standard HTML for the remainder? Will
Struts pick up the raw HTML stuff?
In cases where you guys have had to write an app where multiple entries need
to be made, but the number of entries is unknown at compile-time, how do you
address it?
Here is the HTML; would like to be able to do something similar with Struts:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script language="javascript">
totalCount = 0;
function nextLayer()
{
// Below is the HTML I want to replace in the document.
// In addition to the form fields to add, I add another <div>
// section with the next ID in sequence. Each time I hit the Add
// button, I get another row of fields.
myStr = "<table width='100%' border='0' cellspacing='0'
cellpadding='0'>";
myStr += "<tr valign='top'> ";
myStr += "<td>Assigned to<br>"
myStr += "<input type='text' name='assignedTo" + totalCount + "' size='30'
maxlength='100'>";
myStr += "</td>";
myStr += "<td>Due Date<br>";
myStr += "<input type='text' name='dueDate" + totalCount + "' size='15'
maxlength='20'>";
myStr += "</td>";
myStr += "<td>Action Item notes<br>";
myStr += "<textarea name='notes" + totalCount + "' cols='50'
rows='5'></textarea>";
myStr += "</td>";
myStr += "</tr>";
myStr += "</table>";
myStr += "<br><div id='fieldTotal" + (totalCount + 1) + "'></div>";
document.getElementById("fieldTotal"+totalCount).innerHTML = myStr;
totalCount++;
}
</script>
<body bgcolor="#FFFFFF" text="#000000">
<p> </p>
<div id="fieldTotal0"></div>
<input type="button" name="Submit" value="Add" onClick="nextLayer()">
<p> </p>
</body>
</html>
Darrel
I'm not sure I interpreted this the same way. I'll read it again, though.
Thanks!
- Eric
"Darrel Riekhof" <rie...@lvcm.com> wrote in message
news:1IVM9.18914$6k.12...@news1.west.cox.net...
> Dynamically, though? As in, the form is displayed and I hit "Add another"
> and it automatically ads another set of fields? (This doesn't "refresh"
> the form. It uses the InnerHTML (method?) to insert new HTML into the
> existing html-page/form.
What do you mean by "refresh" the form?
I start out giving the user a blank line (text box). When they fill that
in, and submit, they get the same form back, showing what they entered,
plus a blank line below it for the next entry.
I use JSTL to iterate <c:forEach> but you can also use Struts
<logic:iterate> tags to do it.
Remember that Struts code executes on the server. Once the page has arrived
at the client, and until it gets submitted to the server again,
Struts/Servlet/JSP code can't affect the page. If avoiding that round trip
is important to you, then use JavaScript to modify the page while it's at
the client.
--
Wendy in Chandler, AZ
This is exactly my dilemma -- There are other fields to my form. I
thought about doing a "Wizard" kind of thing, where it has you fill out some
of the form first, then you go to another form that can allow you to add,
submit, add, submit, etc... Until you have all the rows of data submitted
for the data that iterates "unknown" times. This really isn't ideal and
Microsoft showed us all how much we really -hate- wizards.
Albeit the code I provided doesn't show it, I ended up getting rid of
the numbering squence for the field names. i.e. duedate0, duedate1,
duedate2, etc. If I just use "duedate", and I add say, 3 dates, I get a
String[] array of due dates. Easy enough. So, with that, lets say I do the
following:
I create one row of fields, lets say: "Name", "Duedate" and
"ActionItem". The resulting HTML code would be something like:
<input type="text" name="name">
<input type="text" name="duedate">
<input type="text" name="ActionItem">
So, if I know what the resulting HTML will be, why couldn't I just
insert that?
The first thing that I thought was that the ActionForm (is that right?)
wouldn't be able to validate it. It's expecting a String for "name", not a
String[] name. Or can it? Can I define my accessor/settor methods as:
public void setAiName (String[] name)
and
public String[] getAiName ()
Would that work, even if I inserted x number of "html" rows in my form
as I've done above (because I can predict what the JSP will create at
request time?)
Maybe I'm not explaining it right. I do appreciate your help!
- Eric
"Wendy S" <wend...@hotmail.com> wrote in message
news:Qw2N9.25159$vb3.1...@news2.west.cox.net...
http://www.keyboardmonkey.com/StrutMonkey/MonkeyStruts.jsp
http://www.keyboardmonkey.com/StrutMonkey/MonkeyStruts_v2.jsp
I haven't used MonkeyStruts. I managed to bend the Struts framework to make
it do what I needed it to. But it sounds like you need your users to be
able to add something, add a second thing, and then still be able to *edit*
the first item.
In my case, once they've added a line, they can delete it, but they can't
change it. That makes things a little different.
Which way is it for your project?
Basically, the app is for follow up from a meeting. In a meeting, we
will have people from my group, people from other groups and even people
from other companies. We almost always have action items, as well. The
problem is, in each meeting, there may only be one person or 10 people from
my team, etc. In addition, there are other fields that need to be filled
out. Meeting minutes, notes, etc, etc.
I'm not sure how this would work in a Windows app, either. How do you
add (whether you can add them or delete them) an unknown entity of
something?
Maybe the action-item example would work better: We may only have one
action item or 20. You never know. But it would be nice, on the same form,
to keep hitting "Add" and then have the fields appear on the same form
without having to reload a form with the new data included, or doing a
wizard kind of thing where you type in some data, hit Submit. Add some more
data, Hit submit.
The other thing I was thinking I could do, albeit I still see it as a
kludgy way to do it is to have a main form, and in that form I'd have a
field for "Number of Attendees" and then a second form that has Attendee
Information fields. Since I know at view-time, how many fields I would
need, it would be easy to create. But I also have to keep in mind that: I
would need fields for my attendees, other company's attendees as well as
action items. -4- seperate forms. It can get pretty out-of-hand.
I'm still learning about Struts so. I'll check out MonkeyStruts and see
if that helps; otherwise I might be able to divise something on my own.
- Eric
"Wendy S" <wend...@hotmail.com> wrote in message
news:Vy9N9.27878$vb3.1...@news2.west.cox.net...
I checked it out and its very close to what I want -- But it also
re-displays the form. You can do stuff with Dynamic HTML which will display
it right away, without having to reload the form. The example of that is in
the original posting. I've included it here again. I think you can drop
this in an IE 5.5 or later browser and see how it performs.
- Eric
"Wendy S" <wend...@hotmail.com> wrote in message
news:Vy9N9.27878$vb3.1...@news2.west.cox.net...
What you appear to be asking is, can Struts handle multiple fields, not
knowing how many there are. I *think* the answer is yes, if you set up a
setAssingedTo( String[] names ) method in your form bean and name all of
the 'assignedTo' text areas with the same name, probably using the iterate
or forEach tag. And then do whatever you need to do with DHTML to add more
form elements, also with that same name.
When you've tried that, let us know if it works. I'm fairly sure that's the
way that select lists that allow multiple choices are handled, so I don't
see why it wouldn't work for multiple text boxes. When it hits the server,
you can't tell from the HttpServletRequest what the original HTML form
element was, it's just a bunch of name/value pairs.
I am *not* certain if order is guaranteed, so you might end up with a
String[] of assignedTo, and a String[] of dueDate, but they may not match
up by position. In which case you're out of luck with this approach.
What kind of load do you expect? That round trip to the server is
potentially not that big of a deal. Yes, it's nice to avoid it if you can,
but if it's going to unnecessarily complicate your life, then spend the
extra second or two and let the server handle the difficult stuff.