odoo14在tree、kanban视图上添加dashboard
效果图:
实现代码:js:view的类型原来1个js给拆分成了4个: view, controller, renderer, model
1、view:AbstractView的子类,这是工厂类:类需要解析 arch字段并设置其它3个类
2、Renderer :渲染器,来自 AbstractRenderer:负责在用户界面中展示数据;
3、Controller:一个控制器 (来看AbstractController):用于协调、与网页客户端对话
4、Model:一个模型 (来自 AbstractModel):用于和服务端对话、加载数据并处理数据
步骤:一、二、三、四案例
一、MVCR:
var MapController = AbstractController.extend({});
var MapRenderer = AbstractRenderer.extend({});
var MapModel = AbstractModel.extend({});
var MapView = AbstractView.extend({
config: {
Model: MapModel,
Controller: MapController,
Renderer: MapRenderer,
},
});
二、告知网页客户端视图名称和实际类之间的映射。
var viewRegistry = require('web.view_registry');
viewRegistry.add('helpdesk_dashboard', HelpdeskDashboardView);
三、现在我们需要告知网页客户端指定的 ir.ui.view需要使用我们的新类。注意这是一个网页客户端的具体考虑。
从服务端的视角来看,我们还是对应看板视图。这么做的相应方式是通过对框架的根节点使用特殊属性js_class
<record id="helpdesk_team_view_kanban" model="ir.ui.view" >
...
<field name="arch" type="xml">
<kanban js_class="helpdesk_dashboard">
...
</kanban>
</field>
</record>
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | odoo.define( 'purchase.dashboard' , function (require) { "use strict" ; / * * * This file defines the Purchase Dashboard view (alongside its renderer, model * and controller). This Dashboard is added to the top of list and kanban Purchase * views, it extends both views with essentially the same code except for * _onDashboardActionClicked function so we can apply filters without changing our * current view. * / var core = require( 'web.core' ); / / 这里是列表视图上的扩展:导入的模块 var ListController = require( 'web.ListController' ); var ListModel = require( 'web.ListModel' ); var ListRenderer = require( 'web.ListRenderer' ); var ListView = require( 'web.ListView' ); / / 这里是kanban视图上的扩展: var KanbanController = require( 'web.KanbanController' ); var KanbanModel = require( 'web.KanbanModel' ); var KanbanRenderer = require( 'web.KanbanRenderer' ); var KanbanView = require( 'web.KanbanView' ); var SampleServer = require( 'web.SampleServer' ); / / 注册模块 var view_registry = require( 'web.view_registry' ); var QWeb = core.qweb; / / Add mock of method 'retrieve_dashboard' in SampleServer, so that we can have / / the sample data in empty purchase kanban and list view let dashboardValues; SampleServer.mockRegistry.add( 'purchase.order/retrieve_dashboard' , () = > { return Object .assign({}, dashboardValues); }); / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / / List View / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / / 渲染renderer var PurchaseListDashboardRenderer = ListRenderer.extend({ events:_.extend({}, ListRenderer.prototype.events, { 'click .o_dashboard_action' : '_onDashboardActionClicked' , }), / * * * @override * @private * @returns {Promise} * / _renderView: function () { var self = this; return this._super. apply (this, arguments).then(function () { var values = self .state.dashboardValues; var purchase_dashboard = QWeb.render( 'purchase.PurchaseDashboard' , { values: values, }); self .$el.prepend(purchase_dashboard); }); }, / * * * @private * @param {MouseEvent} * / _onDashboardActionClicked: function (e) { e.preventDefault(); var $action = $(e.currentTarget); this.trigger_up( 'dashboard_open_action' , { action_name: $action.attr( 'name' ) + "_list" , action_context: $action.attr( 'context' ), }); }, }); / / 模型 model var PurchaseListDashboardModel = ListModel.extend({ / * * * @override * / init: function () { this.dashboardValues = {}; this._super. apply (this, arguments); }, / * * * @override * / __get: function (localID) { var result = this._super. apply (this, arguments); if (_.isObject(result)) { result.dashboardValues = this.dashboardValues[localID]; } return result; }, / * * * @override * @returns {Promise} * / __load: function () { return this._loadDashboard(this._super. apply (this, arguments)); }, / * * * @override * @returns {Promise} * / __reload: function () { return this._loadDashboard(this._super. apply (this, arguments)); }, / * * * @private * @param {Promise} super_def a promise that resolves with a dataPoint id * @returns {Promise - > string} resolves to the dataPoint id * / _loadDashboard: function (super_def) { var self = this; var dashboard_def = this._rpc({ model: 'purchase.order' , method: 'retrieve_dashboard' , }); return Promise. all ([super_def, dashboard_def]).then(function(results) { var id = results[ 0 ]; dashboardValues = results[ 1 ]; self .dashboardValues[ id ] = dashboardValues; return id ; }); }, }); / / 控制器:controller var PurchaseListDashboardController = ListController.extend({ custom_events: _.extend({}, ListController.prototype.custom_events, { dashboard_open_action: '_onDashboardOpenAction' , }), / * * * @private * @param {OdooEvent} e * / _onDashboardOpenAction: function (e) { return this.do_action(e.data.action_name, {additional_context: JSON.parse(e.data.action_context)}); }, }); / / 视图 view var PurchaseListDashboardView = ListView.extend({ config: _.extend({}, ListView.prototype.config, { Model: PurchaseListDashboardModel, Renderer: PurchaseListDashboardRenderer, Controller: PurchaseListDashboardController, }), }); / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - / / Kanban View / / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - var PurchaseKanbanDashboardRenderer = KanbanRenderer.extend({ events:_.extend({}, KanbanRenderer.prototype.events, { 'click .o_dashboard_action' : '_onDashboardActionClicked' , }), / * * * @override * @private * @returns {Promise} * / _render: function () { var self = this; return this._super. apply (this, arguments).then(function () { var values = self .state.dashboardValues; var purchase_dashboard = QWeb.render( 'purchase.PurchaseDashboard' , { values: values, }); self .$el.prepend(purchase_dashboard); }); }, / * * * @private * @param {MouseEvent} * / _onDashboardActionClicked: function (e) { e.preventDefault(); var $action = $(e.currentTarget); this.trigger_up( 'dashboard_open_action' , { action_name: $action.attr( 'name' ) + "_kanban" , action_context: $action.attr( 'context' ), }); }, }); var = KanbanModel.extend({ / * * * @override * / init: function () { this.dashboardValues = {}; this._super. apply (this, arguments); }, / * * * @override * / __get: function (localID) { var result = this._super. apply (this, arguments); if (_.isObject(result)) { result.dashboardValues = this.dashboardValues[localID]; } return result; }, / * * * @override * @returns {Promise} * / __load: function () { return this._loadDashboard(this._super. apply (this, arguments)); }, / * * * @override * @returns {Promise} * / __reload: function () { return this._loadDashboard(this._super. apply (this, arguments)); }, / * * * @private * @param {Promise} super_def a promise that resolves with a dataPoint id * @returns {Promise - > string} resolves to the dataPoint id * / _loadDashboard: function (super_def) { var self = this; var dashboard_def = this._rpc({ model: 'purchase.order' , method: 'retrieve_dashboard' , }); return Promise. all ([super_def, dashboard_def]).then(function(results) { var id = results[ 0 ]; dashboardValues = results[ 1 ]; self .dashboardValues[ id ] = dashboardValues; return id ; }); }, }); var PurchaseKanbanDashboardController = KanbanController.extend({ custom_events: _.extend({}, KanbanController.prototype.custom_events, { dashboard_open_action: '_onDashboardOpenAction' , }), / * * * @private * @param {OdooEvent} e * / _onDashboardOpenAction: function (e) { return this.do_action(e.data.action_name, {additional_context: JSON.parse(e.data.action_context)}); }, }); 通过VIEW来设置它 var PurchaseKanbanDashboardView = KanbanView.extend({ config: _.extend({}, KanbanView.prototype.config, { Model: PurchaseKanbanDashboardModel, Renderer: PurchaseKanbanDashboardRenderer, Controller: PurchaseKanbanDashboardController, }), }); / / 视图类型和实际类之间的映射需要进行更新 view_registry.add( 'purchase_list_dashboard' , PurchaseListDashboardView); view_registry.add( 'purchase_kanban_dashboard' , PurchaseKanbanDashboardView); return { PurchaseListDashboardModel: PurchaseListDashboardModel, PurchaseListDashboardRenderer: PurchaseListDashboardRenderer, PurchaseListDashboardController: PurchaseListDashboardController, PurchaseKanbanDashboardModel: PurchaseKanbanDashboardModel, PurchaseKanbanDashboardRenderer: PurchaseKanbanDashboardRenderer, PurchaseKanbanDashboardController: PurchaseKanbanDashboardController }; }); |
Dashboard的page的xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | <?xml version = "1.0" encoding = "UTF-8" ?> <templates> <! - - This template is for a table at the top of purchase views that shows some KPIs. - - > <t t - name = "purchase.PurchaseDashboard" > <div class = "o_purchase_dashboard container" > <div class = "row" > <div class = "col-sm-5" > <table class = "table table-sm" > <! - - thead needed to avoid list view rendering error for some reason - - > <thead> <tr> <! - - can't use th tag due to list rendering error when no values in list ... - - > <td class = "o_text" > <div> All RFQs< / div> < / td> <td class = "o_main o_dashboard_action" title = "All Draft RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_draft_rfqs": true}' > <a href = "#" ><t t - esc = "values['all_to_send']" / ><br / >To Send< / a> < / td> <td class = "o_main o_dashboard_action" title = "All Waiting RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_waiting_rfqs": true}' > <a href = "#" ><t t - esc = "values['all_waiting']" / ><br / >Waiting< / a> < / td> <td class = "o_main o_dashboard_action" title = "All Late RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_late_rfqs": true}' > <a href = "#" ><t t - esc = "values['all_late']" / ><br / >Late< / a> < / td> < / tr> < / thead> <tbody> <tr> <td class = "o_text" > <div>My RFQs< / div> < / td> <td class = "o_main o_dashboard_action" title = "My Draft RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_draft_rfqs": true, "search_default_my_purchases": true}' > <a href = "#" ><t t - esc = "values['my_to_send']" / >< / a> < / td> <td class = "o_main o_dashboard_action" title = "My Waiting RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_waiting_rfqs": true, "search_default_my_purchases": true}' > <a href = "#" ><t t - esc = "values['my_waiting']" / >< / a> < / td> <td class = "o_main o_dashboard_action" title = "My Late RFQs" name = "purchase.purchase_action_dashboard" context = '{"search_default_late_rfqs": true, "search_default_my_purchases": true}' > <a href = "#" ><t t - esc = "values['my_late']" / >< / a> < / td> < / tr> < / tbody> < / table>< / div> <div class = "col-sm-7" > <table class = "table table-sm" > <! - - thead needed to avoid list view rendering error for some reason - - > <thead> <tr> <! - - can't use th tag due to list rendering error when no values in list ... - - > <td class = "o_text" >Avg Order Value (<t t - esc = "values['company_currency_symbol']" / >)< / td> <td><span><t t - esc = "values['all_avg_order_value']" / >< / span>< / td> <td class = "o_text" >Purchased Last 7 Days (<t t - esc = "values['company_currency_symbol']" / >)< / td> <td><span><t t - esc = "values['all_total_last_7_days']" / >< / span>< / td> < / tr> < / thead> <tbody> <tr> <td class = "o_text" >Lead Time to Purchase< / td> <td><span><t t - esc = "values['all_avg_days_to_purchase']" / > Days< / span>< / td> <td class = "o_text" >RFQs Sent Last 7 Days< / td> <td><span><t t - esc = "values['all_sent_rfqs']" / >< / span>< / td> < / tr> < / tbody> < / table>< / div> < / div>< / div> < / t> < / templates> |
导入js
1 2 3 4 5 6 7 8 9 10 11 12 | <?xml version = "1.0" ?> <odoo> <template id = "assets_backend" name = "purchase assets" inherit_id = "web.assets_backend" > <xpath expr = "." position = "inside" > <link rel = "stylesheet" type = "text/scss" href = "/purchase/static/src/scss/purchase.scss" / > <script type = "text/javascript" src = "/purchase/static/src/js/purchase_dashboard.js" >< / script> < / xpath> < / template> < / odoo> |
最后引用:
1 2 3 4 5 6 7 | 1. <kanban class = "o_kanban_mobile" js_class = "purchase_kanban_dashboard" sample = "1" >< / kanban> 2. <tree class = "o_kanban_mobile" js_class = "purchase_kanban_dashboard" sample = "1" >< / tree> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示