扩展视图之xpath用法

在视图扩展中,需要定位扩展字段需要显示的位置,通过xpath来实现定位

odoo 视图函数 在整个项目文件中,结构并不是十分明显,虽然它也遵循MVC设计,类比django的MTV模式,各个模块区分的十分明显,在Odoo中,视图的概念不是特别明显,很多时候,我们会将调用模型的函数直接写在models里边(即:类中)。

而对于模版Tempelate部分,odoo里边反而称做“views”,如下是odoo典型模块,销售模块sale的代码结构:

可以看到odoo的前端显示部分,对应的就是views,它是基于xml来实现的,而不是我们日常的html;

今天要说的xpath语法,就是针对于xml文件的一种语法格式。

通过xpath语法,可以对xml文件中要显示的内容,进行自定义的显示,比如:我们要插入一个新的标签在当前已经存在的标签的后边,前边,里边甚至替换。。。

这种语法看着还有点像JS的寻找父子节点。。。

1. xpath有一个用法,就是当你要修改的多个标签是在同一个目录节点下的时候,多个xpath标签可以写在同一个<record>标签中,而不用每次都去声明定义一遍,

支持的视图类型:form、tree、search ...支持的定位方法:

<notebook position="inside">       <xpath expr="//page[@name='page_history']" position="inside"> <field name="mobile" position="after"> <filter name="consumable" position="after"> <group name="bank" position="after"> <xpath expr="//field[@name='standard_price']" position="replace"> <xpath expr="//button[@name='open_ui']" position="replace"> <xpath expr="//div[@class='oe_employee_details']/h4/a" position="after"> <xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/tree/field[@name='analytic_account_id']" position="replace"> <xpath expr="/form/sheet/notebook/page/field[@name='line_ids']/form/group/field[@name='analytic_account_id']" position="replace">

支持的规则:before、after、replace、inside、attributes

插入:

position='before'
position="after"

      <field name="mobile" position="after">        <field name="sale_order_count"/>       </field> <filter name="consumable" position="after"> <separator/> <filter name="filter_to_qty_available" string="在手数量>0" icon="terp-accessories-archiver+" domain="[('qty_available', '>', 0)]"/> <filter name="filter_to_virtual_available" string="预测数量>0" icon="terp-accessories-archiver+" domain="[('virtual_available', '>', 0)]"/> </filter>

替换:
position="replace"

               <xpath expr="//field[@name='standard_price']" position="replace"> <group name='cost_prices' colspan="2" col="4"> <field name="standard_price" nolabel="1" attrs="{'readonly':[('cost_method','=','average')]}"/> <field name="cost_price_extra" groups="product.group_product_variant"/> </group> </xpath> <xpath expr="//button[@name='open_ui']" position="replace"> <button name="open_ui" type="object" string="Start Selling" attrs="{'invisible' : [('pos_state', 'not in', ('opened',))]}" class="oe_highlight" invisible="True"/> </xpath>

内部创建:position="inside

               <xpath expr="//page[@name='page_history']" position="inside"> <group name="grp_task" string="Tasks"> <field name="task_ids" colspan="4" nolabel="1"> <tree string="Tasks" editable="bottom"> <field name="name"/> <field name="user_id"/> <field name="date_deadline"/> <field name="state" invisible="1"/> <button name="do_open" states="pending,draft,done,cancelled" string="Start Task" type="object" icon="gtk-media-play" help="For changing to open state" invisible="context.get('set_visible',False)"/> <button name="action_close" states="draft,pending,open" string="Done" type="object" icon="terp-dialog-close" help="For changing to done state"/> </tree> </field> </group> </xpath> 

修改属性:修改属性能够实现的功能,不要使用 replace
position="attributes"
            

<xpath expr="//field[@name='name']" position="attributes"                        

<attribute name="required">1</attribute>

 </xpath1
        <record id="product_normal_variant_form_view" model="ir.ui.view"> <field name="name">product.normal.variant.form</field> <field name="model">product.product</field> <field name="type">form</field> <field name="inherit_id" ref="product.product_normal_form_view" /> <field name="arch" type="xml"> <data> <xpath expr="//field[@name='name']" position="attributes"> <attribute name="required">1</attribute> </xpath> <xpath expr="//field[@name='standard_price']" position="replace"> <group name='cost_prices' colspan="2" col="4"> <field name="standard_price" nolabel="1" attrs="{'readonly':[('cost_method','=','average')]}"/> <field name="cost_price_extra" groups="product.group_product_variant"/> </group> </xpath> <sheet> <group col="2" colspan="2" groups="base.group_extended" position="replace"> <group colspan="2" col="6" name="weight" groups="base.group_extended"> <field name="is_multi_variants" invisible="1"/> <group colspan="2" col="2"> <separator string="Template Weights" colspan

转载于:https://www.cnblogs.com/zcy1103/p/9667814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值