使用复选框清除 TextField

Clear TextField with a checkbox

我想在用户取消选中复选框时清除文本字段中输入的值,我想知道这是否可以通过 XML Odoo 中的 XMl 或 Javascript 实现查看。

XMl代码

<field name="expend_percetage" attrs="{'readonly':[('state', 'in', ('done','cancel','sale'))],
                                                       'invisible':[('fixed_expend', '=', True)]}" />
                
                <field name="fixed_expend" attrs="{'readonly':[('state', 'in', ('done','cancel','sale'))],
                                                       'invisible':[('expend_percetage', '=', True)]}" />
                
                <field name="percent_amount" nolabel="1"
                attrs="{'invisible':['&amp;',('expend_percetage', '=', False),('fixed_expend', '=', False)],
                        'readonly': [('state', 'in', ('done','cancel','sale'))] }"/>

Python 验证

@api.constrains('percent_amount')
    def check_percent_amount(self):
        for record in self:
            if record.expend_percetage == True:
                if record.percent_amount > 100:
                    raise exceptions.ValidationError("Percentage of costs cannot be greater than 100%")
                elif record.percent_amount < 0:
                    raise exceptions.ValidationError("The percentage cannot be a negative value")
            elif record.fixed_expend == True:
                if record.percent_amount > record.total_revenue:
                    raise exceptions.ValidationError("The Fixed amount cannot be greater than the total revenue")
                elif record.percent_amount < 0:
                    raise exceptions.ValidationError("The Fixed amount cannot be a negative value")

我解决起来很简单。哈哈

#clear textbox
    @api.onchange('expend_percetage','fixed_expend')
    def clear_expend_amount(self):
        for rec in self:
            if rec.expend_percetage == False:
                rec.percent_amount = 0.00
            elif rec.fixed_expend == False:
                rec.percent_amount = 0.00

@api.constrains 更适合验证数据,但要清除前端的用户输入,您应该使用 @api.onchange.

@api.onchange('expend_percetage','fixed_expend')
def onchange_percent_amount(self):
    if not self.expend_percetage or not self.fixed_expend:
        self.percent_amount = 0