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

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

Inherit a widget in js odoo 16

问题

  1. odoo 14我通过以下代码实现了对搜索选项的修改
  2. ```python
  3. odoo.define('customized_m2o_widget.Fields', function (require) {
  4. "use strict";
  5. var relational_fields = require('web.relational_fields');
  6. var FieldMany2One = relational_fields.FieldMany2One;
  7. FieldMany2One.include({
  8. _search: async function (searchValue = "") {
  9. const value = searchValue.trim();
  10. const domain = this.record.getDomain(this.recordParams);
  11. const context = Object.assign(
  12. this.record.getContext(this.recordParams),
  13. this.additionalContext
  14. );
  15. var values = await this._super.apply(this, arguments);
  16. if (this.limit >= values.length || this.limit < values.length) {
  17. values = this._manageSearchMore(values, value, domain, context);
  18. }
  19. return values;
  20. },
  21. })
  22. });

现在我需要迁移到odoo 16,有没有办法以正确的语法和语义代码来实现它,让它再次正常工作?谢谢提前

  1. <details>
  2. <summary>英文:</summary>
  3. There is an edit that I made on the search more option, I need it to be shown all the time,
  4. in odoo 14, i made it by this code :
  1. odoo.define(&#39;customized_m2o_widget.Fields&#39;, function (require) {
  2. &quot;use strict&quot;;
  3. var relational_fields = require(&#39;web.relational_fields&#39;);
  4. var FieldMany2One = relational_fields.FieldMany2One;
  5. FieldMany2One.include({
  6. _search: async function (searchValue = &quot;&quot;) {
  7. const value = searchValue.trim();
  8. const domain = this.record.getDomain(this.recordParams);
  9. const context = Object.assign(
  10. this.record.getContext(this.recordParams),
  11. this.additionalContext
  12. );
  13. var values = await this._super.apply(this, arguments);
  14. if (this.limit &gt;= values.length || this.limit &lt; values.length) {
  15. values = this._manageSearchMore(values, value, domain, context);
  16. }
  17. return values;
  18. },
  19. })
  20. });
  1. 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?
  2. thanks in advance
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 尝试对[many2one_field][0]小部件进行补丁,并将搜索限制设置为`-1`
  7. ***示例:***
  8. ```js
  9. /** @odoo-module **/
  10. import {patch} from "@web/core/utils/patch";
  11. import {Many2OneField} from "@web/views/fields/many2one/many2one_field";
  12. patch(Many2OneField.prototype, "Many2OneField.SearchMore", {
  13. get Many2XAutocompleteProps() {
  14. return {
  15. ...this._super(...arguments),
  16. searchLimit: -1,
  17. };
  18. }
  19. });

将以下条目添加到清单文件中:

  1. 'assets': {
  2. 'web.assets_backend': [
  3. 'MODULE_NAME/static/src/js/many2one_search_more.js',
  4. ],
  5. },

更新

继承relational_utils以覆盖loadOptionsSource函数

示例:

  1. import {patch} from "@web/core/utils/patch";
  2. import { Many2XAutocomplete } from "@web/views/fields/relational_utils";
  3. patch(Many2XAutocomplete.prototype, "Many2XAutocomplete.SearchLimit", {
  4. async loadOptionsSource(request) {
  5. var options = this._super(request);
  6. return options;
  7. }
  8. });
英文:

Try to patch the many2one_field widget and set the search limit to -1

Example:

  1. /** @odoo-module **/
  2. import {patch} from &quot;@web/core/utils/patch&quot;;
  3. import {Many2OneField} from &quot;@web/views/fields/many2one/many2one_field&quot;;
  4. patch(Many2OneField.prototype, &quot;Many2OneField.SearchMore&quot;, {
  5. get Many2XAutocompleteProps() {
  6. return {
  7. ...this._super(...arguments),
  8. searchLimit: -1,
  9. };
  10. }
  11. });

Add the following entry to the manifest file:

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

Update

Inherit relational_utils to override loadOptionsSource function

Example:

  1. import {patch} from &quot;@web/core/utils/patch&quot;;
  2. import { Many2XAutocomplete } from &quot;@web/views/fields/relational_utils&quot;
  3. patch(Many2XAutocomplete.prototype, &quot;Many2XAutocomplete.SearchLimit&quot;, {
  4. async loadOptionsSource(request) {
  5. var options = this._super(request);
  6. return options;
  7. }
  8. });

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:

确定