Struts 2.0 pass selected checkbox mapping value to JavaScript function - struts2

I am using struts 2 framework and iterating 4 fields (checkbox,rollnumber,name,location) in Jsp. It is working fine. Now i need to delete the selected checkbox record, for that i require rollnumber(which is primary key in table) object to be pass to javascript function. How can I pass the rollnumber object to javascript function. I have already pass the checkbox object(document.myForm.subCheckBox) to java script function, i need to pass one more rollnumber object.
<table border="1">
<tr>
<s:if test="%{mode != 'view'}">
<td><input type="checkbox" id="mainCheckBox" onclick="return checkAll(document.myForm.subCheckBox)"/></td>
</s:if>
<th>Roll Number</th>
<td>Name</td>
<td>Location</td>
</tr>
<s:iterator value="beanList" >
<tr>
<s:if test="%{mode != 'view'}">
<td>
<input type="checkbox" name="subCheckBox"/>
</td>
</s:if>
<td>
<s:property value="rollnumber" />
</td>
<td>
<s:property value="name"/>
</td>
<td>
<s:property value="location"/>
</td>
</tr>
</s:iterator>
</table>
<table>
<s:if test="%{mode != 'view'}">
<tr>
<input type="button" value="Delete" onclick="return deleteRecord(document.myForm.subCheckBox) "/>
</tr>
</s:if>
</table>

Related

Display Nested object

I use spring boot with thymeleaf, in a page try to display a form and it's nested object.
My object Factories has
id
name
List
When I display
<form id="factoriesForm" th:object="${factories}" >
...
...
<table id="machinesTable" class="table table-striped table-hover responsive">
<thead>
<tr>
<th th:text="#{name}">Name</th>
<th th:text="#{description}">Description</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="machine, stat : ${machines}">
<td>
<input type="hidden" th:id="${'machineId-'+stat.index}" th:field="*{machines[__${stat.index}__].id}" />
<input type="text" class="form-control" th:id="${'machineName-'+stat.index}" th:placeholder="#{name.placeholder}" placeholder="Name" th:field="*{machines[__${stat.index}__].name}" />
</td>
<td> <input type="text" class="form-control" th:id="${'machineDescription-'+stat.index}" th:placeholder="#{description.placeholder}" placeholder="Description" th:field="*{machines[__${stat.index}__].description}" /></td>
<td> <i class="fas fa-trash-alt"></i></td>
</tr>
</tbody>
</table>
</form>
My factories has many machines, but none is displayed
Any idea?
change
${factories}
for
*{factories}

Create dynamic table with webflow and grails

