How to Use the Widget Handle in Odoo

Abid Patel
25-Oct-2024 Updated : 25-Oct-2024

Learn how to implement the widget handle in Odoo to reorder records using drag-and-drop functionality, enhancing usability and data management.

How to Use the Widget Handle in Odoo

In Odoo, the widget handle is an essential feature that enables users to reorder items in a list view using drag-and-drop functionality. This is especially useful for setting custom sequences for records, such as priority lists or customized sorting orders in a form or tree view.

What is the Widget Handle in Odoo?

The widget handle is a UI feature that allows users to manually adjust the order of records in a list. This widget is displayed as a "handle" icon, which users can drag to change the position of list items. It works in combination with Odoo’s sequencing fields to control the order of records.

How to Add the Widget Handle in Odoo

To implement the widget handle, we need a few simple steps:

  • ▹ Define a sequence field in the model for ordering.
  • ▹ Use the handle widget in the list view where ordering is required.

Step 1: Define a Sequence Field in Your Model

First, add a field to your model that will hold the sequence order. Here’s an example in Python:

python
<from odoo import models, fields>

class Task(models.Model):
    _name = 'project.task'
    _order = 'sequence'  # Set default ordering by sequence field
    
    sequence = fields.Integer(string="Sequence", default=10)

Explanation:

  • sequence: This integer field holds the order value for each record, allowing us to reorder items.
  • _order: We specify sequence as the default ordering for the model to reflect changes from drag-and-drop actions.

Step 2: Use the Widget Handle in XML

In the list view, add the handle widget on the sequence field. This allows users to drag and reorder records directly in the view:

xml
<record id="view_task_tree" model="ir.ui.view">
    <field name="name">project.task.tree</field>
    <field name="model">project.task</field>
    <field name="arch" type="xml">
        <tree string="Tasks" editable="bottom">
            <field name="sequence" widget="handle"/>
            <field name="name"/>
            <field name="priority"/>
        </tree>
    </field>
</record>

Explanation:

  • sequence field with widget="handle": This enables the drag-and-drop functionality.
  • editable="bottom": Allows inline editing, letting you adjust the sequence within the list view.

How Widget Handle Enhances User Experience

The widget handle allows for easy, intuitive customization of data order without requiring users to change sequence numbers manually. By dragging records, users can rearrange items quickly, making it an ideal solution for managing priority lists, custom ordering of tasks, or any other sortable list of records.

Use Cases for Widget Handle

Here are some common use cases where the widget handle is beneficial:

  • ▹ Reordering tasks in a project to set priorities.
  • ▹ Sorting products in a sales order line or manufacturing order.
  • ▹ Arranging steps in a sequence for multi-step processes.

Conclusion

The widget handle in Odoo provides an efficient way to manage the order of records directly from the list view. With a simple configuration, users can adjust record positions using drag-and-drop functionality, improving both usability and the overall user experience in Odoo.

Make a Comment

Your email address will not be published. Required fields are marked *