This is a migrated thread and some comments may be shown as answers.

Number Formatting

5 Answers 815 Views
Templates
This is a migrated thread and some comments may be shown as answers.
Ian
Top achievements
Rank 1
Ian asked on 02 Dec 2011, 07:06 PM
Hi,
We have 10 digit phone numbers coming from a database (as numerical types, not strings) and I want to format them in the kendo grid. I can't seem to find the right formatting string. Any ideas?
Thanks,
ian

5 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 05 Dec 2011, 06:10 PM
Hi Ian,

Could you please elaborate more on this? What is the require format ? 

Regards,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Ian
Top achievements
Rank 1
answered on 05 Dec 2011, 06:23 PM
Hi,
Thanks for your interest. As an example, my viewmodel is returned from the server and has a phone field with a value 5553242345. I would like to bind this to a text input such that it is masked (555) 324-2345, and bind it to a label with the same format. In the past we have used a custom binding in knockout.js to perform this. Ideally, it would be great for the binding to automatically translate the value of the input from the masked/raw format.

Thanks again,
ian
0
Accepted
Georgi Krustev
Telerik team
answered on 07 Dec 2011, 02:41 PM
Hello Ian,

 
In order to achieve your goal you will need to create a function, which will format the number into
"(###) ###-####" format. Once you have this function, you will be able to use it in the template to format the number:

function format(number) {
 //return formatted number;
}
 
var inlineTemplate = kendo.template("The number is #= format(number) #.");
var inlineData = { number: 1234567890 };
 
$("#inline").html(inlineTemplate(inlineData));

I am not expert in knockout.js, but I believe that you should create a value converter. Check this article for more information.

Regards,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Ian
Top achievements
Rank 1
answered on 07 Dec 2011, 02:57 PM
Thanks Georgi,
Yeah, this is actually what we ended up with. I was hoping for a bit of a cleaner syntax within our templates (where don't have to tote around a big list of functions from project to project simply for the equivalent of string.format function. I was hoping that kendo.toString(... function would support a custom formatter. 

Thanks,
ian
0
Gary
Top achievements
Rank 1
answered on 10 Apr 2012, 09:08 PM
Is it the case that any inline template that includes the '#' character needs to be externalized into a function. When I have an inline grid column template like:

'#= kendo.toString(number, "##,#") #'

I get an 'invalid template' error.
Tags
Templates
Asked by
Ian
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Ian
Top achievements
Rank 1
Gary
Top achievements
Rank 1
Share this question
or