jqGrid - 将比率字段格式化为百分比值 [英] jqGrid - format ratio field as percent value
问题描述
我的服务器代码将某些列作为比率值传输,0.0 到 1.0.我需要格式化并将其编辑为百分比.我想在 JavaScript 端做,而不修改服务器端.因此,如果我添加一个自定义格式化程序以将值乘以 100,则显示按预期工作.此外,当我点击编辑按钮时,内联编辑框也会以百分比显示值.当我保存时,麻烦就开始了——值被格式化器再转换一次,给了我 10000 之类的东西.好吧,我在这里需要对称,所以我创建了一个 unformatter,它只是将值除以 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 编辑类型,只需在您的 colModel
:
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屋!