I'm trying to create a webflow with a dynamic table on one page. Which looks like this
def startFlow = {
contact {
on('next') {
flow.developer = params.developer
flow.project = params.project
flow.division = params.division
flow.projectResponsible = params.projectResponsible
flow.email = params.email
[flow : flow]
}.to('ipcount')
on('cancel').to('finish')
}
ipcount{
on('next'){
flow.ipcount = params.int('ipcount')
[flow: flow]
}.to('systems')
on('cancel').to('finish')
}
systems{
on('next') {
flow.hoster= params.hoster
flow.ip = params.ip
flow.os = params.os
flow.dns = params.dns
flow.systemDate = params.systemDate
[flow : flow]
}.to('url')
on('cancel').to('finish')
} ....
The problem is I that the number of systems could be different every time (1...n).
One idea was to ask the page before how many entries should be created (ipcount).
My view looks like this
<g:set var="count" value="${flow.ipcount}" />
<g:each in="${(1..'${count}')}">
<tr class="prop">
<td valign="top" class="name">
<label for="ip">IP Adresse:</label>
</td>
<td valign="top">
<td valign="top" class="value ${hasErrors(bean:hosterInstance,field:'ip','errors')}">
<input type="text" id="ip" name="ip" value="${params.ip}" />
</td>
<td valign="top" class="name">
<label for="dns">DNS:</label>
</td>
<td valign="top">
<input type="text" id="dns" name="dns" value="${params.dns}" />
</td>
<td valign="top" class="name">
<label for="os">Operating System:</label>
</td>
<td valign="top">
<input type="text" id="dns" name="dns" value="${params.os}" />
</td>
</tr>
</g:each>
Beside that this is not working as I get an Internal server error: (java.lang.String cannot be cast to java.lang.Integer) it would be nicer if I could at the table row dynamically on the page.
Here is the question: Is this possible with webflow and how? Especially I don't know how to handle the flow parameter and how to save the collected entries at the end of the webflow to the database.
If you need to work with a list of objects, command objects is the way to go. It supports databinding from the incoming request and will handle lists.
You can check a related question that show's you how to do it.
And in your view, you will need to handle the index in the name of your input. Example:
Consider
class System {
String ip
String dns
...
}
And commandInstance.systems a List<System>.
<g:each in="${commandInstance.systems}" var="command" status="i">
<input type="text" id="ip$i" name="systems[$i].ip" value="${command.ip}" />
</g:each>

How to retrieve struts2 checkboxlist values

I will create a list with checkboxlist. For those I use following code:
<s:form action="accept" namespace="/manager/course">
<s:checkboxlist list="courseRequests" name="acceptList" listValue="studentNickname" listKey="studentId" theme="checkbox-fix"/>
<s:url action="accept" namespace="/manager/course" var="accList" />
<s:a href="%{accList}"><s:text name="Accept"/> </s:a>
</s:form>
It work fine a create a check box list, that you can see its pic in the following:
and this is html code generated by above code:
<form id="accept" name="accept" action="/ESA/manager/course/accept.action" method="post">
<table class="wwFormTable">
<table class="gradienttable">
<tr>
<th class="row"><p>Row</p></th>
<th style="width: 240px;"><p>Student</p></th>
<th ><p>Accept</p></th>
</tr>
<tr>
<td id="row"><p><label>1</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-1" class="checkboxLabel">Mansour Barzegar</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="5" id="acceptList-1" </p></td>
</tr>
<tr>
<td id="row"><p><label>2</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-2" class="checkboxLabel">Ali Mahmoudi</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="6" id="acceptList-2" </p></td>
</tr>
<tr>
<td id="row"><p><label>3</label></p></td>
<td style="width:250px;"><p>
<label for="acceptList-3" class="checkboxLabel">Masih Zare</label>
</p></td>
<td style="text-align:center;"><p>
<input type="checkbox" name="acceptList" value="7" id="acceptList-3" </p></td>
</tr>
</table>
Accept
</table>
</form>
In the Action Class I tried to retrive seleced checkbox value by following code:
private int[] acceptList;
public void setAcceptList(int[] acceptList){
this.acceptList=acceptList;
}
and several other code but I all states I get null.
Do I use wrong code?
in your markup, do this:
<input type="checkbox" name="thename" id="checkbox_id1" value="value1" />
<input type="checkbox" name="thename" id="checkbox_id2" value="value2" />
in your action (or object) do this:
// action/object code...
Set<String> cbox = new HashSet();
public void setThename(String[] thenames) {
for (String thename : thenames) {
cbox.add(thename);
}
}
// action/object code...
notice the checkbox name matches the setter name, e.g. element name == someName and method == setSomeName
Same would apply for Set<Integer>, but you use int[] thenames as the argument. You could also use Integer[] thenames for the argument.
to test output:
if (cbox != null) {
for (String s : cbox) {
log.info(s);
}
}
http://struts.apache.org/release/2.2.x/docs/using-checkboxes.html

Struts2 tag If using IteratorStatus

I am starting with Struts2 and I need help to solve an issue on my IF tag.
This my code
public class InventoryRow {
private String title;
private int[] qty = new int[5];
private boolean[] warningFlag = new boolean[5];
}
In Action class I have this property:
private List<InventoryRow> parts = new ArrayList<InventoryRow>();
In my JSP, I would like to apply different style according with qty[] value or warningFlag[] value.
<s:iterator value="parts" var="product">
<tr>
<td><s:property value="title" /></td>
<s:iterator value="qty" var="val" status="idStatus">
<td class="qty <s:if test="#val==-99"> none</s:if>
<s:elseif test="#warningdFlag[%{#idStatus.index}] == true"> warning</s:elseif>
" >
<s:property />
</td>
</s:iterator>
</tr>
</s:iterator>
The first test (equal -99) works. But not the second one (using warningFlag)
Thanks in advance for your help and your advertisement.
Mickael
The # before warningFlag is not needed and you have typo warningdFlag should be warningFlag.
<s:iterator value="parts">
<tr>
<td><s:property value="title" /></td>
<s:iterator value="qty" var="val" status="idStatus">
<td class="qty <s:if test="#val==-99"> none</s:if>
<s:elseif test="warningFlag[#idStatus.index]"> warning</s:elseif>" >
<s:property />
</td>
</s:iterator>
</tr>
</s:iterator>
The %{...} notation on getting status index inside test attribute is not needed because test takes expression as a value.
Also see this link: http://struts.apache.org/development/2.x/docs/ognl.html.

Calculate the sum of a particular table cell's value created by s:iterator tag in JSP from Action class of Struts2?

I want to calculate the sum of the particular property from action class which is displaying in JSP as HTML Table using <s:iterator> tag.
JSP code is:
<table border="1">
<tr>
<th>Date</th>
<th>Material</th>
<th>Quantity</th>
<th>Buyer</th>
<th>Total</th>
<th>Remarks</th>
<th colspan="2">Action</th>
</tr>
<s:set var="sumTotal" value="%{0}" />
<s:iterator value="eal" status="entries">
<tr>
<td><s:property value="date"/></td>
<td><s:property value="materialName"/></td>
<td><s:property value="quantity"/><s:property value="unitName"/></td>
<td><s:property value="buyer"/></td>
<td>₹<s:property value="total"/></td>
<s:set var="sumTotal" value="%{+#attr.total}" />
<td><s:property value="remarks"/></td>
<td>
Edit
</td>
<td>
Delete
</td>
</tr>
</s:iterator>
<tr>
<th colspan="4">Sum Total</th>
<td colspan="4"><s:property value="#sumTotal"/></td>
</tr>
</table>
I want to calculate the sum of table heading "Total" and display that below in last row with the heading "Sum Total".
Please help me..
Change
<s:set var="sumTotal" value="%{+#attr.total}" />
to
<s:set var="sumTotal" value="#sumTotal + total" />
And you do not need to use %{0} inside first <s:set> tag, simple 0 will work.
<s:set var="sumTotal" value="0" />

Resources