odoo 自定义一个widget
方式一
js
odoo.define('your_module.custom_widget', function (require) {
"use strict";
const core = require('web.core');
const QWeb = core.qweb;
const {useState, useRef} = owl.hooks;
const AbstractField = require('web.AbstractField');
const fieldRegistry = require('web.field_registry');
const your_custom_widget = AbstractField.extend({
template: 'your_module.custom_widget',
init: function (parent, options) {
this._super.apply(this, arguments);
初始化代码
},
start: function () {
return this._super.apply(this, arguments);
启动代码
},
render: function() {
QWeb.add_template('./dynamic_progress_bar.xml');
},
其他自定义方法
});
fieldRegistry.add('your_module.custom_widget', your_custom_widget);
return {
your_custom_widget: your_custom_widget,
};
});
xml不带owl
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<div t-name="your_module.custom_widget">
<div class="test">111</div>
</div>
</templates>
xml带owl
注意要加owl="1" 否则报模板找不到
<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
<div t-name="your_module.custom_widget" owl="1">
<div class="test">111</div>
</div>
</templates>
最后 注册在__manifest__.py文件中,并在view<field name="xxx" widget="your_module.custom_widget"/>使用