Reputation: 342
How can I set the value of a field using javascript (on the client)?
In my view's XML-file, I have:
<field name="zip" />
<field name="city" class="city" />
When the zip changes, I want to do a lookup and set the city (I could do this on the server side with an @api.onchange method, but for performance reasons, I prefer client side).
The lookup works, and I can set the value with:
$('span.city input').val(city);
This puts the city in the input field, but the client doesn't get aware of the change (for instance, I have a server side onchange method to handle other fields, and this method does not get the new city value).
From what I can find, I should call set_value(city) on the field, but how do I find the right object to call the method on?
Upvotes: 2
Views: 2976
Reputation: 342
I found a solution. In my zip widget, I find the parent and save the fields list:
openerp.zip_widget = function(instance) {
var _t = instance.web._t,
_lt = instance.web._lt;
var QWeb = instance.web.qweb;
var fields; // <-- Variable to keep the field list
instance.web.form.widgets.add('zip', 'instance.zip_widget.zip_lookup');
instance.zip_widget.zip_lookup = instance.web.form.FieldChar.extend({
template: "zip_widget",
start: function() {
this._super();
fields = this.getParent().fields; // <-- Get the field list
},
Now I can set a field value with fields.city.set_value(...);
Upvotes: 1
Reputation: 846
Please take a look at hr_timesheet_sheet
module in static/src/js/timesheet.js
. It will give you a clear view of how you can create field and give it a value.
Upvotes: 1