1

hi i have been searching and i cant find any tutorial about how to add a button in my view_form part of a custom module. i wanted to add a button and make it call a method i made every time it is clicked.

in xml view form :

<label name="fieldstring"/>
<field name="fieldstring"/>
 <button name="dosomething"/>

code:

def dosomething(cls,records):
    #treatement

is there any example module that is using a button associated to a treatment??

1 Answers1

2

In order to add a button to a view you have to make 3 steps:

Add the button to the _buttons dictionary of ModelView class. Normally this is done in the setup method of your class. Here you can define the icon and the states (when the button is invisible for example). If nothing needed you can define it with an empty dictionary.

For example:

@classmethod 
def __setup__(cls):
   super(Class, cls).__setup__()    
   cls._buttons.update({
          'mybutton': {},
          })

More complex examples can be found on tryton modules, for example:

http://hg.tryton.org/modules/account_invoice/file/84a41902ff5d/invoice.py#l224

Declare your method and decorate it with ModelView.button (in order to check access right to this button). For example:

@classmethod
@ModelView.button
def mybutton(cls, records)
    #DO whatever you want with records

NOTE that the name method must be the one that you use as key of the _buttons dictionary in step1.

And finally add it to the view. You can find all the attributes that can be used on:

http://doc.tryton.org/3.2/trytond/doc/topics/views/index.html?highlight=button#button

Note that the string and name attributes are mandatory.

Also the name must be the name of the method to call, defined in step 2.

You can find some examples in:

http://hg.tryton.org/modules/account_invoice/file/84a41902ff5d/view/invoice_form.xml#l51

pokoli
  • 1,045
  • 7
  • 15