Transfer data before being incremented by loop - java

I'm trying to get ID to transfer when the button is clicked both ID and button is within the loop(so that they appear in a single row) but when I click the button it only transfers the last ID in the table...
<%
int ID = 0;
int userID =0;
String deleteRow = "delete_Row.jsp";
String myPage = "profile.jsp";
while (rs.next()) {
ID ++;
%>
<tbody>
<tr class="success">
<td><%=ID%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
<td><button type="button" class="btn btn-primary" onClick="window.location='<%= myPage %>';">Check Details</button></td>
<td><button href="delete_Row.jsp?userID=<%ID%>"type="button" class="btn btn-danger" onClick="window.location='<%=deleteRow%>';">Remove</button></td>
</tr>
</tbody>
<% } %> `

I spot two errors in your program:
The most important: <button> has no href attribute. I suggest you change it by:
Remove
The less important: The <tbody> should be defined outside the loop.

Related

Loop inside loop - Display ArrayList

I'm trying to diaplay ArrayList in JSP. I have to ArrayLists I want to loop through and display the data in the same JSP table. This works fine, for the first Arralist (history), but when I get to the second it displays all the ArrayList in each row, instead of the current index from the loop:
<table id="test" class="table text">
<tr>
<th>Result</th>
<th>Deposit</th>
<th>Bet</th>
<th>Return +/-</th>
<th>Current balance</th>
<th>Date</th>
</tr>
<%
for (history his : history) {
%>
<tr class="listing">
<td><%=his.getRes()%></td>
<td><%=resultTwoDecimalsDeposit%></td>
<td><%=his.getOdds()%></td>
<td><%=his.getDate() %> </td>
<td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>
Everything is fine until here. Instead of showing the current index from the loop, it displays all the ArrayList in each tr
<% for (int i = 0; i < list1.size(); i++) {
%>
<td><%=list1.get(i) %></td>
<% } %>
<%}}}%>
</tr>
</table>
Your problem is because of the nested loop. For each his, the complete inner for loop is executing which you do not want. You want that for each history element, the corresponding value from list1 is displayed.
Do it as follows:
<table id="test" class="table text">
<tr>
<th>Result</th>
<th>Deposit</th>
<th>Bet</th>
<th>Return +/-</th>
<th>Current balance</th>
<th>Date</th>
</tr>
<%for (int i=0; i<history.size() && i<list1.size(); i++) {%>
<tr class="listing">
<td><%=history.get(i).getRes()%></td>
<td><%=resultTwoDecimalsDeposit%></td>
<td><%=history.get(i).getOdds()%></td>
<td><%=history.get(i).getDate() %> </td>
<td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>
<td><%=list1.get(i) %></td>
</tr>
<%}%>
</table>

I want to give 'edit-link i.e <a>edit</a>' if ans column of table from database is null

I am retrieving answer from ans column by using jdbc and showing it into a jsp page in a form of a table, if ans column is null or empty then I have to give a user edit link, but if ans column has a value then I'm going to show answer but the problem is if-else condition is not working. Please help me to solve this, My code is given below:
<div class="table-responsive">
<table class="table center-aligned-table">
<thead>
<tr class="text-primary">
<th>Question No</th>
<th>Question Name</th>
<th>Answer:</th>
<th> </th>
<th> </th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<%
try{
int oopa=1;
//String nme=(String)session.getAttribute("vname7");
DbCon cc=new DbCon();
Connection onn=cc.fun();
Statement stt=onn.createStatement();
ResultSet r=stt.executeQuery("select ques,ans from postquestion;");
while(r.next()){
%>
<tr class="">
<td><center><%=oopa++%></center></td>
<td><%=r.getString(1)%></td>
<%
if(r.getString(2)==null)
{
%>
<td>Edit</td>
<%
}
else
{
%>
<td><%=r.getString(2)%></td>
<%
}
%>
<td> </td>
</tr>
<%
}
}
catch(Exception vjin){
System.out.println("I am vjin: "+vjin);
vjin.printStackTrace();
}
%>
</tbody>
</table>
</div>
You might get empty values from your results. So check if r.getString(2) is empty or not, null differs from empty value.
More over, JSP is just a view component, writing DAO code in JSP is not a good practice.
Thanks
You can check for null values using wasNull() method of ResultSet.For example like below :
<%
//getting value of column in "a"
String a = r.getString(2);
//checking if resultset return null i.e a is null then do below:
if(r.wasNull())
{
%>
<td>Edit</td>
<%
}
else
{
%>
<td><%=r.getString(2)%></td>
<%
}
%>

How to return a single data from a row from a table in html?

I have a table that stores the product in the cart of a user. Each row has a button that enables the user to remove a single product from the cart of a user. Here is the code snippet of my html.
<form action="${pageContext.request.contextPath}/customer/removeProduct" method="post">
<input type="hidden" name="page" value="${page}">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<c:forEach items="${productsInCart}" var="product"
varStatus="status">
<input type="hidden" class="form-control" name="upc"
value="${product.getUpc()}">
<tr class="warning">
<td>${product.getName()}</td>
<td>${product.getQuantity()}</td>
<td style="color: green;">₱ ${product.getTotal()}</td>
<td><input type="submit" class="btn btn-warning btn-xs"
value="Remove from cart"></td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
Here is the output.
Product in cart table
The value I want to get is the UPC of a product which is stored in a hidden input type field. However, when the 'remove from cart' button is clicked, it returns all the UPC of the product that is in the cart. Here is the code snippet of the controller.
#RequestMapping(value="/customer/removeProduct", method=RequestMethod.POST)
public String removeProduct(#RequestParam(value = "upc") String upc, HttpServletRequest request){
System.out.println(upc);
// customerService.removeProductInCart(customer.getCustomer(request).getCartId(), upc);
return "customer_home";
}
You may want to create form on every iteration to get the specific upc on each row instead of wrapping your whole table.

Java Get ALL elements from a multiselect Box

I have a multi-select Box and I'm doing some javascript to sort the order of the elements in the box. I want to submit the whole array after sorting back to the Java and not just the select items. How can I achieve this?
JSP:
<script type="text/javascript">
$(document).ready(function(){
$("#mup")[0].attachEvent("onclick", moveUpItem);
$("#mdown")[0].attachEvent("onclick", moveDownItem);
});
function moveUpItem(){
$('#list option:selected').each(function(){
$(this).insertBefore($(this).prev());
});
}
function moveDownItem(){
$('#list option:selected').each(function(){
$(this).insertAfter($(this).next());
});
}
</script>
<table width="100%">
<tr>
<td width="50%" align="center">
<h1>DET Column Maintenance</h1>
</td>
</tr>
</table>
<form action="process.det_column_order" method="post" name="detColumnSortorder" >
<table class="data_table">
<tr align="center">
<td>
<select id="list" name="fieldNames" size="35" multiple="multiple" style="width: 250px;">
<c:forEach var="field" items="${detFields}">
<option value="${field.fieldName}">${field.displayName}</option>
</c:forEach>
</select>
</td>
<tr>
<td style="text-align: center;">
<button id="mup">Move Up</button>
<button id="mdown">Move Down</button>
</td>
</tr>
<tr>
<td style="text-align: center;">
<input name="action" type="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
FORM:
private String[] fieldNames;
public String[] getFieldNames() { return this.fieldNames; }
public void setFieldNames(String[] val) { this.fieldNames = val; }
Since forms only submit the selected values, you'll need a little more JS and another form field.
Introduce a hidden form field that will hold the values you care about:
<input type="hidden" name="fieldNamesOrder" id="fieldNamesOrder"/>
And after every click of Move Up/Move Down:
var order = [], sel = document.getElementById("list");
for(var i = 0, len = sel.options.length; i < len; i++) {
order.push(sel.options(i).value);
}
document.getElementById("fieldNamesOrder").value = order.join(",");
Then, on the server side, you can read your ordered field names out of that posted field.
You either need to create a hidden text field in your HTML form that stores the values from each of your options... or you need to programatically select all of the options before the form is submitted.

Working with <div> tag in JSP for making the contents hidden or visible

I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>

Categories

Resources