继承一个小部件在js odoo 16中

huangapple go评论93阅读模式
英文:

Inherit a widget in js odoo 16

问题

在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(&#39;customized_m2o_widget.Fields&#39;, function (require) {
&quot;use strict&quot;;

var relational_fields = require(&#39;web.relational_fields&#39;);
var FieldMany2One = relational_fields.FieldMany2One;

FieldMany2One.include({
_search: async function (searchValue = &quot;&quot;) {
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 &gt;= values.length || this.limit &lt; 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 &quot;@web/core/utils/patch&quot;;
import {Many2OneField} from &quot;@web/views/fields/many2one/many2one_field&quot;;

patch(Many2OneField.prototype, &quot;Many2OneField.SearchMore&quot;, {
    get Many2XAutocompleteProps() {
        return {
            ...this._super(...arguments),
            searchLimit: -1,
        };
    }
});

Add the following entry to the manifest file:

&#39;assets&#39;: {
    &#39;web.assets_backend&#39;: [
        &#39;MODULE_NAME/static/src/js/many2one_search_more.js&#39;,
    ],
},

Update

Inherit relational_utils to override loadOptionsSource function

Example:

import {patch} from &quot;@web/core/utils/patch&quot;;
import { Many2XAutocomplete } from &quot;@web/views/fields/relational_utils&quot;


patch(Many2XAutocomplete.prototype, &quot;Many2XAutocomplete.SearchLimit&quot;, {

    async loadOptionsSource(request) {
        var options = this._super(request);
        return options;
    }
});

huangapple
  • 本文由 发表于 2023年8月10日 16:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定