英文:
Inherit a widget in js odoo 16
问题 {#heading}
在odoo 14中,我通过以下代码实现了对搜索选项的修改:
```python
odoo.define('customized_m2o_widget.Fields', function (require) {
"use strict";
var relational_fields = require('web.relational_fields');
var FieldMany2One = relational_fields.FieldMany2One;
FieldMany2One.include({
_search: async function (searchValue = "") {
const value = searchValue.trim();
const domain = this.record.getDomain(this.recordParams);
const context = Object.assign(
this.record.getContext(this.recordParams),
this.additionalContext
);
var values = await this._super.apply(this, arguments);
if (this.limit >= values.length || this.limit < values.length) {
values = this._manageSearchMore(values, value, domain, context);
}
return values;
},
})
});
现在我需要迁移到odoo 16,有没有办法以正确的语法和语义代码来实现它,让它再次正常工作?谢谢提前
<details>
<summary>英文:</summary>
There is an edit that I made on the search more option, I need it to be shown all the time,
in odoo 14, i made it by this code :
odoo.define('customized_m2o_widget.Fields', function (require) {
"use strict";
var relational_fields = require('web.relational_fields');
var FieldMany2One = relational_fields.FieldMany2One;
FieldMany2One.include({
_search: async function (searchValue = "") {
const value = searchValue.trim();
const domain = this.record.getDomain(this.recordParams);
const context = Object.assign(
this.record.getContext(this.recordParams),
this.additionalContext
);
var values = await this._super.apply(this, arguments);
if (this.limit >= values.length || this.limit < values.length) {
values = this._manageSearchMore(values, value, domain, context);
}
return values;
},
})
});
now I need to migrate to odoo 16, so anyone have a way to do it in the write syntax and semantic code to let it works again?
thanks in advance
</details>
# 答案1
**得分**: 1
尝试对[many2one_field][0]小部件进行补丁,并将搜索限制设置为`-1`
***示例:***
```js
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import {Many2OneField} from "@web/views/fields/many2one/many2one_field";
patch(Many2OneField.prototype, "Many2OneField.SearchMore", {
get Many2XAutocompleteProps() {
return {
...this._super(...arguments),
searchLimit: -1,
};
}
});
将以下条目添加到清单文件中:
'assets': {
'web.assets_backend': [
'MODULE_NAME/static/src/js/many2one_search_more.js',
],
},
更新
继承relational_utils
以覆盖loadOptionsSource
函数
示例:
import {patch} from "@web/core/utils/patch";
import { Many2XAutocomplete } from "@web/views/fields/relational_utils";
patch(Many2XAutocomplete.prototype, "Many2XAutocomplete.SearchLimit", {
async loadOptionsSource(request) {
var options = this._super(request);
return options;
}
});
英文:
Try to patch the many2one_field widget and set the search limit to -1
Example:
/** @odoo-module **/
import {patch} from "@web/core/utils/patch";
import {Many2OneField} from "@web/views/fields/many2one/many2one_field";
patch(Many2OneField.prototype, "Many2OneField.SearchMore", {
get Many2XAutocompleteProps() {
return {
...this._super(...arguments),
searchLimit: -1,
};
}
});
Add the following entry to the manifest file:
'assets': {
'web.assets_backend': [
'MODULE_NAME/static/src/js/many2one_search_more.js',
],
},
Update
Inherit relational_utils
to override loadOptionsSource
function
Example:
import {patch} from "@web/core/utils/patch";
import { Many2XAutocomplete } from "@web/views/fields/relational_utils"
patch(Many2XAutocomplete.prototype, "Many2XAutocomplete.SearchLimit", {
async loadOptionsSource(request) {
var options = this._super(request);
return options;
}
});