Nacho Salvador
Nacho Salvador

Reputation: 92

OpenERP. Add image into field dynamically in a tree view

I am using v7 and I want to show in a tree view a field with image icons (like a semaphore) depending of other field values in the same row.

Actually, I get the functionality I want with a function field and put the result as string but I really want it as an image. I don't know if is possible to return HTML from the function so I decided to do that with jQuery.

I implemented the jQuery code using the browser console and works but when I put the jQuery code in the view the "data-field" selector does not selected.

Please, can anyone explain me why or tell me another way to get my objective?

Upvotes: 2

Views: 1822

Answers (1)

Lists

The root element of list views is tree. The list view's root can have the following attributes:

editable, default_order, colors, fonts, create, edit, delete, on_write, string

See more about ListView

You can achieve this by defining child elements button in list view. I have added icon in product to show whether product is available / hold / sold based on product's status.

  • button

    icon: icon to use to display the button

Xml code to display icon in listview.

<xpath expr="//notebook/page[@string='Order Lines']/field[@name='order_line']/tree[@string='Sales Order Lines']/field[@name='product_id']" position="before">
      <field name="product_status" invisible="1" />
      <button icon='Hold' readonly="1" attrs="{'invisible':[('product_status', '!=', 'hold')]}"/>
      <button icon='Available' readonly="1" attrs="{'invisible':[('product_status', '!=', 'available')]}"/>
      <button icon='sold' readonly="1" attrs="{'invisible':[('product_status', '!=', 'sold')]}"/>
</xpath>

NOTE

  • Remember base field on which you defined button's visibility must be present on listview, doesn't matter whether it's visible or not but it must be there, like product_status in above example.
  • base field must be store=True.

    Why store=True ?

Reason: when you set any function field store=True then it will physically created in database, while you give this field name in domain the odoo framework add this field directly in WHERE clause, it will not access through browsable object, so if your field is store=False then it won't be able to find that field and gives you an error.

  • icons must be present in web/static/src/img/icons.
  • if you want to keep icons in your custom module then you have to create the same hierarchy for that in side your module folder create /static/src/img/icons and keep all the icons there.

xpath is used to define at which location you want to add/update fields.

See more about xpath

Upvotes: 3

Related Questions