JSP problem
Hi,
I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.
<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
out.println("
");
out.println("<table width=\"100%\" border=\"1\" bordercolor=\"Black\" cellspacing=\"0\" cellpadding=\"0\">");
out.println("<tr>");
out.println("<td class=\"bodyTableDataCol\" nowrap width=\"6%\" height=\"20\">Transfer Amount</td>");
out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Value Date</td>");
out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Customer Reference</td>");
out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Detail</td></tr>");
//for ( int row = 1; row >= intPer_page; row++ ){
out.println("<tr>");
out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + TransAmt + "</td>");
//out.println("<input type=\"text\" class=\"formItemMessageLabel\" name=\"FromValueDate\" width=\"12%\" height=\"20\">" value='" + FromValueDate + "' onChange=\"upperMe(this)\">");
//out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + FromValueDate + "</td>");
out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">");
out.println("<input type=\"text\" name=\"ValueDate\" size=\"11\" maxlength=\"11\" value='" + FromValueDate + "' onChange=\"upperMe(this)\">");
out.println("<A HREF=\"javascript:doNothing()\" onMouseOver=\"showtip(this,event,'Select from Calendar')\" onMouseOut=\"hidetip()\" onClick=\"setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('/mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')\">");
out.println("<IMG SRC=\"/mwintl11/images/calendar.gif\" BORDER=0></A></td>");
out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + CustRef + "</td>");
out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">");
out.println("<input value=\"Edit\" name=\"Edit\" tabindex=\"25\" type=\"button\" onClick=\"openNew()\" class=\"formButton\">");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
%>
The drop down name is "per_page".
Can anyone please help on this?
Thanks.
[3211 byte] By [
nsahooa] at [2007-11-27 7:04:33]

