Alter multiple columns in the Database based on selected values- Hibernate - spring

I am displaying the details of a particular table in my JSP page using a for-each loop in the following manner:
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
</c:forEach>
<input type="radio" id="agree" value="Agree"/>Agree
<input type="radio" id="disagree" value="Disagree"/> Disagree
<input type="submit" value="Submit"/>
Now, there is no column named "check" in my DB. It is a transient field in my POJO. I do have a Agree/Disagree column in my DB. I want to check particular rows, click "Agree" or "Disagree" and on clicking Submit, the Agree/Disagree column for those records should be updated respectively. I'm using Spring MVC with Hibernate. Please tell me if any more info is required from my side.

You can do this with simple jQuery, Set class(say .chkbox here) to all checkboxes. Whenever any change event happened say,
if checked, the value of checkbox added to JS array.
If unchecked, the value will be removed from JS array.
$(document).ready(function() {
var selectedArray = [];
$('.chkbox').change(function() {
if($(this).is(":checked")) {
selectedArray.push($(this).val());
} else {
selectedArray.pop($(this).val());
}
//alert(selectedArray);
$("#textbox1").val(selectedArray);
});
});
Finally I used textbox to hold all the selected checkboxes value. You can make it into a hidden give a name attribute.
<input type="checkbox" class="chkbox" value="101"> This is 101<br />
<input type="checkbox" class="chkbox" value="102"> This is 102<br />
<input type="checkbox" class="chkbox" value="103"> This is 103<br />
<input type="checkbox" class="chkbox" value="110"> This is 110<br />
<input type="checkbox" class="chkbox" value="111"> This is 111<br />
<input type="checkbox" class="chkbox" value="112"> This is 112
<br />
<input type="hidden" id="textbox1" name="selectedArray" />
Sent it to a controller and split the values by comma (,).
Update :
In your code, you can update like this.
<c:forEach items="${List2}" var="alist">
<tr>
<td>
<input type="checkbox" class="chkbox" name="check" value="${alist.check}">
</td>
<td>${alist.two}</td>
<td>${alist.three}</td>
<td>${alist.four}</td>
</tr>
<input type="hidden" id="textbox1" name="selectedArray" />
</c:forEach>
That's all about required.
See the JS Demo how it works. Hope this helps.

Related

Spring mvc include one single 'form' for different c:url

I am working on a spring-mvc application. In the application, there are various types of events, for which the user can register. As for registration, there is a standard formula with details like firstName, email, address, etc. I am just giving different c:url values depending which event it is.
I was wondering if it is possible to include the form(not the c:url part) in another jsp file, and just call jsp:include. This would serve me very well as when the form needs to be styled, updated, etc, then I need to update only one file. I am including how the form looks. Kindly have a look. Thank
eventregistration.jsp :
<c:url var="addAction" value="/student/add/seminar-e-commerce" ></c:url>
<form:form class="seminar-form" action="${addAction}" commandName="students">
<table>
<form:hidden path="studentid"/>
<label>Firstname/LastName <span class="color-red">*</span></label>
<form:input path="firstname" type="text" class="span6 border-radius-none" />
<span> </span>
<form:input path="lastname" type="text" class="span6 border-radius-none pull-right" />
<label>Email-Address <span class="color-red">*</span></label>
<form:input path="emailadress" type="text" class="span12 border-radius-none" />
<label>Street Name/Number <span class="color-red">*</span></label>
<form:input path="address1" type="text" class="span10 border-radius-none" />
<span> </span>
<form:input path="address2" type="text" class="span2 border-radius-none pull-right" />
<label>City/PLZ <span class="color-red">*</span></label>
<form:input path="city" type="text" class="span10 border-radius-none" />
<span> </span>
<form:input path="plz" type="text" value="" class="span2 border-radius-none pull-right" />
<label>Handy Number <span class="color-red"></span></label>
<form:input path="handynumber" type="text" value="" class="span12 border-radius-none" />
<label path="newsletterpermission">Want to subscribe newsletter?</label>
<form:radiobutton path="newsletterpermission" class="form-control" value="true"/>Yes
<form:radiobutton style="margin-left:15px" path="newsletterpermission" class="form-control" value="false"/>No
<br/>
<br/>
<c:if test="${empty students.firstname}">
<input class="btn-u" type="submit"
value="<spring:message text="Register for event"/>" />
</c:if>
</table>
</form:form>
Any pointers would be nice. Thanks a lot.
You can achieve this with an entrypoint/method in your controller with a Pathvarible of action like below
#RequestMapping(value="/student/{action}", method = RequestMethod.POST)
public String EntryPoint(#PathVaribale String action, ModelMap model) {
switch (action) {
case "add":
// delegate to addMethod
break;
case "activate":
// delegate to activateMethod
break;
case "register":
// delegate to registerMethod
break;
default:
///Unsurported operation
}
return "index";
}
and you can keep the c:url , remember to set up the model.action attribute on a controller method responsible for setting up the form bean, for your case students bean or make it more general
<c:url var="action" value="/student/model.action/" ></c:url>

how to open pop up window table using selenium webdriver?

there is text label on a webpage, and I am trying to click on that to open a pop-up window,but not getting opened. here is the HTML code:
<td width="40%">
<div id="EmpId.outline">
<input type="hidden" name="EmpId" value="" id="popupEmpId">
<input type="text" name="EmpCode" value="" readonly="readonly" class="textMedium250" id="popupEmpCode" autocomplete="off"> <label onclick="checkForPopup('EmpPopupDiv','Select a Emp',640,true,refreshConditionOptions);"> + Search for a Emp</label>
</div>
</td>
I have to click on the label + Search for a Emp, but could not do that.
Try to find element by xpath and click. Based on your example, the xpath to find/click should be something like this:
"//a[#href='#f2']"
<td width="40%">
<div id="EmpId.outline">
<input type="hidden" name="EmpId" value="" id="popupEmpId">
<input type="text" name="EmpCode" value="" readonly="readonly" class="textMedium250" id="popupEmpCode" autocomplete="off"> <label > + Search for a Emp</label>
</div>
</td>

