我们可以通过设置状态字段来在同一个界面实现多步向导
视图文件:
your_wizard.xml
<record model="ir.ui.view" id="wizard_with_step_form">
<field name="name">wizard_with_step.form</field>
<field name="model">wizard_with_step</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Wizard with step" version="7.0">
<field invisible="1" name="state" />
<group states="step1">
<field name="name1" />
</group>
<group states="step2">
<field name="name2" />
</group>
<footer states="step1">
<button name="action_next" string="Next" type="object" />
</footer>
<footer states="step2">
<button name="action_previous" string="Previous" type="object" />
</footer>
</form>
</field>
</record>
模型文件:
your_wizard.py
class wizard_with_step(osv.osv_memory):
_name = 'wizard_with_step'
_description = 'Wizard with step'
name1 = fields.Char(string="Name 1")
name2 = fields.Char(string="Name 2")
state = fields.Selection([('step1', 'step1'),('step2', 'step2')], string="state")
def action_next(self, cr, uid, ids, context=None):
#your treatment to click button next
#...
# update state to step2
self.write({'state': 'step2',})
#return view
return {
'type': 'ir.actions.act_window',
'res_model': 'your_wizard',
'view_mode': 'form',
'view_type': 'form',
'res_id': this.id,
'views': [(False, 'form')],
'target': 'new',
}
def action_previous(self, cr, uid, ids, context=None):
#your treatment to click button previous
#...
# update state to step1
self.write({'state': 'step1',})
#return view
return {
'type': 'ir.actions.act_window',
'res_model': 'your_wizard',
'view_mode': 'form',
'view_type': 'form',
'res_id': this.id,
'views': [(False, 'form')],
'target': 'new',
}
}
----------------
下面是另一种更简单的方法。
视图文件:
In your wizard.xml:
<record model="ir.ui.view" id="wizard_with_step_form">
<field name="name">wizard_with_step.form</field>
<field name="model">wizard_with_step</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Wizard with step" version="7.0">
<field invisible="1" name="state"/>
<group states="step1">
<field name="name1"/>
</group>
<group states="step2">
<field name="name2"/>
</group>
<footer states="step1">
<button name="action_step2" string="Next" type="object"/>
</footer>
<footer states="step2">
<button name="action_step1" string="Previous" type="object"/>
</footer>
</form>
</field>
</record>
模型文件:
name1 = fields.Char(string="Name 1")
name2 = fields.Char(string="Name 2")
state = fields.Selection(
selection=[
('step1', 'Step 1'),
('step2', 'Step 2'),
],
string="Current step",
default="step1",
readonly=True
)
@api.multi
def action_step1(self):
self.state = 'step1'
return {"type": "set_scrollTop"}
@api.multi
def action_step2(self):
self.state = 'step2'
return {"type": "set_scrollTop"}
-----
说明:
这种返回类型比“无动作”更好,因为它不会发出警告,并且确保用户被滚动回顶部。