Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

9.0社区版中开发自定义widget



  • 在FORM报表中添加饼状图的方法
    无锡特迪 tedi3231@qq.com QQ:85920312

    在ODOO9.0社区版中对JS的调用方式略有变化,简单介绍下如何通过JS自定义控件。

    创建目录
    在模块下创建"static/src/js/"目录用来存放JS文件,如果需要用到QWEB模板的话应该再建一个“static/src/view”,名称可自行定义。

    添加JS文件
    这里添加一个名为"widgets.js"的文件,具体9.0下的语法可参考官方文档(注意9.0下的那个和petstore有关的文档都没有更新,JS的语法有些变化.饼状图使用了Highcharts的JS报表,具体使用方法可参考文档.

    <br /> odoo.define(&#039;crm_cm.crm_cm&#039;, function (require) {<br />&quot;use strict&quot;;<br /><br />var core = require(&#039;web.core&#039;);<br />var form_common = require(&#039;web.form_common&#039;);<br />var formats = require(&#039;web.formats&#039;);<br />var Model = require(&#039;web.Model&#039;);<br /><br />var _t = core._t;<br />var QWeb = core.qweb;<br /><br />var BarChart = form_common.AbstractField.extend({<br />render_value:function(){<br />&nbsp; &nbsp; var self = this;<br />&nbsp; &nbsp; console.log(this.get(&quot;value&quot;));<br />&nbsp; &nbsp; //这里的value对于one2many字段来讲就是多个Id数组,如[1,5,3]<br />&nbsp; &nbsp; var line_ids = this.get(&quot;value&quot;);<br />&nbsp; &nbsp; var mod = new Model(&quot;crm.cm.asset.allocation.result.line&quot;);<br /><br />&nbsp; &nbsp; mod.query([&quot;rule_name&quot;,&quot;rule_percent&quot;,&quot;amount&quot;])<br />&nbsp; &nbsp; &nbsp; &nbsp; .filter([&#91;&#039;id&#039;,&#039;in&#039;,line_ids]])<br />&nbsp; &nbsp; &nbsp; &nbsp; .all().then(function(lines){<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(lines);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = lines.map(function(item,index){return [item.rule_name,item.amount];});<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.$el.highcharts({<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chart: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: &#039;pie&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options3d: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled: true,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alpha: 45,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beta: 0<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: &#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tooltip: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pointFormat: &#039;{series.name}{point.amount}: &lt;b&gt;{point.percentage:.1f}%&lt;/b&gt;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plotOptions: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pie: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allowPointSelect: true,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor: &#039;pointer&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; depth: 35,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataLabels: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled: true,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; format: &#039;{point.name}-{point.percentage:.1f}%&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; series: [{<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: &#039;pie&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#039;占比&#039;,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: data<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />&nbsp; &nbsp; &nbsp; &nbsp; });<br />&nbsp; &nbsp; }<br />});<br /><br />//这句话一定要有,要不然找不到这个控件的<br />core.form_widget_registry.add(&quot;barchart&quot;,BarChart);<br />});<br /><br />
    



    将JS文件在头部进行引用
    在模块的views文件中添加一个assets.xml文件,内容如下:

    <br /> &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br />&lt;openerp&gt;<br />&nbsp; &nbsp; &lt;data&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;template id=&quot;assets_backend&quot; name=&quot;crm_cm_assets2&quot; inherit_id=&quot;web.assets_backend&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;xpath expr=&quot;.&quot; position=&quot;inside&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;/crm_cm/static/src/js/widgets.js&quot;&gt;&lt;/script&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;script src=&quot;http://cdn.hcharts.cn/highcharts/highcharts.js&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp;  &lt;/xpath&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;/template&gt;<br />&nbsp; &nbsp; &lt;/data&gt;<br />&lt;/openerp&gt;<br /><br />在openerp.py的data中添加涉及到的文件<br /><br /> &#039;data&#039;: [<br />&nbsp; &nbsp; #其他视图省略<br />&nbsp; &nbsp; &quot;views/assets.xml&quot;,<br />&nbsp; &nbsp; #&quot;report/gold_report.xml&quot;,<br />],<br />
    


    在FORM中使用饼状图控件
    在FORM视图中使用上面的饼图控件,将fields的widget属性设为"barchart"就可以了,我目前是用在one2many字段上的。代码如下:

    <br />&nbsp; &nbsp;  &lt;group string=&quot;Asset Allocation Result Line&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;result_lines_2&quot; nolabel=&quot;1&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &lt;tree string=&quot;Asset Allocation Result Line&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;rule_id&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;rule_percent&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;amount&quot;/&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/tree&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp;  &lt;/field&gt;<br />&nbsp; &nbsp; &lt;/group&gt;<br /><br />&nbsp; &nbsp; &lt;group string=&quot;Bar Chart&quot;&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;field name=&quot;result_lines&quot; widget=&quot;barchart&quot; nolabel=&quot;1&quot;/&gt;<br />&nbsp; &nbsp; &lt;/group&gt;<br />
    


    注意事项
    我目前只在one2many字段上测试过,而且引用了外部的highcharts,有其他朋友建议可以使用odoo中原有的饼图控件nvd3,有空研究下。另外 result_lines_2 字段是个冗余字段,只是用在同时展示列表和饼图时使用,因为测试过程中发现界面上对一个字段展示两次的时候有可能不显示,具体原因尚未研究出来。这样的用法应该也只能用在one2many字段上,代码如下:

    <br />result_lines = fields.One2many(&quot;crm.cm.asset.allocation.result.line&quot;,&quot;result_id&quot;,string=&quot;Asset Allocation Result Line&quot;)<br />result_lines_2 = fields.One2many(&quot;crm.cm.asset.allocation.result.line&quot;,&quot;result_id&quot;,string=&quot;Asset Allocation Result Line&quot;)<br />
    


  • 界面上对一个字段展示两次,前一个字段的值不显示



  • 的确是这个样子,以前都没注意过。 😉
    [quote author=Jeff link=topic=17658.msg32388#msg32388 date=1467122427]
    界面上对一个字段展示两次,前一个字段的值不显示
    [/quote]