Whether it is possible hide 'checkbox' with use th:unless? - thymeleaf

I have a color filter, simillar to this:
<input type="checkbox" id="orange" class="orange" value="orange"/>orange
<br/>
<input type="checkbox" id="peach" value="peach"/>peach
<br/>
<input type="checkbox" id="terracotta" value="terracotta"/>terracotta
<br/>
<input type="checkbox" id="coffee" value="coffee"/>coffee
<br/>
<input type="checkbox" id="browne" value="browne"/>browne
<br/>
<input type="checkbox" id="rose" value="rose"/>rose
<br/>
<input type="checkbox" id="red" value="red"/>red
<br/>
I recive color from DB.
<div th:each="model : ${allColor}">
<span th:text="${model.color}"/>
Can I hide color, with use Thymeleaf, which is not in DB?
For example now I haven't Rose, Coffee and Peach colors, but maybe in future I will have this color. I want to do colors verification , if color is in DB, user can see on UI checkbox, else checkbox hide. I read about th:if and th:unless. Whether it is possible to make it with use Thymeleaf?
If I try to do:
<input type="checkbox" th:if="${model.color==coffee}"id="coffee" value="coffee"/>coffee
It's not working.

Instead of hiding colors, you should create the color checkboxes with a loop as well. Like this:
<th:block th:each="model : ${allColor}" th:with="color=${model.color}">
<input type="checkbox" th:id="${color}" th:class="${color}" th:value="${color}"/> <span th:text="${color}" />
<br />
</th:block>
As for why your original attempt didn't work, it's hard to tell without seeing more of the html. I'm guessing that ${model.color} was undefined, because you weren't in a loop? Also, you were missing quotes around 'coffee'. like this: `${model.color == 'coffee'
Something like this may work as well, but I would recommend the loop.
<input th:if="${allColor.contains('orange')}" type="checkbox" id="orange" class="orange" value="orange"/>orange<br/>
<input th:if="${allColor.contains('peach')}" type="checkbox" id="peach" value="peach"/>peach<br/>
<input th:if="${allColor.contains('terracotta')}" type="checkbox" id="terracotta" value="terracotta"/>terracotta<br/>
<input th:if="${allColor.contains('coffee')}" type="checkbox" id="coffee" value="coffee"/>coffee<br/>
<input th:if="${allColor.contains('browne')}" type="checkbox" id="browne" value="browne"/>browne<br/>
<input th:if="${allColor.contains('rose')}" type="checkbox" id="rose" value="rose"/>rose<br/>
<input th:if="${allColor.contains('red')}" type="checkbox" id="red" value="red"/>red<br/>}`.

Related

Search box and asp-route

I have just added search box to an index view
<form asp-action="Index" asp-route-account="#Model.First().AccountId" method="get">
<div class="form-actions ">
<p>
<input id="searchStringId" type="text" name="searchString" value="#ViewData["currentFilter"]" />
<input type="submit" value="Search" class="btn btn-primary">
<a asp-action="Index" asp-route-account="#Model.First().AccountId">Back to Full List</a>
</p>
</div>
</form>
This gives me:
https://localhost:44377/MainUsers/Funds?searchString=aldi
But I need
https://localhost:44377/MainUsers/Funds?account=1&&searchString=aldi In other words I need the search string to also include the route id.
Any ideas?
I need the search string to also include the route id. Any ideas?
You can try to achieve the requirement by using a hidden input field as below.
<input name="account" type="hidden" value="#Model.First().AccountId" />
<input id="searchStringId" type="text" name="searchString" value="#ViewData["currentFilter"]" />
Test Result

How to recive selected value from Spring to Thymeleaf

