| Page 1 of 1 | [ 10 posts ] |
|
Hello all,
I am using a custom formatter for displaying date . Both functions work fine in FF but not in IE 7/8 . Any help will be appreciated. // Define a custom format function this is from YUI site as is var myFormatDate = function (elCell, oRecord, oColumn, oData) { var oDate = oData, sMonth; switch(oDate.getMonth()) { case 0: sMonth = "Jan"; break; case 1: sMonth = "Feb"; break; case 2: sMonth = "Mar"; break; case 3: sMonth = "Apr"; break; case 4: sMonth = "May"; break; case 5: sMonth = "Jun"; break; case 6: sMonth = "Jul"; break; case 7: sMonth = "Aug"; break; case 8: sMonth = "Sep"; break; case 9: sMonth = "Oct"; break; case 10: sMonth = "Nov"; break; case 11: sMonth = "Dec"; break; } elCell.innerHTML = sMonth + " " + oDate.getDate() + ", " + oDate.getFullYear(); }; // This is using format under util.Date var myFormatDate1 = function (elCell, oRecord, oColumn, oData) { var oDate = new Date(oData); elCell.innerHTML = YAHOO.util.Date.format(oDate, { format: "%b %d, %Y"}, 'en-US');; }; // Column definitions var myColumnDefs = [ // sortable:true enables sorting {key:"Gender",label:"Gender"}, {key:"BirthDate",label:"DOB",formatter: myFormatDate1} ]; myDataSource.responseSchema = { resultsList: "records", fields: [ {key:"Gender"}, {key:"BirthDate",parser: 'date'} ] " BirthDate" is of type date in MySql. And setting parser to string displays date as in the db . Thank you Sucheta |
Matt ParkerYUI Contributor
|
Hi,
Two things: firstly, the date you send in your data needs to be something javascript can parse into a date object - ie something like mm/dd/yyyy - so you'll need to do some conversion on the server. Secondly, take a look at this viewtopic.php?p=14172#p14172 - it might make your date formatter a bit smaller. Matt |
|
Thanks Matt.
This is what I did. My BirthDate column in db held value as date and as say 2010-06-02 . I made the following changes in my code : fields: [ {key:"Gender"}, {key:"BirthDate",parser: "string"} ], And changed my second format function myformatDate1 function as var myFormatDate1 = function (elCell, oRecord, oColumn, oData) { var year=oData.substr(0,4); var month=oData.substr(5,2); var day=oData.substr(8,2); var oDate = new Date(year,month,day); elCell.innerHTML = YAHOO.util.Date.format(oDate, { format: "%b %d, %Y"}, 'en-US');; }; and assigned it as var myColumnDefs = [ // sortable:true enables sorting {key:"Gender",label:"Gender"}, {key:"BirthDate",label:"DOB",formatter: myFormatDate1} ]; It works in both browsers now. Thanks Sucheta |
|
Though it should work as you have it, it probably won't be fully functional. If you were to set that column as editable, the DateCellEditor would not work with a date stored as a string.
The right way to do it is to set a parser in the fields array of the responseSchema to parse the date in the format you receive it and return it as an instance of javaScript Date object. Then you can use the regular 'date' formatter with whatever dateOptions.format you want. You cannot use the built-in date parser for the date format you have. The built-in date parser uses JavaScript's own Date.parse method, which accepts only a few formats and not the YMD format you receive from your server. So, the solution is not on the side of the formatter as you have done but you should, instead, build your own parser for that YMD format. |
|
Thanks Satyam and thanks Matt too.
Going by your suggestion Satyam this is what I have done now and it works in both browsers. 1. Defined a custom parser var myDateParser = function (oData) { var year=oData.substr(0,4); var month=oData.substr(5,2); var day=oData.substr(8,2); var oDate = new Date(year,month,day); return oDate; }; 2. applied to my field {key:"BirthDate",parser: myDateParser}, 3.set Date formatter in column {key:"BirthDate",label:"DOB",formatter:YAHOO.widget.DataTable.formatDate}, 4.and used dateOptions in config as below dateOptions: {format:"%b %d, %Y"} Hope I have got it right now BTW I am just displaying some data , no editing. Thanks Sucheta |
|
You have it right now. Even if you don't edit that date right now, you might in some other page or other application in the future and it is best to have it right. A couple of tips, you could assign your parser to
YAHOO.util.DataSource.Parser.mySqlDate = function (oData) { // your parser } Then you could use it as {key:"BirthDate", parser:"mySqlDate"} Also, in the col defs, you could say: formatter:"date" |
|
Thank You so much.
I have done as suggested by you . Sucheta |
|
hey i hv done as said but it still doesnt work in IE
|
|
So, it does in others? And even after doing it all still refuses to do it in IE?
|
|
If you are seeing a mismatch in month [month in Javascript begins from 0 ].
you may have to add this month=month-1; in the custom method . Sucheta |
| Page 1 of 1 | [ 10 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group