Navigation

    Odoo 中文社区

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Popular
    • Users
    • Groups

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

    由于系统升迁的原因,本论坛部分较早期的内容存在格式和链接损坏失效的问题,并非本论坛系统本身的缺陷,望谅解

    本社区没有维护任何QQ群讨论组,任何与本社区同名的QQ群讨论组的言论与本社区无关!

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

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

    odoo中怎么把数字转化为百分比显示?

    Odoo 新手求助
    4
    17
    5372
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 鲍
      鲍永道 last edited by

      在odoo的一个列表中,想显示出百分比,比如说20%等这样的数字,但填写的时候肯定是直接填写数字的,然后在自动转化为百分比显示,要怎么做?有没有对应的widget,或是需要自己另外定义方法,还是odoo有处理方式。

      Felix_Cheng 1 Reply Last reply Reply Quote 0
      • Felix_Cheng
        Felix_Cheng @鲍永道 last edited by

        @鲍永道
        填写时为20,在后台做转换20/100.0。
        %以如下形式显示与视图:
        <field name="your_field_name" class="oe_inline" />%%

        鲍 1 Reply Last reply Reply Quote 0
        • 鲍
          鲍永道 last edited by

          谢谢你,我先试下。目前做转换我还不知道怎么做,我试下。再次感谢。

          1 Reply Last reply Reply Quote 0
          • 鲍
            鲍永道 @Felix_Cheng last edited by

            @felix_cheng 可以自定义一个widget吗,这样就很方便使用,不用每个里面都转化一下,直接使用widget就行了。

            S 1 Reply Last reply Reply Quote 0
            • S
              Siyuan @鲍永道 last edited by

              @鲍永道 wiget 的话要注意tree 和 form 的widget 分别在不同的位置

              鲍 2 Replies Last reply Reply Quote 0
              • 鲍
                鲍永道 last edited by

                在.py里面除以100,然后在field里面怎么写的?我没看明白?能具体一点吗?

                1 Reply Last reply Reply Quote 0
                • 鲍
                  鲍永道 @Siyuan last edited by

                  @siyuan 你看代码: @api.depends('material_composition_id')
                  def change_precent(self):
                  precent = 0
                  for r in self.material_composition_id:
                  if(r.percentage_composition):
                  r.percentage_composition = r.percentage_composition/100

                  在field里面直接引用一样样式就行吗?

                  1 Reply Last reply Reply Quote 0
                  • 鲍
                    鲍永道 @Siyuan last edited by

                    @siyuan 应该是用@api.onchange吧。

                    1 Reply Last reply Reply Quote 0
                    • Joshua
                      Joshua 管理员 last edited by

                      @鲍永道

                      找到个第三方的模块可以尝试下
                      https://www.odoo.com/apps/modules/9.0/percentage_widget/

                      S 鲍 3 Replies Last reply Reply Quote 0
                      • S
                        Siyuan @Joshua last edited by

                        @joshua 看了下代码,widget的话应该就是这个思路了,做的再好点,可以将%通过option 传入,

                        这样不止可以显示 百分比,还可以显示例如 1.2元之类的,

                        相当于 (数值)(单位)

                        鲍 2 Replies Last reply Reply Quote 0
                        • 鲍
                          鲍永道 @Siyuan last edited by

                          @siyuan 能否具体把这种widget写出来,大家共享。。。

                          1 Reply Last reply Reply Quote 0
                          • 鲍
                            鲍永道 @Siyuan last edited by

                            @siyuan 我原本的思路就是这样的,但自己是刚接触odoo,写不出widget。

                            1 Reply Last reply Reply Quote 0
                            • 鲍
                              鲍永道 @Joshua last edited by

                              @joshua 感谢,我去安装下这个模块,试试看行不行。非常感谢。

                              1 Reply Last reply Reply Quote 0
                              • 鲍
                                鲍永道 @Joshua last edited by

                                @joshua 你好,我在odoo10里面使用了,但填入线束后直接的转为了0.00%,不知道为什么,我看了源码也没有错啊。0_1501133011895_ab47d2a5-5c47-4620-85d6-05aa3cc561c4-image.png
                                0_1501133045177_ea412b04-d0e4-4874-8d83-17591c82ef58-image.png
                                图片的代码截图和效果截图。
                                想请教下是什么原因。

                                1 Reply Last reply Reply Quote 0
                                • S
                                  Siyuan last edited by

                                  @鲍永道

                                  // Form View
                                  var PercentageWidget = FieldFloat.extend({
                                  	template : 'PercentageWidget',
                                  
                                  	render_value : function() {
                                  		if (!this.get("effective_readonly")) {
                                  			this._super();
                                  		} else {
                                  			var _value = parseFloat(this.get('value'));
                                  			if (isNaN(_value)) {
                                  				this.$el.find(".percentage_filed").text('');
                                  			} else {
                                  				this.$el.find(".percentage_filed").text(
                                  						(_value * 100).toFixed(2) + ' %');
                                  			}
                                  		}
                                  	}
                                  });
                                  

                                  在编辑状态下调用:this._super();的时候,模块自己写的xml没有加载到对应的this.$inpout变量里,

                                  应该是9 和 10 的js 代码加载顺序有不同,

                                  具体的要花时间看了。

                                  鲍 1 Reply Last reply Reply Quote 0
                                  • 鲍
                                    鲍永道 last edited by

                                    恩恩,非常感谢。目前暂时的处理方式是直接使用小数来表示的,具体看两个版本的代码,目前我能力有限,应该不会具体去研究了。最后还是非常感谢你的解答,至少知道问题出在哪里了。

                                    1 Reply Last reply Reply Quote 0
                                    • 鲍
                                      鲍永道 @Siyuan last edited by

                                      @siyuan 可以重新封装一个类似的widget吗?因为在odoo项目开发中,百分比还是必不可少的。

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post