奥多。隐藏字段选择中的一些选项
Odoo. Hide some options in field selection
我的模型中有一些选择字段。这里的例子:
class MyModel(models.Model):
_name = 'my_app.my_model'
example_selection = fields.Selection(
[
('first', 'First'),
('second', 'Second'),
# etc.
],
string='My selection',
)
在某些情况下,我需要隐藏选择中的特定选项(或单选按钮)。我怎样才能正确地做到这一点?
下方来自 基本日历模块 的屏幕可以更详细地解释我的问题。
提前致谢。
据我所知,这是不可能的,但是如果您在可以使用
的视图中使用 MAny2one 而不是选择(因此使用域)结束,则可以实现类似的效果
<field name="example_with_domain" widget="selection"/>
获得选择字段的相同视觉行为(不创建,不编辑)。
我找到了解决办法。
首先,我们需要为 FieldSelection 创建自定义小部件。这里的例子(path_to_your_module/static/src/js/form_widgets.js):
odoo.define('your_module.form_widgets', function (require) {
"use strict";
var core = require('web.core');
var FieldSelection = core.form_widget_registry.get('selection');
var MySelection = FieldSelection.extend({
// add events to base events of FieldSelection
events: _.defaults({
// we will change of visibility on focus of field
'focus select': 'onFocus'
}, FieldSelection.prototype.events),
onFocus: function() {
if (
// check values of fields. for example I need to check many fields
this.field_manager.fields.name_field_1.get_value() == 'value1' &&
this.field_manager.fields.name_field_2.get_value() == 'value2' /* && etc fields...*/
) {
// for example just hide all options. You can create any kind of logic here
this.$el.find('option').hide();
}
}
});
// register your widget
core.form_widget_registry.add('your_selection', MySelection);
});
在此之后,您只需将小部件设置为视图中的字段,如下所示:
<field name="example_selection" widget="your_selection"/>
如果您不知道如何包含模块 .
的 static
我希望这对某人有所帮助 ;)
有点晚了。就我而言,我是这样做的:
odoo.define('my_module.custom_selection', function(require) {
"use strict";
var registry = require('web.field_registry');
var relational_fields = require('web.relational_fields');
var MySelection = relational_fields.FieldRadio.extend({
init: function() {
this._super.apply(this, arguments);
// use to decrement in splice, bc position change when element is removed
let decrement = 0;
// this.values can be undefined or [[], [], []]
// copying the content of original array or []
let value_copies = this.values? [...this.values]: [];
for (let index = 0; index < value_copies.length; index++) {
// 'other' is the value to be removed
if (value_copies[index].includes('other')) {
this.values.splice(index - decrement, 1);
decrement++;
}
}
},
});
registry.add('custom_selection', MySelection);
return MySelection;
});
你可以在这里查看我的回购协议:https://github.com/m0r7y/wdgt_hide_option
我的模型中有一些选择字段。这里的例子:
class MyModel(models.Model):
_name = 'my_app.my_model'
example_selection = fields.Selection(
[
('first', 'First'),
('second', 'Second'),
# etc.
],
string='My selection',
)
在某些情况下,我需要隐藏选择中的特定选项(或单选按钮)。我怎样才能正确地做到这一点?
下方来自 基本日历模块 的屏幕可以更详细地解释我的问题。
提前致谢。
据我所知,这是不可能的,但是如果您在可以使用
的视图中使用 MAny2one 而不是选择(因此使用域)结束,则可以实现类似的效果<field name="example_with_domain" widget="selection"/>
获得选择字段的相同视觉行为(不创建,不编辑)。
我找到了解决办法。
首先,我们需要为 FieldSelection 创建自定义小部件。这里的例子(path_to_your_module/static/src/js/form_widgets.js):
odoo.define('your_module.form_widgets', function (require) {
"use strict";
var core = require('web.core');
var FieldSelection = core.form_widget_registry.get('selection');
var MySelection = FieldSelection.extend({
// add events to base events of FieldSelection
events: _.defaults({
// we will change of visibility on focus of field
'focus select': 'onFocus'
}, FieldSelection.prototype.events),
onFocus: function() {
if (
// check values of fields. for example I need to check many fields
this.field_manager.fields.name_field_1.get_value() == 'value1' &&
this.field_manager.fields.name_field_2.get_value() == 'value2' /* && etc fields...*/
) {
// for example just hide all options. You can create any kind of logic here
this.$el.find('option').hide();
}
}
});
// register your widget
core.form_widget_registry.add('your_selection', MySelection);
});
在此之后,您只需将小部件设置为视图中的字段,如下所示:
<field name="example_selection" widget="your_selection"/>
如果您不知道如何包含模块
我希望这对某人有所帮助 ;)
有点晚了。就我而言,我是这样做的:
odoo.define('my_module.custom_selection', function(require) {
"use strict";
var registry = require('web.field_registry');
var relational_fields = require('web.relational_fields');
var MySelection = relational_fields.FieldRadio.extend({
init: function() {
this._super.apply(this, arguments);
// use to decrement in splice, bc position change when element is removed
let decrement = 0;
// this.values can be undefined or [[], [], []]
// copying the content of original array or []
let value_copies = this.values? [...this.values]: [];
for (let index = 0; index < value_copies.length; index++) {
// 'other' is the value to be removed
if (value_copies[index].includes('other')) {
this.values.splice(index - decrement, 1);
decrement++;
}
}
},
});
registry.add('custom_selection', MySelection);
return MySelection;
});
你可以在这里查看我的回购协议:https://github.com/m0r7y/wdgt_hide_option