Skip to Content
Menu
This question has been flagged
1 Reply
771 Views

Hi,


I'm new to Odoo and I want to develop a searchpanel in my app. I want to adjust the width of searchpanel in the view. How can I achieve that?

<searchpanel class="account_root w-auto">
<field name="account_root_id" icon="fa-filter" groupby="account_id" limit="0"/>
</searchpanel>


Avatar
Discard
Best Answer

To adjust the width of the searchpanel in Odoo views, you can achieve this by applying custom CSS to override the default styles. Unfortunately, the class attribute in the searchpanel tag is not sufficient for styling the width directly in the XML view. Below is a detailed guide on how to adjust the width of the searchpanel.

Steps to Adjust Search Panel Width

1. Add a Custom CSS File

Odoo's QWeb views are rendered dynamically, and the searchpanel element can be styled with custom CSS by targeting its structure.

  1. Create or Extend Assets for Your Module:
    • If you have a custom module, add or extend the web.assets_backend to include your custom CSS.
    Example:
    xmlCopy code<template id="assets_backend" inherit_id="web.assets_backend" name="Custom Search Panel Width">
        <xpath expr="." position="inside">
            <link rel="stylesheet" type="text/css" href="/your_module_name/static/src/css/custom_searchpanel.css"/>
        </xpath>
    </template>
    
  2. Create a CSS File:
    • Create a CSS file in your module directory:
      scssCopy codeyour_module_name/static/src/css/custom_searchpanel.css
      
  3. Add Custom Styles:
    • Target the searchpanel element and adjust its width:
      cssCopy code.o_search_panel {
          width: 300px !important; /* Adjust this value as needed */
      }
      

2. Target the Search Panel Dynamically

In Odoo's backend views, the search panel is wrapped with the o_search_panel class. Use this selector to apply styles globally or restrict the changes to specific views if needed.

Restrict Styling to Specific Views

If you only want to apply the styling in specific views:

  1. Add a unique class or ID to the container of the search panel in your view:
    xmlCopy code<searchpanel class="custom-search-panel">
        <field name="account_root_id" icon="fa-filter" groupby="account_id" limit="0"/>
    </searchpanel>
    
  2. Update the CSS to target the custom-search-panel class:
    cssCopy code.custom-search-panel .o_search_panel {
        width: 300px !important; /* Adjust width as needed */
    }
    

3. Adjust via JavaScript (Optional)

If dynamic adjustments based on conditions are needed, you can use JavaScript.

  1. Extend Search Panel Behavior: Create a JavaScript file in your module:
    scssCopy codeyour_module_name/static/src/js/custom_searchpanel.js
    
  2. Modify Width Dynamically: Use Odoo's JS framework to adjust the width dynamically:
    javascriptCopy code/** @odoo-module **/
    
    import { Component } from "@web/core/component";
    
    class CustomSearchPanel extends Component {
        mounted() {
            super.mounted();
            const searchPanel = this.el.querySelector(".o_search_panel");
            if (searchPanel) {
                searchPanel.style.width = "300px"; // Adjust width dynamically
            }
        }
    }
    
  3. Include JS in Assets: Extend the web.assets_backend to include your custom JavaScript:
    xmlCopy code<template id="assets_backend" inherit_id="web.assets_backend" name="Custom Search Panel JS">
        <xpath expr="." position="inside">
            <script type="module" src="/your_module_name/static/src/js/custom_searchpanel.js"></script>
        </xpath>
    </template>
    

4. Testing the Changes

  • Deploy the Module:
    • Restart Odoo and upgrade your module to apply the changes.
  • Verify the Layout:
    • Navigate to the view with the search panel and confirm the width adjustment.

Notes

  • Always test your CSS and JS changes in a staging environment before deploying to production.
  • If the search panel's width needs to be responsive, use relative units (%, vw) or media queries in CSS.

Avatar
Discard