Xpath 增加 on_change 响应事件 分享及求指点
- 
为某些字段增加on_change 响应事件 ,可以使用xpath 修改属性:修改属性能够实现的功能, 
 position="attributes"<br />                    <xpath expr="//field[@name='name']" position="attributes"><br />                        <attribute name="required">1</attribute><br />                    </xpath><br />
 本例为 discount 增加 响应事件,需求来自于实际折扣为具体金额,而系统中计算的全是百分比的discount,需要提供一个具体金额的输入点,折算为 百分比,同时,输入百分比 具体折算金额也应该显示。 则新增字段discount_value<br />  <xpath expr="//field[@name='discount']" position="attributes"><br />                        <attribute name="on_change"><br />                        discount_change(product_uom_qty,price_unit,discount,context)<br />                        </attribute><br />                </xpath><br />
 py 对应代码 其中 'discount_value' 为新增加字段,<br /> def discount_change(self, cr, uid, ids, product_uom_qty=1,price_unit=0,discount=0,context=None):<br /> <br />        value = {<br />                    'discount_value':0,<br />            }<br />        if product_uom_qty * price_unit > 0:<br />          try:<br />                value.update({<br />                'discount_value':  ( product_uom_qty * price_unit)*discount*0.01<br />              <br />                })<br />            except ZeroDivisionError:  # 这个地方没删掉,,,copy 过来的 <br />                    pass<br />        return {'value': value}<br /><br />
 discount_value界面代码 其中<label > 都没起效<br /><xpath expr="//div[@name='discount']" position="after"><br />              <br />                <label for="discount_value123" /><br />              <br />                <div name="discount_value" groups="sale.group_discount_per_so_line"><br />                    <field name="discount_value" string ="discount_value1"<br />                    on_change="discount_value_change(product_uom_qty,price_unit,discount_value,context)"<br />                    /><br />                    <label for="discount_value1235" /><br />                </div><br />                <label for="discount_value1243" /><br />            </xpath><br />
 [img [检测到链接无效,已移除] /img]
 加上<br />              <xpath expr="//label[@for='discount']" position="after"><br />              <br />                <label for="discount_value123" /><br />              <br />              <br />            </xpath>
 排成。。。。。。
 [img [检测到链接无效,已移除] /img]
 用了一段超长的xpath 解决问题<br /><br /> <xpath expr="//notebook/page[@string='Order Lines']/field[@name='order_line']/form[@string='Sales Order Lines']/group/group"<br />                position="inside"><br />              <br />                    <field name="discount_value" <br />                    on_change="discount_value_change(product_uom_qty,price_unit,discount_value,context)"<br />                    /><br />              </xpath><br />
 顺便 发个 XPath 语法链接 http://www.w3school.com.cn/xpath/xpath_syntax.asp br />openerp学习笔记 视图继承(tree、form、search)http://www.cnblogs.com/cnshen/p/3164275.html
 界面问题解决了,实际测试时发现,discount 和discount_value 字段都设置了on_change 响应,而且都是改对方的值,杯具了,修改discount或discount_value 时 不停的执行。。。。on_change 死循环了
 [img [检测到链接无效,已移除] /img]
 求 指点[img2 [检测到链接无效,已移除] /img2]
- 
需要再加个判断来实现不要重新返回数值。 <br /><xpath expr="//field[@name='discount']" position="attributes"><br />                        <attribute name="on_change"><br />                        discount_change(product_uom_qty,price_unit,discount,discount_value,context)<br />                        </attribute><br />                </xpath><br /><br />def discount_change(self, cr, uid, ids, product_uom_qty=1,price_unit=0,discount=0,context=None):<br />    if discount_value == ( product_uom_qty * price_unit)*discount*0.01:<br />        return {}<br />...<br />
 同理 discount_value_change 也要。
- 
3Q 后来这么干了一下,解决了。 <br />if  abs(newdiscount-discount)>0.001:        <br />            return {'value': value}  <br />        else:<br />            return {'value': {}}    <br /><br />

