Skip to content

Commit 1dda29b

Browse files
miguel-S73Reyes4711-S73
authored andcommitted
[IMP] website_field_autocomplete: compatibility many2one fields
Show Input with `displayField` value and generate hide field with `valueField` value
1 parent a06af7d commit 1dda29b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

website_field_autocomplete/README.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Following is a breakdown of the available attributes & their defaults:
3636
+====================+=============================================+===============+==========+
3737
| data-model | Model name to query | | True |
3838
+--------------------+---------------------------------------------+---------------+----------+
39+
| data-field | Field name in Model | | False |
40+
+--------------------+---------------------------------------------+---------------+----------+
3941
| data-query-field | Field to query when searching | name | False |
4042
+--------------------+---------------------------------------------+---------------+----------+
4143
| data-display-field | Field to display | query-field | False |
@@ -75,6 +77,8 @@ Contributors
7577
------------
7678

7779
* Dave Lasley <[email protected]>
80+
* `Studio73 <https://www.studio73.es>`__:
81+
* Miguel Gandia <[email protected]>
7882

7983
Maintainer
8084
----------

website_field_autocomplete/static/src/js/field_autocomplete.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,29 @@ odoo.define("website_field_autocomplete.field_autocomplete", function (require)
4141

4242
/* Return arguments that are used to initialize autocomplete */
4343
autocompleteArgs: function () {
44+
const self = this;
4445
return {
4546
source: (request, response) => {
4647
this.autocomplete(request, response);
4748
},
49+
focus: function (event, ui) {
50+
self.$target.val(ui.item.label);
51+
self.many2oneCompatibility(ui.item);
52+
return false;
53+
},
54+
select: function (event, ui) {
55+
self.$target.val(ui.item.label);
56+
self.many2oneCompatibility(ui.item);
57+
return false;
58+
},
4859
};
4960
},
5061

5162
start: function () {
5263
this.model = this.$target.data("model");
5364
this.queryField = this.$target.data("query-field") || "name";
5465
this.displayField = this.$target.data("display-field") || this.queryField;
66+
this.field = this.$target.data("field") || this.field;
5567
this.valueField = this.$target.data("value-field") || this.displayField;
5668
this.limit = this.$target.data("limit") || 10;
5769
this.add_domain = this.$target.data("domain");
@@ -62,5 +74,18 @@ odoo.define("website_field_autocomplete.field_autocomplete", function (require)
6274
this.$target.autocomplete(this.autocompleteArgs());
6375
return this._super(...arguments);
6476
},
77+
78+
many2oneCompatibility: function (item) {
79+
if (!this.field) return;
80+
let formField = $(this.el.closest("form")).find(
81+
`input[name='${this.field}']`
82+
);
83+
if (!formField.length) {
84+
formField = $(this.el.closest("form")).append(
85+
`<input class="d-none" name='${this.field}' />`
86+
);
87+
}
88+
formField.val(item.value);
89+
},
6590
});
6691
});

0 commit comments

Comments
 (0)