Skip to Content
Menu
This question has been flagged
8 Replies
11314 Views

hi all

a created a custom type view, for example 'form2'

created action when i add this type view without 'form'

in this case when click on any record do nothing

if add 'form' type into action, opening record in form view

how i can open my custom type view 'form2' from tree or kanban when i click on any record?

Avatar
Discard
Best Answer

when you click on the list view record it triggers up 'open_record' and it bubbles up to an abstract controller.
it always open default from view as it's hardcoded. 
https://github.com/odoo/odoo/blob/12.0/addons/web/static/src/js/views/abstract_controller.js#L460
override you _onOpenRecord controller and switch to your view from(2)
and same for the kanban record

Avatar
Discard
Author

i created new file and included AbstractController, when check if in context available view_type:

src/custom_module/static/src/js/abstract_controller.js

odoo.define('custom_module.AbstractController', function (require) {

"use strict";

var AbstractController = require('web.AbstractController');

AbstractController.include({

_onOpenRecord: function (event, params) {

event.stopPropagation();

var record = this.model.get(event.data.id, {raw: true});

var view_type = 'form'

if (this.initialState.context.view_type) {

view_type = this.initialState.context.view_type

}

this.trigger_up('switch_view', {

view_type: view_type,

res_id: record.res_id,

mode: event.data.mode || 'readonly',

model: this.modelName,

});

},

});

});

and add context in my action when i need other type view instead form

src/custom_module/views/model_views.xml

<record id="action_view_id" model="ir.actions.act_window">

<field name="name">any name</field>

<field name="res_model">model</field>

<field name="context">{'view_type': 'form2'}</field>

<field name="view_ids"

eval="[(5, 0, 0),

(0, 0, {'view_mode': 'kanban'}),

(0, 0, {'view_mode': 'tree'}),

(0, 0, {'view_mode': 'form2', 'view_id': ref('custom_view_id_form2')}),

(0, 0, {'view_mode': 'calendar'}),

(0, 0, {'view_mode': 'pivot'}),

(0, 0, {'view_mode': 'graph'}),

(0, 0, {'view_mode': 'activity'})]"/>

</record>

and added a new js file to assets.xml

Best Answer

Hi Mark,

In the action, rather than just defining the view_mode, you should define view_ids. This allows you to specify the view to be used for each mode if you wish. Some can be defined while others can be blank, which will use the default.

<record id="action_res_company" model="ir.actions.act_window">
<field name="name">Company Test</field>
<field name="res_model">res.company</field>
<field name="view_mode">tree,form</field>
<field name="view_ids" eval="[(5, 0, 0),
(0, 0, {'view_mode': 'tree'}),
(0, 0, {'view_mode': 'form', 'view_id': ref('test.res_company_form2_view')})]"/>
</record>

Cheers
Jake Robinson

Avatar
Discard
Author

hi Jake, my view has type 'form2':

(0, 0, {'view_mode': 'form2', 'view_id': ref('test.res_company_form2_view')})]

if write

[(5, 0, 0),

(0, 0, {'view_mode': 'tree'}),

(0, 0, {'view_mode': 'form2', 'view_id': ref('test.res_company_form2_view')})]

from tree when click on any record - do nothing

Is there a reason for this? I don't think form2 is a valid view mode and I've never seen it used before. Have you tried changing it just to form?

Author

i using created custom type view because the customer does not fit the current version form, it has other styles and there are no buttons to create and edit, if open this new custom form from any button that calling view, all correct working, but i need also open form2 from kanban and tree

Author

By the way, in your version you can not write this line

<field name="view_mode">tree,form</field>

because the next line

(5, 0, 0)

will delete all values ​​anyway

Sorry, I thought you meant you have just created a new view, I didn't realised you'd created a new type. You'll need to dig around the javascript, it will explicitly call the form view mode on click.

From a quick glance, clicking a kanban calls trigger_up('openRecord') which I think ends up calling odoo/addons/web/static/src/js/views/abstract_controller.js _onOpenRecord() but I can't be sure.

These are two separate fields and conventionally Odoo always defines both.

Author

okay, thank you, I dig in there in the morning.

i testing this with and without <field name="view_mode">tree,form</field>, and both variants are working.

if i correct understood this line are equal to

<field name="view_ids" eval="[(0, 0, {'view_mode': 'tree'}),

(0, 0, {'view_mode': 'form'})"/>

Related Posts Replies Views Activity
0
Jun 20
2806
1
Oct 18
7681
0
Feb 22
1633
1
Oct 19
5835
6
Aug 19
9749