jqGrid-格式比率字段为百分比值 [英] jqGrid - format ratio field as percent value

查看:248
本文介绍了jqGrid-格式比率字段为百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器代码将某些列作为比率值(0.0到1.0)进行传输.我需要格式化将其编辑为百分比.我想在JavaScript方面做到这一点,而无需修改服务器端.因此,如果我添加一个自定义格式器以将该值乘以100,则显示将按预期工作.而且,当我点击编辑"按钮时,内联编辑框也将值显示为百分比.当我保存时,麻烦就开始了-使用格式化程序再一次转换值,给我类似10000的东西.好吧,我在这里需要对称,因此我创建了一个仅将值除以100的取消格式化程序.但这也行不通-现在,编辑控件将其显示为不是我想要的比例(尽管现在可以正确保存).

My server code transfers some column as a ratio value, 0.0 to 1.0. I need to format and edit it as a percent. I would like to do it on JavaScript side, without modifying server-side. So if I add a custom formatter to just multiply the value by 100, display works as expected. Moreover, when I hit edit button, inline edit box also displays value as percentage. The trouble starts when I save - the value gets converted with formatter one more time, giving me things like 10000. So OK, I need symmetry here, so I create an unformatter which just divides value by 100. But that doesn't work too - now edit control displays it as a ratio which not what I wanted (although saving now works correctly).

在不更改服务器代码的情况下还有什么办法吗?

Is there any way around without changing the server code?

推荐答案

您可以跳过 unformatter 而改用 custom 编辑类型,只需在:

You can skip the unformatter in favour of custom edit type, just use this in your colModel:

{ ..., edittype: 'custom', editoptions: { custom_element: function(value, options) { return $('<input type="text" value="' + value + '"/>'); }, custom_value: function(element){ return parseFloat(element.val())/100; }  }}

这篇关于jqGrid-格式比率字段为百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