Odoo14 OWL 如何訪問model方法和res_id


首先OWL是Odoo14版本新加的功能。

因為是新加的所以並沒有太多的說明文檔,包括英文板文檔也沒有;所以你要用它再沒有更詳細的文檔之前你得自己去看源碼。

注意owl是沒有do_action函數給你跳轉至其他視圖的。你如果要你的控件可以跳轉視圖的話就得用“web.AbstractField”去實現了。

owl如何訪問記錄res_id(或者其他記錄信息):

1 this.record.res_id

 

owl例子:

 1 odoo.define('my_company_users_widget', function (require) {
 2     "use strict";
 3 
 4     const { Component } = owl;
 5     const AbstractField = require('web.AbstractFieldOwl');
 6     const fieldRegistry = require('web.field_registry_owl');
 7 
 8     //這里生成子控件
 9     class UserCardInfo extends Component {
10         static template = 'UserCardInfo';
11         CardClicked() {
12             this.trigger('card-clicked', {user_id: this.props.user_id});
13         }
14     }
15 
16     class FieldCompanyUser extends AbstractField {
17         static supportedFieldTypes = ['many2many'];
18         static template = 'OWLFieldCompanyUsers';
19         static components = { UserCardInfo };//子控件聲明,這樣你就可以在界面上使用了
20         constructor(...args) {
21             super(...args);
22             this.data_users = [];
23         }
24         async willStart() {
25             self = this
26             //
27             //
28             await this.rpc({
29                 model: 'res.groups', 
30                 method: 'get_company_users',
31                 args: [[self.record.res_id],self.record.res_id]
32             }).then(function (result){
33                 self.data_users = result
34             });
35         }
36         UserCardClicked(ev) {
37             console.log(ev.detail.user_id);
38             self = this;
39             this.rpc({//不知道怎么用rpc 40                 model: 'res.users', 
41                 method: 'get_userform_action',
42                 args: [[ev.detail.user_id]]
43             }).then(function (result){
44                 self.action = result;
45             });
46             console.log(self.action);
47             
48             //owl是沒有do_action函數給你跳轉至其他視圖的
49             // this.do_action({    
50             //     name: 'User Info',
51             //     type: 'ir.actions.act_window',
52             //     res_model: 'res.users',
53             //     view_mode: 'form',
54             //     view_type: 'form',
55             //     views:[false, 'form'],
56             //     target: 'current',
57             //     res_id: ev.detail.user_id,
58             //     flags: {'form': {'action_buttons': true, 'options': {'mode': 'edit'}}},
59             //     context: {}
60             // });
61         }
62     }
63 
64     fieldRegistry.add('company_users', FieldCompanyUser);
65 
66     return {
67         FieldCompanyUser: FieldCompanyUser,
68     };
69 });

以下是template代碼

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <templates>
 3     <t t-name="UserCardInfo" owl="1">
 4         <div class="card mt16" >
 5             <div class="card-body" t-on-click="CardClicked" >
 6                 <h5 class="card-title mt8">
 7                     <t t-esc="props.user_name"/>
 8                 </h5>
 9             </div>
10         </div>
11     </t>
12 
13     <div t-name="OWLFieldCompanyUsers" owl="1" t-on-card-clicked="UserCardClicked">
14         <div class="row ml16 mr16" >
15             <t t-foreach="data_users" t-as="itemUser">
16                 <UserCardInfo user_name="itemUser['name']" user_id="itemUser['id']" active='false'/>
17             </t>
18         </div>
19     </div>
20 
21 </templates>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM