How can i display only selected options in Thymeleaf - java

I am trying to modify the option in Thymeleaf so that the table can show me only selected item, after clicking the option. Here is my code:
<th>
<select th:onchange="???" id="stateSelect" class="form-control" th:field="*{receiverType}">
<option th:value='NULL'>All aggregators</option>
<option th:each="receiverType : ${receiverTypes}"
th:value="${receiverType}"
th:text="${receiverType}">
</option>
</select>
</th>
I know that i should use onchange , but i am confused.

Related

How to access thymeleaf model attribute (A list from an object specifically) to populate an html dropdown list?

I've been doing some research and I an understand how to use specific objects if you passed the entire things in with the model.addAttribute("object name", object); but I don't know how to grab the data inside of the html if I only passed a method that returns a list with it.
So, this would be my controller for the class:
#Controller
public class randomNotification {
#Autowired
private randomService randomService;
#Autowired
private secondService secondService;
#GetMapping("/random-notification.html")
public String renderpage(Model model) {
model.addAttribute("orgs", randomService.getOrgs());
model.addAttribute("templates", secondService.getTemplates());
return "random-notification";
}
}
The randomService.getOrgs() would return a Collection of Strings like ["AB","BC","CD","DE","EF","FG"]
The secondService.getTemplates() would also return a similar collection of strings.
What I want to do is use each of the values in the list of strings to populate a drop down list inside my html. So if I had the following blank combobox:
<div class="random-combobox">
<th><label>Random </label></th>
<th>
<select class="random-cbox">
<option value="">Select one..</option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
</th>
</div>
I'd want to populate each option with one item from the list so it would almost be like
<div class="random-combobox">
<th><label>Random </label></th>
<th>
<select class="random-cbox">
<option value="">Select one..</option>
<option value="<getOrgsOption1>">AB</option>
<option value="<getOrgsOption2>">BC</option>
<option value="<getOrgsOption3>">CD</option>
<option value="<getOrgsOption4>">DE</option>
<option value="<getOrgsOption5>">EF</option>
<option value="<getOrgsOption6>">FG</option>
</select>
</th>
</div>
Something like this will do it
<select>
<option th:each="org : ${orgs}"
th:value="${org}"
th:text="${org}"></option>
</select>

thymeleaf, bind model properties to multiple form elements

I have a model property (region) than can be bound to more than one form elements as shown below:
<select th:field="*{region}" class="custom-select" id="country" style="padding: 0px">
<option value="a" selected>Select one..</i></option>
<option th:each="country : ${counties}" th:value="${country.id}" th:text="${country.name}"
selected></option>
</select>
<select th:field="*{region}" class="custom-select" id="state" style="padding: 0px">
<option value="a" selected>Select one..</i></option>
<option th:each="state : ${states}" th:value="${state.id}" th:text="${state.name}"
selected></option>
</select>
as shown, region property of the model can be bound to either country or state and i have a radio group for user to select one of the two. My question is on how determine the one to pass to the controller to be saved. Is there a way to dynamically set the th:field for the model, or us there any other working solution?

html load conditional value to select tag

I try to load conditional value to a select tag based on the user's choice on a pervious form tag. it might require java or php. i am not competent in any of those two language.
the logic is explain as the following.
<form>
<input name="complaint" value="copyright" type="radio">copyright <br>
<input name="complaint" value="trademark" type="radio">trademark <br>
<input name="complaint" value="other_concern" type="radio"> other concerns
</form>
if complaint="copyright" then
<select>
<option value="image">image</option>
<option value="product">product</option>
</select>
elseif complaint="trademark" then
<select>
<option value="item">item</option>
<option value="image">image</option>
</select>
elseif complaint="other_concern"
<select>
<option value="explain">explain</option>
</select>
end if
any solution?
Heres a solution in Jquery. Add a class name to the inputs and hide the selct options by default
<input name="complaint" value="copyright" type="radio" class="complaint">
then add unique ID tags to the select options along with a css class i like to use, .addClass and .removeClass are slighty faster and more efficent than .hide() and .show() selectors
Add the css at the bottom of your css sheet
.hidden{position:absolute;width:0;height:0;overflow:hidden;visibility:0;padding:0;margin:0}
<select id="copyright" class="selectOptions hidden">
<option value="image">image</option>
<option value="product">product</option>
</select>
Finally add some JQuery You need to copy the remainder for your other hidden values
$('.complaint').click(function() {
$('.selectOptions').addClass('hidden');
var checkComplaint = $(this).val();
$('#' + checkComplaint).removeClass('hidden');
});

add form field based on option selection

