@hui
1、这个widget在odoo10的时候还是只是在project模块中自定义的一个widget,而且只是在form,kanban视图中有效果,list视图没有效果的。
2、这个widget不能通用的,odoo11好像可以通用了。
我抄写官方的代码,在我需要的模块自定义了一个status_selection的widget,测试效果可以。主要的js代码如下:
代码中50-59行代码,就是根据自定义的selection的key进行颜色的定义。
这个不是很懂,也不知道哪些是多余的,还有就是这个template用原来的好像不行,复制了一份的。:face_with_stuck-out_tongue_winking_eye:
odoo.define('sps_project.include_form_widget', function (require) {
'use strict';
var ajax = require('web.ajax');
var core = require('web.core');
var crash_manager = require('web.crash_manager');
var data = require('web.data');
var datepicker = require('web.datepicker');
var dom_utils = require('web.dom_utils');
var Priority = require('web.Priority');
var ProgressBar = require('web.ProgressBar');
var Dialog = require('web.Dialog');
var common = require('web.form_common');
var formats = require('web.formats');
var framework = require('web.framework');
var Model = require('web.DataModel');
var pyeval = require('web.pyeval');
var session = require('web.session');
var utils = require('web.utils');
var _t = core._t;
var QWeb = core.qweb;
var StatusSelection = common.AbstractField.extend({
template: "FormSelection",
events: {
'click a': function(e) {
e.preventDefault();
},
'mouseup a': function(e) {
e.stopPropagation();
},
'click li': 'set_kanban_selection'
},
start: function () {
// hook on form view content changed: recompute the states, because it may be related to the current stage
this.view.on('view_content_has_changed', this, function () {
this.render_value();
});
return this._super();
},
prepare_dropdown_selection: function() {
var self = this;
var _data = [];
_.map(self.field.selection || [], function(selection_item) {
var value = {
'name': selection_item[0],
'tooltip': selection_item[1],
};
if (selection_item[0] === 'initial') {
value.state_class = 'oe_kanban_status_grey';
value.state_name = selection_item[1];
} else if (selection_item[0] === '1st review') {
value.state_class = 'oe_kanban_status_green';
value.state_name = selection_item[1];
} else {
value.state_class = 'oe_kanban_status_red';
value.state_name = selection_item[1];
}
_data.push(value);
});
return _data;
},
render_value: function() {
this._super();
this.states = this.prepare_dropdown_selection();
var self = this;
// Adapt "FormSelection"
var current_state = _.find(this.states, function(state) {
return state.name === self.get('value');
});
this.$('.oe_kanban_status')
.removeClass('oe_kanban_status_red oe_kanban_status_green')
.addClass(current_state.state_class);
// Render "FormSelection.Items" and move it into "FormSelection"
var $items = $(QWeb.render('FormSelection.items', {
states: _.without(this.states, current_state)
}));
var $dropdown = this.$el.find('.dropdown-menu');
$dropdown.children().remove(); // remove old items
$items.appendTo($dropdown);
},
/* setting the value: in view mode, perform an asynchronous call and reload
the form view; in edit mode, use set_value to save the new value that will
be written when saving the record. */
set_kanban_selection: function (ev) {
var self = this;
ev.preventDefault();
var li = $(ev.target).closest('li');
if (li.length) {
var value = String(li.data('value'));
if (this.view.get('actual_mode') === 'view') {
var write_values = {};
write_values[self.name] = value;
return this.view.dataset._model.call(
'write', [
[this.view.datarecord.id],
write_values,
self.view.dataset.get_context()
]).done(self.reload_record.bind(self));
}
else {
return this.set_value(value);
}
}
},
reload_record: function() {
this.view.reload();
},
});
core.form_widget_registry.add('status_selection', StatusSelection);
return {
StatusSelection: StatusSelection,
};
});