Manage kanban stages
The kanban view in Odoo provides the simple and easiest way to use the workflows. It's a kind of view that organizes the records in columns with each stage. Under each stage, the records will be listed; thus, it’s possible to handle the record between these stages quickly.
Let us see how these stages and the kanban view are created;
Similar to the definition of form view and tree view, we have to mention the view type of kanban view in action. This can be added as follows;
<record id="action_mobile_service" model="ir.actions.act_window">
<field name="name">Mobile Service</field>
<field name="res_model">mobile.service</field>
<field name="view_mode">tree,kanban,form</field>
<field name="help" type="html">
<p class="o_view_nocontent_smiling_face">
Add a new analytic account
</p>
</field>
</record>
Here the view_mode includes the tree, form and kanban views. Now the view definition will be like this;
<record id="service_request_view_kanban" model="ir.ui.view">
<field name="name">mobile.service.kanban</field>
<field name="model">mobile.service</field>
<field name="arch" type="xml">
<kanban default_group_by="service_state">
<field name="service_state"/>
<templates>
<t t-name="kanban-box">
<div class="oe_kanban_global_click">
<div class="oe_kanban_content">
<div class="oe_kanban_card">
<div>
<b>
<field name="name"/>
</b>
</div>
<div class="text-muted">
<field name="partner_id" widget="res_partner_many2one"/>
</div>
</div>
</div>
</div>
</t>
</templates>
</kanban>
</field>
</record>
This is a basic kanban view definition. Apart from this, more classes and attributes are available to enhance it.
As mentioned above, the kanban view contains records in a card along with their states. Kanban states are really useful, and the records of each state will be draggable. For showing the states in the kanban view, we have to use the attribute ‘group_expand.’ By using this attribute on the field declaration, the kanban view will display the states even though no records are available on the state. So let’s set how it’s defined;
service_state = fields.Selection([('draft', 'Draft'), ('assigned', 'Assigned'),
('completed', 'Completed'), ('returned', 'Returned'),
('not_solved', 'Not solved')],
string='Service Status', group_expand='_group_expand_states',
default='draft', track_visibility='always')
As you can see, the attribute group_expand contains a function named ‘_group_expand_states.’ It’s defined as follows;
def _group_expand_states(self, states, domain, order):
return [key for key, val in type(self).service_state.selection]
This function returns a list of states; thus, here, the function tries to pass all the selection fields in the list form to the method ‘group_expand.’
In the XML, the state field is defined as a normal field definition; here, we have defined it in the above example.
After following these things, the kanban view and its states will be as given below.
Progress bar in kanban view
Progress bar is another feature of the kanban view. Sometimes it is difficult to understand the states if the records are huge in number. Here the progress bar will help show each state's status on top of it. Let’s check how it’s defined;
Here, as an example, the progress bar is based on the field priority;
priority = fields.Selection([('0', 'Normal'),('1', 'Low'),('2', 'High'),('3', 'Very High')], string="Priority")
Three attributes are there used in the progress bar they are field, colors and sum_field
Field: The progress bar is shown based on the value of this field.
Colors: Mainly, there are four colors to assign to the values of the field. They are a success, warning, danger, and info.
Sum_field: This is an optional field for displaying the sum of any field in the record.
The progress bar is defined in the XML using the tag <progressbar>.
<progressbar field="priority" colors='{"3": "success", "2": "warning", "1": "danger", "0": "info"}'/>
Once the module is upgraded after adding the progress bar the kanban view will look like this;
While placing the mouse over the progress bar you can see the records belong to this field and the total number of the records will be displayed right to the column. This is by default, and if you want to show the sum of a particular field in a record, then it can be achieved by the attribute ‘sum_field’, but this is optional.