Spring Boot Get Value of object from select using thymeleaf - spring

So I have this project, and I want to return an object, from the previous choice of 2 select items. How can I do it? for example:
<select class="form-control" id="cityPicker1">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
<select class="form-control" id="cityPicker2">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
I want to display an item from another item list which has a number1 field and a number2 something like ticket.number1 and ticket.number2 but they have to match the values from the previous selects. I can't seem to find anything. Any ideas?
This is how I set it up in the controller:
#GetMapping("/tickets")
public String viewTicketsPage(Model model) {
List<City> listCities = cityRepo.findAll();
List<Ticket> listTickets = ticketRepo.findAll();
model.addAttribute("listCities", listCities);
model.addAttribute("listTickets", listTickets);
return "tickets";
}

I need to clarify a few things about your question.
1 - did you manage to get the checkboxes to work?
2 - can you send the selected data to the controller?
you need a controller to receive data from the page, send this data to the repository to fetch other data..
I believe you want to select a table, where the data matches the selected ones. .
take a look at JPQL, maybe this will help you:
https://www.baeldung.com/spring-data-jpa-query

Related

Laravel Livewire Autofill Input Box Based On Drop Down Selection

I am asking for help. Is there any way I could fill an input box based on drop down selection? On my modal I have a dropdown option for subject description and I would like the subject number input field dynamically change its value. Any help would be much appreciated.
This is how I retrieved the data for my dropdown
subjects=DB::table('programs_subj')->select('corsdes', 'corsno')->get()
This is my dropdown code for the subject description which is working and the selected is saved but I could quite how to incorporate the subject number
<select name="corsdes" id="corsdes" wire:model="corsdes">
<option value="corsdes" wire:model="corsdes"></option>
#foreach($subjects as $sbj)
<option value="{{ $sbj->corsdes }}">{{ $sbj->corsdes}}</option>
#endforeach
</select>
<input name="corsno" id="corsno" wire:model="corsno"><input>
if you have the public properties $corsdes and $corsno then:
public function updatedCorsdes($value)
{
$this->corsno = $this->subjects->where('corsdes',$value)->first()->corsno;
}

Save name instead of id from select component in laravel collective

