0

Does anybody have an example of using the date formatter with a server side database, or can you point me to something to help?

Mike Cole
  • 14,474
  • 28
  • 114
  • 194
Mark Jones
  • 1
  • 1
  • 1
  • 2

2 Answers2

6

You can find information about predefined formatters on the jqGrid wiki.

The following is an example of how date formatting can be used in the grid. The format ShortDate displays the date according to the selected locale. You can use your own formatting instead, for example Y-m-d H:i:s.

srcformat describes the format of the date as sent by the server, newformat describes the desired output format.

This example includes searchoptions which will make sure that your users can select the desired date with the help of a datepicker when performing a search on the grid.

colModel :[ 
    { name:'startdate', index:'startdate', formatter:'date', 
        formatoptions: { srcformat:'m/d/Y', newformat:'ShortDate' }, 
        searchoptions: { sopt: ['eq','lt','le','gt','ge'],
            dataInit : function (elem) { 
                $(elem).datepicker({ changeMonth: true, changeYear: true, 
                    dateFormat: 'yy-mm-dd' }); 
            }
        }
    }
]
flanth
  • 201
  • 1
  • 3
  • 2
    Thank you for your reply and the example you provided. Unfortunately, I'm still not clear about this.For example, if a grid displays data from a mysql, how should I handle mysql date fields. Mysql date fields use yyyy-mm-dd format, but the grid displays mm-dd-yy. I used srcformat:'Y-m-d' and newformat:'mm-dd-yy'. The data is read and displayed properly. However, when I edit an entry (using a modal form), the update doesn't work, because the date is not in the proper mysql format. In the past, I thought the formatter handled this automatically, but it doesn't seem to anymore. – Mark Jones Nov 01 '11 at 15:21
  • You would need to specify datepicker format as "yy-mm-dd" which should be the same as the MySql date format. – BinBin Apr 19 '13 at 22:27
0

We can also take the transient field of the date in pozo class and check in getter methed if date is not null then convert it into datetostring .Also we have to change in jsp where we used this jqgrid we have to take transient field instead of date field.

example :

(Pozo Class)

 transient private String indentDate_String;

  public String getIndentDate_String() 
  {
     if(indentDate != null)
     indentDate_String = DateConversion.dateToString(indentDate);
     return indentDate_String;
  }

jqgrid (jsp form):
colNames:['Indent Date'],
colModel:[      
 {name:'indentDate_String',index:'indentDate',autoheight: true, width:100},
]