First, use JSP. You might as well be writing a servlet if you're gonig to do it like you have it below.
If I understand your problem correctly, you want to change a table's contents based on the selection of a dropdown. Do you want to do this w/o reloading the page? If so then you will need to create something lile a <span> or a <div> and change its text value using JavaScript.
I think its something like span.innerHTML="new data"
Here, the "new data" is the table.
If you are allowed to reload the page then your selection in the drop down should create a request with the selected value present.
Ex: if you use GET to load the page then GET the same url with &dropdown=<selected value>
> First, use JSP. You might as well be writing a> servlet if you're gonig to do it like you have it> below.That IS a JSP, amazingly enough. Note that all of that Java code is actually a JSP scriptlet!
Yes,that is a JSP and the java code written within scriplet. I need to load the page according to the number selection from drop down. I could get the value of the drop down through request object. My problem is to get the same row the number of times on selection of the drop down. I tried to put the for loop but it gave me some problem. Can you suggest something?
Thanks.
> Yes,that is a JSP and the java code written within scriplet.
I can see that. Doing it that way makes it even more unreadable than JSP is normally. And what you did there is a WTF. Why not simplify it like this:<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
%>
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
That at least would make it readable, so we might be able to understand what it had to do with your question.
Hi,
I have modified the code to jsp and repeating the query again.
I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.
<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
%>
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td></tr>
<tr>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton">
</td>
</tr>
</table>
The drop down name is "per_page".
Thanks.
Sorry. I think all things meshed up.
I have modified the code to jsp and repeating the query again. I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.
<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
%>
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>
</tr>
<tr>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"> <input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"> </td>
</tr>
</table>
The drop down name is "per_page".
Thanks.
What you should look at are the JSTL tag libraries. These allow you to put most loop and conditionals you might need in a JSP without mucking about with scriptlets (which are considered something of a last resort, these days).
Hi,I am not using JSTL. I need this in simple JSP. I have to display a grid of records according to the dropdown number selection. Like if i select 3 then i should show 3 rows with same values int he rows.Thanks.
> Hi,
>
> I am not using JSTL.
Why not?
> I need this in simple JSP.
Well it can be done with scriplets, but don't use all those prints. If you want to implement a loop with scriplets, for example, you do it something like;
<select name="fred">
<% for(String value : values) { %>
<option><%= value %></option>
<% } %>
</select>
Hi,
Just for understanding I did like this.
Script:
function updatePerPageCount(objname)
{
//alert('I am inside updatePerPageCount');
var perpageStr, perpageInt;
perpageInt = objname.options.selectedIndex;
perpageStr = objname.options[perpageInt].value;
if (perpageStr != null)
{
document.RepetitiveInitiation.per_page.value = perpageStr;
return (true)
}
}
Setting in Java and getting that in JSP:
<%
String per_page = (String)request.getAttribute("per_page");
if (per_page == " " || per_page == null)
{
per_page = "";
}
%>
JSP:
<select name="per_page" style="width:50px" class="searchLabelWhite" onChange="return updatePerPageCount(this);">
<option value="1" ><%= per_page.equals("1") ? "selected" : "" %>>1</option>
<option value="2" ><%= per_page.equals("2") ? "selected" : "" %>>2</option>
<option value="3" ><%= per_page.equals("3") ? "selected" : "" %>>3</option>
<option value="4" ><%= per_page.equals("4") ? "selected" : "" %>>4</option>
<option value="5" ><%= per_page.equals("5") ? "selected" : "" %>>5</option>
</select>
And the grid:
<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
%>
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>
</tr>
<tr>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"></td>
</tr>
</table>
In this grid can't I use a for loop to get the rows? Here I need to get the number of rows selected from dropdown (per_page) and i need the same rows number of times as selected from drop down. I am getting the per_page value but couldn't able to loop it here. Can you help me on this?
Thanks.
Nobody can possibly be expected to follow that, please use code tags and sane line breaks.
Hi,
Just for understanding I did like this.
Script:
function updatePerPageCount(objname)
{
var perpageStr, perpageInt;
perpageInt = objname.options.selectedIndex;
perpageStr = objname.options[perpageInt].value;
if (perpageStr != null) {
document.RepetitiveInitiation.per_page.value = perpageStr;
return (true)
}
}
Setting in Java and getting that in JSP:
<%
String per_page = (String)request.getAttribute("per_page");
if (per_page == " " || per_page == null)
{ per_page = "";
}
%>
JSP:
<select name="per_page" style="width:50px" class="searchLabelWhite" onChange="return updatePerPageCount(this);">
<option value="1" ><%= per_page.equals("1") ? "selected" : "" %>>1</option>
<option value="2" ><%= per_page.equals("2") ? "selected" : "" %>>2</option>
<option value="3" ><%= per_page.equals("3") ? "selected" : "" %>>3</option>
<option value="4" ><%= per_page.equals("4") ? "selected" : "" %>>4</option>
<option value="5" ><%= per_page.equals("5") ? "selected" : "" %>>5</option>
</select>
And the grid:
<%
Vector TransAmt = (Vector)request.getAttribute("TransAmt");
Vector CustRef = (Vector)request.getAttribute("CustRef");
%>
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>
</tr>
<tr>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"></td>
</tr>
</table>
In this grid can't I use a for loop to get the rows? Here I need to get the number of rows selected from dropdown (per_page) and i need the same rows number of times as selected from drop down. I am getting the per_page value but couldn't able to loop it here. Can you help me on this?
Thanks.
> In this grid can't I use a for loop to get the rows?
Of course
> Here I need to get the number of rows selected from
> dropdown (per_page) and i need the same rows number
> of times as selected from drop down. I am getting
> the per_page value but couldn't able to loop it
> here. Can you help me on this?
>
I just did. Put your loop start and end code in separate scriptlets, then use <%= expression %> to insert values from variables into the text.
Hi,
Thanks for your immediate response. But still have some problem. I tried to loop like this but couldn't get any rows.
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>
</tr>
<%
for (int j = 0; j > per_page.length(); j++)
{
%>
<tr>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">
<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="modifyPayment()" class="formButton"></td>
</tr>
<%
}
%>
Can you suggest?
Thanks.
Hi,
Thanks for your immediate response. But still have some problem. I tried to loop like this but couldn't get any rows.
<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">
<tr>
<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>
<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>
</tr>
<% for (int j = 0; j > per_page.length(); j++)
{
%>
<tr>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">
<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">
<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('/mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">
<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=CustRef%></td>
<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">
<input value="Edit" name="Edit" tabindex="25" type="button" onClick="modifyPayment()" class="formButton"></td>
</tr>
<%
}
%>
Can you suggest?
Thanks.
I think you might have more luck with j < per_page.length()
Just want to know that is this the correct way I am following?Thanks.
Lord almighty!!! He just told you what you did wrong!> I think you might have more luck with j <> per_page.length()It should say j < per_page.length() and not j > per_page.length()
I am sorry I couldn't follow that. Thanks.
> Lord almighty!!! He just told you what you did
> wrong!
>
> > I think you might have more luck with j <
> > per_page.length()
>
> It should say j < per_page.length() and not j >
> per_page.length()
No my apologies... I shouldn't have blown up like that.
I appreciate your greatness. Thanks all your help guys. Atlast my problem is solved. I am really thankfull to all of yours valuable inputs. Regards.