Odoo 9 如何将自定义视图模板附加到现有视图中?
Odoo 9 how to append custom view template into existing views?
我想在 KanbanView.buttons 和 KanbanView.Group 之间附加一个模板,以便在我查看 "project.task" 看板视图时在 "project.project" 模型中显示描述字段。
我想应该有什么地方可以将这些模板附加到看板视图中,但不幸的是,我找不到它。
我做的是
我创建了自定义模板
layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<templates>
<t t-name="eric-kanban-view">
<div>This is Eric's kanban view</div>
</t>
</templates>
</data>
</openerp>
我想将模板添加到 "project.view_task_kanban" 以定位在看板视图上方
project.xml
<record id="project_task_custom_kanban" model="ir.ui.view">
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="model">project.task</field>
<field name="arch" type="xml">
<xpath expr="//templates" position="before">
<t t-call="eric-kanban-view"/>
</xpath>
</field>
</record>
openerp.py
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name' : 'project_customized',
'version' : '1.1',
'author' : "Eric Lee",
'description': 'eric\'s Project customized module',
'installable' : True,
'data' : [
'project.xml',
],
'depends' : [
'project',
],
'qweb': [
'static/src/xml/layouts.xml',
'static/src/xml/project.xml',
],
}
但是什么也没发生。
下面是我想要的布局
<record id="project_task_custom_kanban" model="ir.ui.view">
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="model">project.task</field>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
<field name="project_id.name"/>
</xpath>
</field>
</record>
所以这个例子应该运行良好。在任务名称之后将显示项目名称。
您需要通过扩展看板视图来创建一个小部件。
示例如下:
这里是示例代码。
static/src/xml/template.xml
<template id="template" xml:space="preserve">
<t t-name="kanban.sample_header">
<div class="sample_header" style="width:100%;height:50px;background-color:#fff;">
<h2>Sample header</h2>
</div>
</t>
</template>
static/src/js/sample.js
odoo.define('kanban.sample_dashboard', function (require) {
"use strict";
var core = require('web.core');
var formats = require('web.formats');
var Model = require('web.Model');
var session = require('web.session');
var KanbanView = require('web_kanban.KanbanView');
var QWeb = core.qweb;
var _t = core._t;
var _lt = core._lt;
var sampleHeader = KanbanView.extend({
display_name: _lt('Dashboard'),
icon: 'fa-dashboard',
fetch_data: function() {
// Overwrite this function with useful data
return $.when();
},
render: function() {
var super_render = this._super;
var self = this;
return this.fetch_data().then(function(result){
self.show_demo = result && result.nb_opportunities === 0;
var sales_dashboard = QWeb.render('kanban.sample_header', {
widget: self,
show_demo: self.show_demo,
values: result,
});
super_render.call(self);
$(sales_dashboard).prependTo(self.$el);
});
},
});
core.view_registry.add('sample_header', sampleHeader);
return sampleHeader;
})
view.xml
<odoo>
<data>
<template id="assets_backend" name="sample_header" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/kanban/static/src/js/sample_js.js"></script>
</xpath>
</template>
<record model="ir.ui.view" id="view_task_kanban_inherit">
<field name="name">project.task.kanban.inherit</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="arch" type="xml">
<xpath expr="//kanban" position="attributes">
<attribute name='js_class'>sample_header</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
希望对您有所帮助。
我想在 KanbanView.buttons 和 KanbanView.Group 之间附加一个模板,以便在我查看 "project.task" 看板视图时在 "project.project" 模型中显示描述字段。
我想应该有什么地方可以将这些模板附加到看板视图中,但不幸的是,我找不到它。
我做的是
我创建了自定义模板 layouts.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<templates>
<t t-name="eric-kanban-view">
<div>This is Eric's kanban view</div>
</t>
</templates>
</data>
</openerp>
我想将模板添加到 "project.view_task_kanban" 以定位在看板视图上方 project.xml
<record id="project_task_custom_kanban" model="ir.ui.view">
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="model">project.task</field>
<field name="arch" type="xml">
<xpath expr="//templates" position="before">
<t t-call="eric-kanban-view"/>
</xpath>
</field>
</record>
openerp.py
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name' : 'project_customized',
'version' : '1.1',
'author' : "Eric Lee",
'description': 'eric\'s Project customized module',
'installable' : True,
'data' : [
'project.xml',
],
'depends' : [
'project',
],
'qweb': [
'static/src/xml/layouts.xml',
'static/src/xml/project.xml',
],
}
但是什么也没发生。
下面是我想要的布局
<record id="project_task_custom_kanban" model="ir.ui.view">
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="model">project.task</field>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
<field name="project_id.name"/>
</xpath>
</field>
</record>
所以这个例子应该运行良好。在任务名称之后将显示项目名称。
您需要通过扩展看板视图来创建一个小部件。
示例如下:
这里是示例代码。
static/src/xml/template.xml
<template id="template" xml:space="preserve">
<t t-name="kanban.sample_header">
<div class="sample_header" style="width:100%;height:50px;background-color:#fff;">
<h2>Sample header</h2>
</div>
</t>
</template>
static/src/js/sample.js
odoo.define('kanban.sample_dashboard', function (require) {
"use strict";
var core = require('web.core');
var formats = require('web.formats');
var Model = require('web.Model');
var session = require('web.session');
var KanbanView = require('web_kanban.KanbanView');
var QWeb = core.qweb;
var _t = core._t;
var _lt = core._lt;
var sampleHeader = KanbanView.extend({
display_name: _lt('Dashboard'),
icon: 'fa-dashboard',
fetch_data: function() {
// Overwrite this function with useful data
return $.when();
},
render: function() {
var super_render = this._super;
var self = this;
return this.fetch_data().then(function(result){
self.show_demo = result && result.nb_opportunities === 0;
var sales_dashboard = QWeb.render('kanban.sample_header', {
widget: self,
show_demo: self.show_demo,
values: result,
});
super_render.call(self);
$(sales_dashboard).prependTo(self.$el);
});
},
});
core.view_registry.add('sample_header', sampleHeader);
return sampleHeader;
})
view.xml
<odoo>
<data>
<template id="assets_backend" name="sample_header" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/kanban/static/src/js/sample_js.js"></script>
</xpath>
</template>
<record model="ir.ui.view" id="view_task_kanban_inherit">
<field name="name">project.task.kanban.inherit</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_kanban"/>
<field name="arch" type="xml">
<xpath expr="//kanban" position="attributes">
<attribute name='js_class'>sample_header</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
希望对您有所帮助。