如何在点击按钮时打开新表单?

0 投票
2 回答
1737 浏览
提问于 2025-04-18 03:19

我在我的界面上创建了一个按钮,但我不知道点击它后怎么打开一个表单,这个表单里有一个文本框和两个按钮,分别是取消和确定。因为我需要在文本框里输入调用复制函数的次数,以便复制对象。

    `<record id="immo_personne_form" model="ir.ui.view">
        <field name="name">immo.personne.form</field>
        <field name="model">immo.personne</field>

        <field name="arch" type="xml">
            <form string="personne" >

                      <button string="Copy" type="object" name="copy_data"/>

                <field name="Pid_ftravail" />
                <field name="id_localisation" />
                <newline/>
                <field name="matricule" />
                <newline/>
                <field name="name" />
                <field name="prenom" />
                <field name="fonction" />


            </form>
        </field>
    </record>`
            <record id="immo_personne_tree" model="ir.ui.view">
        <field name="name">immo.personne.tree</field>
        <field name="model">immo.personne</field>
        <field name="arch" type="xml">
            <tree string="personnes">
            <field name="Pid_ftravail" />
            <field name="id_localisation" />
            <field name="matricule" />
                <field name="name" />
                <field name="prenom" />
                <field name="fonction" />

            </tree>
        </field>
    </record>  


        <record id="immo_personne_form_act" model="ir.actions.act_window">


        <field name="name">Personne</field>
        <field name="res_model">immo.personne</field>
        <field name="view_mode">tree,form</field>
        <field name="type">ir.actions.act_window</field>
        <field name="view_type">form</field>
        <field name="view_id" ref="immo_personne_tree"/> 
        <field name="help" type="html">
  </field>
    </record>

这是我的类和我的函数 copy_data。

class immo_personne(osv.osv):
_name = "immo.personne"
_description = "personne" 

def copy_data(self, cr, uid, id, default=None, context=None):
   if default is None:
     default = {}
   res = 1
   idea = self.browse(cr, uid, id)
   res += int(idea.matricule)
   default['matricule'] = res
   return super(immo_personne, self).copy_data(
      cr, uid, id, default, context)    

_columns = {
            'matricule':fields.integer('Matricule',size=255,required=True),
            'name':fields.char('Nom',size=255),
            'prenom':fields.char('Prenom',size=255),
            'fonction':fields.many2one('immo.fonction', 'Fonction'),
            'Pid_ftravail' : fields.many2one('immo.ftravail' ,'Formation de travail'),

            'id_localisation':fields.many2one('immo.localisation','Localisation',domain="   [('id_ftravail','=',Pid_ftravail)]"),
            }

immo_personne() 

我只是想知道怎么打开这个带有文本框和按钮的新表单,有人知道怎么做吗?

2 个回答

0

在OpenERP中,表单是通过一种叫做 act_window 的动作打开的。在你的情况下,你需要在你的对象 immo.personne 里面创建一个方法 copy(...),这个方法会返回一个这样的动作(以字典的形式)。

假设你新建的表单里有一个文本框和两个按钮,这个表单叫做 view_copy_multiple。你可以试试下面的代码:

def copy(self, cr, uid, ids, context=None):
       if not ids: return False
       if context is None: context = {}
       model_data_pool = self.pool.get('ir.model.data')
       view = model_data_pool.get_object(cr, uid,
                                         'immo_personne',
                                         'view_copy_multiple',
                                         context=context)
       return {
           'name': _("Copy multiple records"),
           'view_mode': 'form',
           'view_id': [view.id],
           'view_type': 'form',
           'res_model': 'immo.personne',
           'type': 'ir.actions.act_window',
           'nodestroy': True,
           'target': 'new',
           'context': context,
           'res_id': ids,
       }
1

如果你想在点击按钮时打开一个新表单,可以把按钮的类型改成 action,然后在按钮的名字里加上你的 action_id。下面是一个例子:

        <record id="model_action_id" model="ir.actions.act_window">
            <field name="name">Personne</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">immo.personne</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
        </record>

        <button string="%(model_action_id)d" type="action" name="copy"/>

如果你不想改变按钮的类型,那么你的按钮方法应该返回一个动作。比如:

def copy(self, cr, uid, ids, context):
    '''
    your code
    '''
    return {
       'name': _("personne"),
       'view_mode': 'form,tree',
       'view_type': 'form',
       'res_model': 'immo.personne',
       'type': 'ir.actions.act_window',
       'nodestroy': True,
       'target': 'new',
       'context': context,
   }

这样可以帮助你理解按钮的动作。

撰写回答