Ive been trying to take a user input from the dropdown list and update it to the backend through controller.i will explain my code below,
JS
JavaScript
x
27
27
1
$(document).ready(function() {
2
3
$(".form-group input").hide() //hide inputs
4
$(".edit_save").click(function() {
5
var selector = $(this).closest(".form-group")
6
var btnText = $(this).text();
7
if (btnText === 'Edit') {
8
$(this).text('Save');
9
$(this).next("button").show(); //hide
10
selector.find("form span").hide() //span hide
11
selector.find("form input").show() //show inputs
12
} else {
13
$(this).text('Edit');
14
$(this).next("button").hide();
15
selector.find("form span").show()
16
selector.find("form input").hide()
17
18
var blood_group = document.getElementById("blood_group").value;
19
20
ajax.jsonRpc("/my/health-record-save", 'call', {
21
'sex': sex,
22
});
23
selector.find("span.blood_group").text(blood_group)
24
}
25
26
});
27
XML
JavaScript
1
26
26
1
<div class="form-group">
2
<form class="form-horizontal" style="bg-light">
3
<t>
4
<button type="button" class="edit_save">Edit</button>
5
<button class="cancel" type="button" style="display:none">Cancel</button>
6
</t>
7
<div class="col-md text-md-left" style="padding-top:10px">
8
<label class="text-secondary" for="blood_group"><strong>Blood group:</strong></label>
9
<select name="blood_group" id="blood_group">
10
<option>
11
<t t-esc ="hr.blood_group"/>
12
</option>
13
<option value="o+">O+</option>
14
<option value="o-">O-</option>
15
<option value="a+">A+</option>
16
<option value="a-">A-</option>
17
<option value="b-">B-</option>
18
<option value="b+">B+</option>
19
<option value="ab+">AB+</option>
20
<option value="ab-">AB-</option>
21
</select>
22
</div>
23
</form>
24
</div>
25
26
controller.py
JavaScript
1
10
10
1
@http.route(['/my/health-record-save'], type='json', auth="public", website=True)
2
def portal_save_health_record(self, **kw):
3
health_record_id = kw.get('h_record')
4
blood_group = kw.get('blood_group')
5
health_record = request.env['health.record'].search([('id', '=', health_record_id)])
6
health_record.write({
7
'blood_group': blood_group,
8
})
9
return request.redirect('/my/health-record')
10
What my concern is i dont know how to give the input class to <select>
<option>
dropdown list and make it to save to the backend through controller when the user click the button named save
Advertisement
Answer
It’s based on how you are render the view through- JS or controller.
Any way you should get hr.blood_group as a dictionary like – {‘o+’:”O+”,..}
then you can replace the option as below code.
JavaScript
1
5
1
<t t-set="blood_group" t-value="hr.blood_group"/>
2
<option t-foreach="blood_group" t-as="bgroup" t-att-value="bgroup">
3
<t t-esc="blood_group[bgroup]"/>
4
</option>
5
This is the base reference I can give, if you have any queries comment below.
Also, I’m adding a reference link from the website, take a look at it.