KendoNumericTextBox percentage formatting

馋奶兔 提交于 2019-12-10 17:42:28

问题


I want to achieve the following: a percentage value (represented in model e.g. by "0.7" for 70%) should be edited in a Kendo NumericTextBox. Normal behavior for "P" formats of the NumericTextBox is that when you edit the value it shows "0.7" and when viewing the value it shows "70%". Now in our scenario we want to show "70" when editing the value (instead of "0.7").

I found that this behavior is not supported by the Kendo NumericTextBox. Of course, I could use custom formatting, e.g. "format: '#.00 \%'" - but in this case the model value has to be "70" for 70% instead of "0.7"...

I found the following way to define a new "percentage" binding, which does the conversion correctly: http://boniestdeveloper.net/post/2013/04/16/Editing-percentage-values-with-a-KendoUI-NumericTextBox.aspx. Now I want to encapsulate this binding in a custom widget "kendoNumericPercentageTextBox()", which behaves the same as "kendoNumericTextBox()", but renders the "percentage" binding.

Can anybody give me a hint how I can define such a widget that renders the custom binding and extends the kendoNumericTextBox widget?


回答1:


I know it's an old question, but what you want to achieve is now possible out of the box with the factor option:

Specifies the factor by which the value is multiplied. The obtained result is used as edit value. So, if 15 as string is entered in the NumericTextBox and the factor value is set to 100 the visual value will be 1500. On blur the visual value will be divided by 100 thus scaling the widget value to the original proportion.

Example:

$("#numerictextbox").kendoNumericTextBox({
   format: "p0",
   factor: 100,
   min: 0,
   max: 1,
   step: 0.01, 
   change: function() {
     console.log(this.value());
   }
});

JSfiddle: https://jsfiddle.net/qs4jnLqs/3/



来源:https://stackoverflow.com/questions/30883554/kendonumerictextbox-percentage-formatting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!