I have a form where i have to add form field based on options selected in the select box
what i have done is created all form fields and based on selection show hide fields , But is there any simplified way to do this
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="age1">
<select name="age1" id="age1">
//options
</select>
</div>
<div id="age2">
<select>
//options
</select>
</div>
<div id="age3">
<select>
//options
</select>
</div>
here is my jquery
$('#child').change(function() {
if( $(this).val() == 0 ) {
$("div#age1").hide();
$("div#age2").hide();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 1 ) {
$("div#age1").show();
$("div#age2").hide();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 2 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").hide();
$("div#age4").hide();
}
if( $(this).val() == 3 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").show();
$("div#age4").hide();
}
if( $(this).val() == 4 ) {
$("div#age1").show();
$("div#age2").show();
$("div#age3").show();
$("div#age4").show();
}
});
How to do it in simplified way and how to get post value for this
same basic approach as Prashant - but theres no need to use eq().. the divs all have ids that match the values of the select list - and note that the first select list in your current code have the same id as its parent div. This will cause an issue, but the following will fix it.
All this does is on the change of the #child select list - triggers all divs to hide (note the display none in the CSS to hide them initially) and then show the one that matches the age+value of child. I would not actually do it this way myself, but given the code you had this works. I also added options to the age select lists to give them something to show when you change the #child list.
As I said, I would not suggest doing it this way - I would suggest appending a new select list to the end of the form based on the selection -that way you dont have all select lists just sitting there and you don't have to worry about submitting the selected value from one of the hidden lists when you submit the form. Just a thought.
$('#child').change(function() {
$("div").hide();
$("#age"+$(this).val()).show();
});
div{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="age1">
<select name="age1">
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>
<div id="age2">
<select name="age2">
<option value="c">c</option>
<option value="d">d</option>
</select></div>
<div id="age3">
<select name="age2">
<option value="e">e</option>
<option value="f">f</option>
</select></div>
<div id="age4"> <select>
<option value="g">g</option>
<option value="h">h</option>
</select></div>
Try This:
<select name="child" id="child">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div class="myage" id="age1">
<select name="age1" id="age_1">
//options
</select>
</div>
<div class="myage" id="age2">
<select>
//options
</select></div>
<div class="myage" id="age3"> <select>
//options
</select></div>
Your JS :
$(".myage:eq(0)").show();
$('#child').change(function() {
$(".myage").hide();
$(".myage:eq("+$(this).val()+")") .show();
});
Use Class instead of ID :)

retrieving value from a dynamic selection list

I am working on a jsp project where I have a dynamic selection list. The values in this list change according to the value selected in the 1st selection list.
Here's the code:
<script language="JavaScript" type="text/javascript">
function optionsChange(){
var service = document.getElementById("service").value;
if(service == 'GSM'){
document.getElementById("cdmaService").value= '';
document.getElementById("cdmaService").style.display = 'none';
document.getElementById("gsmService").style.display = 'block';
$('gsmService').attr('name', 'services');
}else if(service == 'CDMA'){
document.getElementById("gsmService").value= '';
document.getElementById("gsmService").style.display = 'none';
document.getElementById("cdmaService").style.display = 'block';
$('cdmaService').attr('name', 'services');
}
}
</script>
<select id="service" onChange="javascript:optionsChange();">
<option value="GSM">GSM</option>
<option value="CDMA">CDMA</option>
</select>
<td id="gsmService" ><select name="services" >
<option value="COMBO OFFER">COMBO OFFER</option>
<option value="CRICKET">CRICKET</option>
<option value="ASTRO">ASTRO</option>
</select>
</td>
<td id="cdmaService" style="display:none"><select name="services" >
<option value="COMBO OFFER CDMA">COMBO OFFER CDMA</option>
<option value="WIN THE DREAM">WIN THE DREAM</option>
<option value="VOICE CHAT">VOICE CHAT</option>
</select>
</td>
now when the user selects a service, lets say "GSM", and then selects a service from the second list, lets say "ASTRO". He clicks on a button which redirects him to the next page where he sees "ASTRO" printed. This works fine.
But if the user selects "CDMA" from the 1st list and then selects, let's say "VOICE CHAT" from the second list. It still prints "ASTRO" on the next page. IT should print "VOICE CHAT".
this is the method to submit form:
<script language=javascript>
function submitForm(actionStr)
{
if(actionStr=="User Details")
{
document.login.action="showUsrDetail.jsp";
document.login.submit();
}
}
this is the code for the button:
<input type="button" value="User Details" onclick="submitForm(this.value);"/>
then it redirects to the page ""showUsrDetail.jsp". And when it does the name of the service is printed on the console. For which the code is:
<%
String service = request.getParameter("services");
System.out.println("Value Added Service selected is ="+service);
%>
if i change the first selection to CDMA and then select any service from the second selection list, it still prints the Service which is under GSM.
Can somebody please help me out?
Since you are doing nothing in submitForm() function other that submitting the form that can be achieved directly by <input type="submit>" as shown in below sample.
<form action="showUsrDetail.jsp" id="login" method="post">
<!-- other fields -->
<input type="submit" value="User Details"/>
</form>
Solution 1
Use different names for all the select fields and check the values in showUsrDetail.jsp.
<form id="login" action="showUsrDetail.jsp" method="post">
<table>
<tr>
<td><select id="service" onChange="javascript:optionsChange();"
name="service">
<option value="GSM">GSM</option>
<option value="CDMA">CDMA</option>
</select></td>
<td id="gsmService"><select name="gsmService">
<option value="COMBO OFFER">COMBO OFFER</option>
<option value="CRICKET">CRICKET</option>
<option value="ASTRO">ASTRO</option>
</select></td>
<td id="cdmaService" style="display: none"><select
name="cdmaService">
<option value="COMBO OFFER CDMA">COMBO OFFER CDMA</option>
<option value="WIN THE DREAM">WIN THE DREAM</option>
<option value="VOICE CHAT">VOICE CHAT</option>
</select></td>
<td><input type="submit" value="User Details" /></td>
</tr>
</table>
</form>
showUsrDetail.jsp:
<c:if test="${param.service == 'GSM'}">
<c:out value="${param.gsmService}" />
</c:if>
<c:if test="${param.service == 'CDMA'}">
<c:out value="${param.cdmaService}" />
</c:if>
Solution 2
Add a hidden input field and update its value based on selection change in cdmaService and gsmService select item. Add change listener on both the select item.
<input type="hidden" name="serviceValue"/>
showUsrDetail.jsp:
<c:out value="${param.serviceValue}" />
Note:
I suggest you to use JavaServer Pages Standard Tag Library or Expression Language instead of Scriplet that is more easy to use and less error prone.

Categories