I want to save the name of a department in the table employees, in the select component it displays the name but saves the id, so I think the problem is in it.
This is the select component:
{{Form::select('nombre_dep', $departamentos, null, array('class'=>'form-control', 'placeholder'=>'Asignar departamento'))}}
In my controller I have this for returning to the view:
$departamentos = departamento::pluck('nombre_dep', 'id');
In the model for employee I have this for relation:
public function departamentos(){
return $this->belongsTo('App\departamentos');
}
I expect to save in that field for example: production instead of 1 that could be id of the department
The selected answer is correct, but offers no explanation.
The issue is that you create your LaravelCollective Form::select so the integer id is sent in the ensuing request. You can verify this by viewing the source of the Form element in your rendered blade html. It currently will look like:
<select class="form-control" name="nombre_dep">
<option selected="selected" value="">Asignar departamento</option>
<option value="1">Production</option>
<option value="2">Department 2</option>
<option value="3">Department 3</option>
</select>
so when the action is triggered with (say) option 1 selected, your generated request simply has "nombre_dep" => "1".
You need to structure your form with the desired option value. like
<select class="form-control" name="nombre_dep">
<option selected="selected" value="">Asignar departamento</option>
<option value="Production">Production</option>
<option value="Department 2">Department 2</option>
<option value="Department 3">Department 3</option>
</select>
To do this, pass the $departamentos variable from your Controller like so:
$departamentos = array('Production' => 'Production', 'Department 2' => 'Department 2', 'Department 3' => 'Department 3');
and an easy way to do that (as the answer https://stackoverflow.com/a/56673741/8093282 stated):
$departamentos = departamento::pluck('nombre_dep', 'nombre_dep');
Perform the pluck specifying in the key parameter (the second parameter), the field from which you want to get the value to use in the value attribute of the select.
$departamentos = departamento::pluck('nombre_dep', 'nombre_dep');
Then when you pass the $departamentos array to the Laravel Collective select(), it will create an <option> element for each array element, using the array key as value attribute and the array value as option tag content.
{{ Form::select('nombre_dep', $departamentos) }}

JQGrid Drop Down Menu/Text Field in one

I have a requirement to load a set of pre-defined values from the database but also give a user ability to enter a custom values as well. Good example is here:
https://jsfiddle.net/api/mdn/
<label>Choose a browser from this list:
<input list="browsers" name="myBrowser" /></label>
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
<option value="Microsoft Edge">
</datalist>
Is it possible to implement this feature in JQGrid?
This will only happen in the edit mode one per row. It should function exactly as as selecting from a drop down, only one value will be displayed and updated/saved. I will have to use is for only one column where each row will have a list of dynamic predefined values coming from the database.
Here is my start:
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Internet Explorer">
<option value="Opera">
<option value="Safari">
<option value="Microsoft Edge">
</datalist>
function myelem (value, options) {
var el = document.createElement("input");
el.type='text';
options.id = 'myBrowser';
el.setAttribute("list", "browsers");
return el;
}
The question is how to populate the datalist dynamically for each row based on the next column value and after exiting the edit mode save the selected value to the db?
Thank you,
If a value was previously saved for this column it should be displayed. When a user enters an edit mode the previously displayed value will be at the top (default) but the same set of predefined values should be available in the drop down with the option to enter a new custom value.
Thanks.
I found this very interesting and have created new article in our Guriddo Knowledge base where I explain in detail how to do this

How to develop cascading dropdowns in spring 4 using thymeleaf

i have a difficulty in developing cascading dropdowns in spring 4 with thymeleaf.
Here is my scenario:
i have 2 dropdowns like state and city. based on state selection, i need to populate city dropdown with corresponding cities for that state.
I am using Spring boot, spring 4 and thymeleaf template for view.
Thanks in advance.
Do you want to Dropdown / List selectors ?
Could I help you now. Please read this article.
Select fields have two parts: the <select> tag and its nested <option> tags. When creating this kind of field, only the <select> tag has to include a th:field attribute, but the th:value attributes in the nested <option> tags will be very important because they will provide the means of knowing which is the currently selected option (in a similar way to non-boolean checkboxes and radio buttons).
Let’s re-build the type field as a dropdown select:
<select th:field="*{type}">
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>
also you can check this Thymeleaf Website
You can use the appelsiini chained select to implement the cascading dropdown.
template.html
<select th:field="*{state}">
<option value="0">--</option>
<option th:each="state : ${states}" th:value="${state.stateId}" th:text="${state.name}">Florida</option>
</select>
<select th:field="*{city}">
<option value="0">--</option>
</select>
<script type="text/javascript">
$("#city").remoteChained({
parents: "#state",
url: /*[[#{/getStateCityValues}]]*/ ""
});
</script>
Controller.java
#PostMapping("/getStateCityValues")
public #ResponseBody
Map<String, String> getStateCityValues(#RequestParam("state") Integer state) {
Map<String, String> cityValues = new HashMap<>();
List<City> cities = cityService.getStateCities(state);
for(City city : cities){
cityValues.put(String.valueOf(city.getCityId()), city.getName());
}
return cityValues;
}

Validation engine with message over select field

I have a form here using the validation engine, I would like to have it so that if someone clicks on "no experience" a message comes up saying "you must have at least 1 year of experience" and they can't move on to submit the form if they have 1 year.
https://web.archive.org/web/20121129085036/http://www.barr-nunn.com/careers/form.php
How can I do that?
By Using Jquery Validation Engine:
<select id="experience" class="validate[required]">
<option >No Experience</option>
<option >Below 1 yr</option>
<option >1 yr</option>
<option >Above 1 yr</option>
</select>
You can achieve this by using on change function,
Ex :
$("#Selectbox").change(function(){
var selecVal = $(this).val();
if( selecVal == 'No Experience') {
$('#Selectbox').attr("class","validate[required]");
}
});
If you want pass custom message you do that also, refer this
http://posabsolute.github.io/jQuery-Validation-Engine/

Resources