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

[Solved] Formating error with Ajax binding

15 Answers 185 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.
Sean Cross
Top achievements
Rank 1
Sean Cross asked on 31 Mar 2010, 05:06 AM
I have the following grid definition:

            <%= Html.Telerik().Grid<IrpGoalVM>() 
                .DataBinding(dataBinding => dataBinding.Ajax().Select("IrpGoals", "AccData", new {Id = Model.Id } )) 
                .Name("GoalGrid") 
                .PrefixUrlParameters(true) 
                             
                .Columns(columns => 
                { 
                    columns.Bound(g => g.Id).Visible(false); ; 
                    columns.Bound(g => g.Type); 
                    columns.Bound(g => g.Goal); 
                    columns.Bound(g => g.GoalDate).Format("{0:dd MMM yyyy}").Title("Achieve by"); 
                    columns.Bound(g => g.Responsibility); 
                    columns.Bound(g => g.Completed).Format("{0:dd MMM yyyy}").Width(120); 
                    columns.Bound(g => g.EstCost).Format("{0:c}"); 
                }) 
                .Pageable() 
                .Sortable()                 
            %> 




This works fine with server side binding but not so well with ajax.  With server side, the null dates get formated as an empty string, and the EstCost gets formated as currency.  With ajax binding however null dates show as "null" and the EstCost has no formatting at all.

See attached.

How can I fix this?

Sean



15 Answers, 1 is accepted

Sort by
0
IQworks
Top achievements
Rank 1
answered on 31 Mar 2010, 07:22 AM

Hi Sean,

    Not sure if this would help, but  Here is what I do for a date ajax side ...

    

 

columns.Add(o => o.cDateUpdated).Format(

"{0:MM/dd/yyyy}").Width(85).Title((string)Resources.BCAResources.stdUpdated);

 

 

  If this helps, maybe if you look around for this Format date option, the currency won't be far behind ?

    good luck.

0
Sean Cross
Top achievements
Rank 1
answered on 31 Mar 2010, 09:31 PM
I am using a format string of {0:dd MMM yyyy}.  Changing it, or removing it doesn't solve the problem, nulls are still shown as null.
0
Alan MacDougall
Top achievements
Rank 1
answered on 01 Apr 2010, 12:23 AM
I got around my null date issues by adding
.ClientTemplate("<%#= NULLALBEDATECOLUMN #>".Replace("null", "")) 

to the end of my column binding.
0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 12:36 AM

Hi Sean,

    Sorry, thought I saw something else in your formating.

    Dont want to waste your time.

    I believe that there are ways to check for null inside your ViewModel so that if the date from the data base is null, then you can change it to whatever you need. I never had to do that.

    Another thought is RowAction. You might be able to initialize your date to a datetime if it is null, or format your date if it is null.

                /* After the data is bound to the Grid control, then it appears here.  
                 * This happens also when page is loaded & after a click.*/  
                 .RowAction(row =>    
                 {  // If the companyname.length>0, set row as selected.  
                     rowrow.Selected = row.DataItem.cCompanyName.Length>0;  
                 })     
 In this case, my cCompanyName is comming in from my ViewModel. If I can check it here, maybe YOU can CHANGE your date here.  This RowAction goes in the same area where your  .Pageable() and .Sortable() are (you may already know that - sorry).

  

 
0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 01:35 AM
Hi Alan, 

    If that dosnt work, its an excellent shot.  Is there a place that shows these types of properties or methods ?  I keep seeing code like this , but I dont know where to go to find it ? I often see people propose solutions with code I never saw before.

Thanks Alan 
0
Sean Cross
Top achievements
Rank 1
answered on 01 Apr 2010, 01:45 AM
Thanks.  That didn't work as it stands but it got me on the right track.  I am now using

columns.Bound(g => g.Completed).Width(120).ClientTemplate("<#= Completed == null ? '' : (Completed).format('dd MMM yyyy') #>"); 
 




Now I just need to figure out what to do with the currency field.

Sean
0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 02:05 AM
Hi Sean,
   Couldnt use the same statement and just change the formating to something for currency ?
columns.Bound(g => g.Completed).Width(120).ClientTemplate("<#= Completed == null ? '' : (Completed).format('$ 00.00') #>");  // Something like that ?    
    
 THAT OF COURSE WONT WORK - BUT YOU GET THE IDEA
 
 
 
  
0
Sean Cross
Top achievements
Rank 1
answered on 01 Apr 2010, 02:25 AM
I am getting closer:

columns.Bound(g => g.GoalDate).Title("EC").ClientTemplate("<#= (EstCost).format('C') #>"); 

That displays the number as "¤500.00".  Now I just need to replace "¤" with "$".

0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 03:02 AM
do you still have "g.GoalDate" in there ? or is that your $$ field name ?
0
Sean Cross
Top achievements
Rank 1
answered on 01 Apr 2010, 03:04 AM
Doh!  Should be:
columns.Bound(g => g.EstCost).Title("EC").ClientTemplate("<#= (EstCost).format('C') #>");

Still has the "¤500.00" problem though.
0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 03:09 AM
how about this ... 
http://www.telerik.com/community/forums/aspnet-mvc/general/format.aspx
 columns.Add(o => o.OrderTotal).Format("{0:C}").Title("Total"); 

0
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 03:18 AM
sorry, that had no dollor sign   This is not MVC, but it is formatting to give a try. 

  HeaderText="Order Total" DataFormatString="${0:C2}" DataField="OrderTotal" SortExpression="OrderTotal">

links i found on currency - you may have seen these already.

http://demos.telerik.com/aspnet-ajax/grid/examples/client/columnformatting/defaultcs.aspx 

http://demos.telerik.com/mvc/forum/aspnet-ajax/grid/varying-currency-symbol
0
Accepted
Atanas Korchev
Telerik team
answered on 01 Apr 2010, 08:07 AM
Hello,

I would like to shed some light in this.

  1. "{0:c}" is not supported for the time being. This is mentioned in this help topic (number formatting is not currently supported in ajax binding). As a workaround you can create a ViewModel class and a string property which contains the formatted value (you will format it server side when performing the binding).
  2. Indeed during client-side binding we show null which is not so user friendly. I have fixed this. To use the fix open telerik.grid.js and replace this
    if (column.format || column.type == 'Date')
           return function(data) {
                    return value == null ? '' : $t.formatString(column.format || '{0:G}', column.value(data));
           }
    with this:
    if (column.format || column.type == 'Date')
           return function(data) {
                 var value = column.value(data);
                 return value == null ? '' : $t.formatString(column.format || '{0:G}', value);
           }

Regards,
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
IQworks
Top achievements
Rank 1
answered on 01 Apr 2010, 02:40 PM
 Thanks for the info Atanas
0
Sean Cross
Top achievements
Rank 1
answered on 05 Apr 2010, 10:45 PM
Thanks that sorted out the dates.
Tags
Grid
Asked by
Sean Cross
Top achievements
Rank 1
Answers by
IQworks
Top achievements
Rank 1
Sean Cross
Top achievements
Rank 1
Alan MacDougall
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or