how to get xpath preceding-sibling working for this table

I have a table where I need to find if the checkbox corresponding to a cell is checked or not, see for example, I need to find out if the checkbox id=830 which is a sibling of DeviceHelp is checked or not.
Similarly for element Contact, I will have to check whether checkbox id="72" is checked / unchecked
And I can only rely on the text elements (contact, DeviceHelp, etc) to get the checkboxes state and not the checkbox ids which could change.
<tr class="row active" style="display: table-row;">
<td>
<input id="72" class="rowcheck" type="checkbox" value="true" name="ModesForUi[0].Allow" categoryid="58" disabled="">
<input type="hidden" value="false" name="ModesForUi[0].Allow">
</td>
<td> Administration - Read </td>
<td>
Contact
<input id="ModesForUi_0__ResourceID" type="hidden" value="72" name="ModesForUi[0].ResourceID">
<input id="ModesForUi_0__ModeID" type="hidden" value="12185" name="ModesForUi[0].ModeID">
</td>
<td> Controls access to Search Contacts link and to view contact details page. Navigate to Menu\Advanced\More </td>
</tr>
<tr class="row active" style="display: table-row;">
<td>
<input id="830" class="rowcheck" type="checkbox" value="true" name="ModesForUi[1].Allow" categoryid="58" disabled="">
<input type="hidden" value="false" name="ModesForUi[1].Allow">
</td>
<td> Administration - Read </td>
<td>
DeviceHelp
<input id="ModesForUi_1__ResourceID" type="hidden" value="830" name="ModesForUi[1].ResourceID">
<input id="ModesForUi_1__ModeID" type="hidden" value="12186" name="ModesForUi[1].ModeID">
</td>
<td> Controls access to Help icon displayed next to logout icon and next to the menu. </td>
</tr>
Have tried the following, but I am not getting the checkbox value
"ancestor::td[1]/input[1]/preceding-sibling::td[normalize-space(text())='DeviceHelp']")
And
//tr[position()>1]/td[normalize-space(text())='DeviceHelp*']/parent::td/preceding-sibling::td/input#value
Please xpath experts could you shine some light on what is wrong with the paths above and how to get to the checkbox ?
Thanks!
I think I know what you're trying to get here...
Try this for Device Help:
/root/tr/td[normalize-space(text())='DeviceHelp']/../td[1]/input[1]/#value
And this for Contact:
/root/tr/td[normalize-space(text())='Contact']/../td[1]/input[1]/#value
"root" is just whatever node you're starting from, I added that to test locally, as the XML snippet isn't complete.
EDIT
Try this:
//tr/td[normalize-space(text())='DeviceHelp']/../td[1]/input[1]/#value
//tr/td[normalize-space(text())='Contact']/../td[1]/input[1]/#value

Grails - Select box tied to object ID and remote field

Run into a bit of an odd problem that is increasingly frustrating
Scenario: I have a list of domain objects, each has a g:select attached to it that is rendered by a remote field.
How do I tie the status variable OR the personInstance ID to the selection box, so that when I use the renderField, I update testDiv_(number)
View:
<g:each in="${listOfPeople}" status="i" var="personInstance">
<td>
Text: <g:remoteField action="getResults" controller="person" id="" update="testDiv_${personInstance.id}" paramName="search" name="getResults" value="" />
<g:each in ="${personInstance?.choices}" var="choice" status="x">
<li>${choice}</li>
</g:each>
</td>
<td>
<g:render template="renderThisTemplate"></g:render>
</td>
</g:each>
Template:
<div id="testDiv_${personInstance.id}" class="testDiv_${personInstance.id}">
<g:select id="aChoice" name="aChoice.id" from="${allChoices}" optionKey="id" value="" />
<g:actionSubmit action="addChoice" value="Add"/>
</div>
Edit
I know that the remote call (ajax) is passing the update for testDiv_(number). The problem is with the template ID and assigning that value to the template div.
Just incase anyone needs this answer in the future. Apparently you cannot reference the instance variable (personInstance) in the g:each from the template.
So I replaced the g:render with its code and it worked:
<g:each in="${listOfPeople}" status="i" var="personInstance">
<td>
Text: <g:remoteField action="getResults" controller="person" id="" update="testDiv_${personInstance.id}" paramName="search" name="getResults" value="" />
<g:each in ="${personInstance?.choices}" var="choice" status="x">
<li>${choice}</li>
</g:each>
</td>
<td>
<div id="testDiv_${personInstance.id}" class="testDiv_${personInstance.id}">
<g:select id="aChoice" name="aChoice.id" from="${allChoices}" optionKey="id" value="" />
<g:actionSubmit action="addChoice" value="Add"/>
</div>
</td>
</g:each>

Multiple checkboxes in mvc 3

I have multiple checkboxes with the same name. Like this:
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
<input name="zones" value="#zoneItem.Id" type="checkbox" /><label>#zoneItem.Name</label></span>
but in get or post i can't get which ones are checked. How can i do it?
They should have different values. Right now you gave them all the same value. So once you give them different values:
<input name="zones" value="1" type="checkbox" />
<input name="zones" value="2" type="checkbox" />
<input name="zones" value="3" type="checkbox" />
in your controller action you will get the list of values of those that were checked:
public ActionResult Foo(string[] zones)
{
...
}
They can be retrieved by adding a parameter to your action method.
public ActionResult GetData(Guid[] zones)
{
}
Make sure the array type matches your Id type.

Resources