I send from Spring to Thymeleaf ArrayList, which consists from two BigDemical parameters. This for price filter and I want to do: from 0 to first element from ArrayList, from first element to second and more second
Spring:
modelAndView.addObject("price",filterPrice);
Html
<div th:each="filterPrice : ${price}">
<input class="priceSelected" type="checkbox" id="price01" value="1"/> 0 -
<span class="filterPriceFirst" th:text="${price[0]}"></span> <br/>
<input class="priceSelected" type="checkbox" id="price02" value="2"/>
<span class="filterPriceFirst" th:text="${price[0]}">
-<span class="filterPriceFirst" th:text="${price[1]}"></span> <br/>
<input class="priceSelected" type="checkbox" id="price02" value="3"/>
> <span class="filterPriceFirst" th:text="${price[1]}">
<br/>
</div>
Code working, but I recive all value twice. But I need to recive all value only one time. Where my misstake?
In HTML You are using th:each that loops through the array. Since your array has two elements it does it twice. Removing th:each will fix it. Also element ID should be unique so you should use price03 on the last input.
Your code should look something like this
<div>
<input class="priceSelected" type="checkbox" id="price01" value="1"/> 0 -
<span class="filterPriceFirst" th:text="${price[0]}"></span>
<br/>
<input class="priceSelected" type="checkbox" id="price02" value="2"/>
<span class="filterPriceFirst" th:text="${price[0]}">-
<span class="filterPriceFirst" th:text="${price[1]}"></span>
<br/>
<input class="priceSelected" type="checkbox" id="price03" value="3"/> >
<span class="filterPriceFirst" th:text="${price[1]}">
<br/>
</div>

cant save radio button selections to database with thymeleaf

I have form and inside I have radio button option , I have the controller class and the model. I can successfully save the other data but radio buttons options reflects to database as 0 always. I believe there is something wrong with thymeleaf radio button implementation.
<form id="add" role="form" th:action="#{/add}" method="post" th:object="${radiobutton}">
<input type="hidden" th:field="*{id}"/>
<div class="form-group">
<label>is it new</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline1"
value="true" th:checked="*{isNew}"/>Yes
</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline2"
value="false" th:checked="*{isNew} == false"/>No
</label>
</div>
</form>
I found it , values should be 0 or 1 instead of true and false
<form id="add" role="form" th:action="#{/add}" method="post" th:object="${radiobutton}">
<input type="hidden" th:field="*{id}"/>
<div class="form-group">
<label>is it new</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline1" value="1"
th:checked="*{isNew}"/>Yes
</label>
<label class="radio-inline">
<input type="radio" name="optionsRadiosInline1" id="optionsRadiosInline2" value="0"
th:checked="*{isNew} == false"/>No
</label>
</div>
</form>

How to add text field in front of label in jquery mobile

Can you please tell me how to add text field in front of label in jquery mobile .
There is two different types of text field in this image.How to implement this?
Just you have to copy simple code to your website...
For input box:-
<div data-role="fieldcontain">
<label for="text-12">Text input:</label>
<input name="text-12" id="text-12" value="" type="text">
</div>
For textarea:-
<div data-role="fieldcontain">
<label for="textarea-12">Textarea:</label>
<textarea cols="40" rows="8" name="textarea-12" id="textarea-12"></textarea>
</div>
Keep that in mind the value of for="" and id="" of input box should be same...
For more details check out:- http://view.jquerymobile.com/1.3.1/demos/widgets/textinputs/
Use this code to add textfield
<label for="basic">Text Input:</label>
<input type="text" name="name" id="basic" value="" />
Ref this docs to know more informations about JQM
http://jquerymobile.com/demos/1.2.1/docs/forms/textinputs/

How parse checkbox values in server side(rails)?

I am beginner in ruby + rails.
I have problem to get values in controler of checkbox with params[] of rails.
The Case:
I have simple form that that have checkbox and i want to sent reqular request to controller action with user checked values.
The problem that params[:rating] can have few values.
My case:
html user side:
<form id="ratings-form">
<input name="rating" type="checkbox" value="G">G
<input name="rating" type="checkbox" value="PG">PG
<input name="rating" type="checkbox" value="PG-13">PG-13
<input name="rating" type="checkbox" value="R">R
<input type="submit" value="refresh">
</form>
controller action code to parse checked values: (get error 1. params[:rating] == nil or params[:rating] == string)
params[:rating].each do |rat|
p rat;
end
What should i change in the code to make it work?
Thanks
Try this HTML
<input name="rating[]" type="checkbox" value="G">G
<input name="rating[]" type="checkbox" value="PG">PG
<input name="rating[]" type="checkbox" value="PG-13">PG-13
<input name="rating[]" type="checkbox" value="R">R
Then you should have an array in params[:rating].

Resources