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

[Solved] Format number in grid ClientTemplate

2 Answers 611 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michiel
Top achievements
Rank 1
Michiel asked on 23 Dec 2010, 11:39 AM
I'm trying to format a whole number (like 1234) in a MVC Grid so that it looks like 1,234
If I use 
.Format("{0:n}")
then it becomes 1,234.00
.Format("{0:n2}")
Is not supported (using Ajax binding).
I can get it to work with
.Format("{0:###,###}")

However I use an extension to determine if the value is null. If so, we show N/A, if not, we use the format specified to display the value.

public static GridBoundColumnBuilder<T> DefaultToNA<T>(this GridBoundColumnBuilder<T> columnHelper) where T : class
        {
            const string defaultValue = "N/A";
            var format = columnHelper.Column.Format ?? "";
            if (!String.IsNullOrEmpty(format.Trim()))
            {
                columnHelper.ClientTemplate(string.Format("{0}", "<# if(" + columnHelper.Column.Member + " + ''.length == 0) { #>" + defaultValue + "<#;} else { #> <#= $.telerik.formatString('" + format + "', " + columnHelper.Column.Member + ") #> <#}#>"));
            }
            else
                columnHelper.ClientTemplate(string.Format("{0}", "<# if(" + columnHelper.Column.Member + " + ''.length == 0) { #>" + defaultValue + "<#;} else { #> <#=" + columnHelper.Column.Member + "#> <#}#>"));
             
            return columnHelper;
        }

When I use the format ({0:###,###}) in combination with this method extension it fails with a javascript error (Illegal Character).

The same happens when I do this:

columns.Bound(o => o.CSO).ClientTemplate("<#= $.telerik.formatString('{0:###,###}', CSO) #>")

If I replace the # with 0 it works. So it looks like it cannot handle the # character. Might be because it is an escape character for Telerik? Any way to override this or another solution to use the # sign in an ClientTemplate?

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 Dec 2010, 11:48 AM
Hello Michiel,

 Indeed the # character is special. I suggest a simple workaround - use a helper  JavaScript function :

<script>
       function formatter(value) {
        return    $.telerik.formatString('{0:###,###}', value)
       }
</script>


columns.Bound(o => o.CSO).ClientTemplate("<#= formatter(CSO) #>")

Greetings,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Michiel
Top achievements
Rank 1
answered on 23 Dec 2010, 12:01 PM
Good tip. Thanks!

I convert the # to another character in my method extension and use a javascript function to convert it back to # before passing it to the Telerik format function. I need the ability to pass formatting rules to the function.

function formatGridValue(format, value) {
 
     format = format.replaceAll('^', '#');
 
    return  $.telerik.formatString(format, value)
}

Would be nice if we can use an escape character in the client template to specify a # character to the parser ignores this.

Thanks again for your quick response.
Tags
Grid
Asked by
Michiel
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Michiel
Top achievements
Rank 1
Share this question
or