How to select option in HTML select using TAL in ZPT? - html-select

I got the following drop-down list - SELECT element - construction in my ZPT template:
<select id="record_selector">
<option tal:repeat="record view/records" tal:attributes="value record/id">
<span tal:replace="record/name"></span>
</option>
</select>
How make it possible to have selected OPTION which value is equal to one from the corresponding view property (i.e. for example, OPTION tag value == view/currentRecordId then make it selected).

Using the sdupton's clue, I got the following solution:
<select id="record_selector">
<tal:block tal:repeat="record view/records">
<option tal:condition="python: record['id'] != view.recordId"
tal:attributes="value record/id"
tal:content="record/name">
</option>
<option tal:condition="python: record['id'] == view.recordId"
tal:attributes="value record/id"
tal:content="record/name"
selected>
</option>
</tal:block>
</select>
TAL conditionals are awesome :)

I found another solution here: https://old.zope.org/Members/peterbe/DTML2ZPT/index.html#example14
This still works with Zope 5.3 on Python 3.
<select id="record_selector">
<option
tal:repeat="record view/records"
tal:attributes="
value record/id;
selected python: record['id'] == view.currentRecordId">
<span tal:replace="record/name"></span>
</option>
</select>

Related

How keep value from input select after request?

I'm doing a search form in my site. And what i want is to keep on my inputs, the value selected by the user. It's ok for my checkbox, but i can't do it for select.
I show you my form first :
<select name="garde" id="garde">
<option value="">Choisir un type de garde</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
<div class="flex items-center">
<input id="chats" name="chats" value="1" id="chats" type="checkbox"
#if(request()->chats) {{ 'checked' }} #endif>
<label for="chats" class="ml-3 text-sm text-gray-600">Chat</label>
</div>
I tried to use some :
<select blablabla>
<option value="{{$garde->id}}" #if(request()->garde) {{'selected'}} #endif/>{{$garde->garde}}</option>
</select>
So it's certainly good for the value, but it doesn't display the good option name. Always sending me the last value from the database (which is 3 in this case).
My controller
$garde = request()->input("garde");
$chats = request()->input("chats");
$g = Garde::where('id', 'like', "%$garde%")->pluck('id');
Annonce::when($g, function ($s) use ($g) {
return $s->where('garde_id', $g);})
->when($chats, function ($s) use ($chats) {
return $s->where('chats', $chats);})
Edit : Here the "%$garde%" was after trying another option, using "$garde" doesn't changed anything.
As i said, it's ok for checkbox, but for select i can not keep this value without doing a "bidouille" as we say in french ^^
<select name="garde" id="garde">
<option value="{{ request()->garde ?? '' }}">
#if(request()->garde == 1) Chez le Pet-Sitter
#elseif(request()->garde == 2) Visite à domicile
#elseif(request()->garde == 3) Chez le Pet-Sitter/En visite
#endif
</option>
#foreach($gardes as $garde)
<option value="{{$garde->id }}">{{$garde->garde }}</option>
#endforeach
</select>
But this is not a good practice i know it, and it's display 2 times the selected value ofc..
Have you got some ideas?
Maybe not using the pluck('id) in my controller is the key? But what is other way to do it?

how to set selected value in drop down menu depending on conditions

I have code with drop down menu. The object for this menu has few fields. Two of them are playerPosition and isFirstSquadPlayer.
If isFirstSquadPlayer is TRUE I need to display in menu player.playerPosition.
Trying on few ways but failed.
My controller:
public String players(#PathVariable long clubId, Model model) {
Club club = this.clubRepository.findByClubId(clubId);
model.addAttribute("players", this.playerRepository.findAllByPlayerClub(club));
return "players";
}
My HTML:
<select name="playerposition"
id="createnewplayerposition" th:value="${player.playerPosition}" required>
<option value="0">Select position for player</option>
<!--HERE NEED PROPER REQUEST -->
<option th:selected="${player.playerPosition}" th:text="${player.playerPosition}"></option>
<option value="GK">Goalkeeper</option>
<option value="RWB">Right Wingback</option>
<option value="RCB">Right Centreback</option>
(...)
</select>
as per kindly advice:
ok, tried and found such request with thymeleaf conditional:
<option th:if="${player.playerPosition!=null}" th:selected="${player.playerPosition}" th:text="${player.playerPosition}"></option>
it's not excactly how I expected but gives some solution –

how to thymeleaf select row value parameter jquery function?

How can I send the selected line value?
<select th:onchange="changes(this.getAttribute('data-test'))">
<option th:each="list : ${allList}"
th:value="${list?.number}"
th:text="${list?.name}"
th:data-test="${list?.test}">
</option>
</select>
onchange(list.test) value parameter ....
This should work (in your case, this refers to the select object, and not an option).
<select th:onchange="changes(this.options[this.selectedIndex].dataset.test)">
<option th:each="list : ${allList}"
th:value="${list?.number}"
th:text="${list?.name}"
th:data-test="${list?.test}">
</option>
</select>

How can I Use th:selected attribute in thymeleaf

<select name="fGroup-selector">
<option th:each="fGroup : ${fGroupsList}"
th:value="${fGroup.id}"
th:selected="${fGroup.is_default}"
th:text="${fGroup.name}">fGroup
</option>
</select>
It is not working....
This is thymeleaf dropdown list code in which th:selected attribute is not working Please any help me

Set default value of dropdown through grails paramater

I send a user to a page with setting the parameters using JS like this:
window.location='/myPgae/MyController?terminId='+terminId;
On the view page I want to set the dafult value of one drop-down menu from the parameter if it was sent.
Drop down:
<select name="terminId" dojoType="dijit.form.Select" style="width:180px;">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3 Tage</option>
</select>
If parameter was sent I want to set option3 as the selected value. I know there is an option selected="selected" but how can I do the check?
I've got it:
<g:if test="${params.terminId}">
<option value="option3" selected="option3">option3</option>
</g:if>
<g:else>
<option value="option3">option3</option>
</g:else>

Resources