Loading ...
Sorry, an error occurred while loading the content.

[ydn-javascript] datatable empty dates, invalid dates, NaNs

Expand Messages
  • Martin Tschofen
    I don t understand how to handle non existing dates so they do not display NaN/NaN/NaN in my datatable. I looked through previous posts but can t seem to
    Message 1 of 3 , Aug 21, 2008
    • 0 Attachment
      I don't understand how to handle non existing dates so they do not display NaN/NaN/NaN in my datatable.
      I looked through previous posts but can't seem to figure it out.

      I'm pulling my data from markup. A simple HTML table. I specify the parser and the formatter to be a date:
                  var myColumnDefs = [
                      {key:"openedDate", label:tblLabels[0].innerHTML,className:tblLabels[0].className, formatter:'date', sortable:true},
                      {key:"status", label:tblLabels[2].innerHTML,className:tblLabels[2].className, formatter:CMW.soal.util.formatStatus, sortable:true},
                      {key:"dueDate", label:tblLabels[3].innerHTML,className:tblLabels[3].className, formatter:'date', sortable:true}
                  ];

                  var resDefs = [
                          {key:"openedDate", parser:YAHOO.util.DataSource.parseDate},
                         {key:"status"},
                          {key:"dueDate", parser:YAHOO.util.DataSource.parseDate}
                      ];
                  tblParser.responseSchema = {fields: resDefs};

      Some <td>s where a date is supposed to go are empty: <td></td>. No space or return in it.
      But these dates display as NaN/NaN/NaN instead of an empty cell.

      So I figured I'd put a custom formatter on the date cells:
              formatDate : function(elCell, oRecord, oColumn, oData) {
                  if (oData != 'Invalid Date') {
                      YAHOO.widget.DataTable.formatDate(elCell, oRecord, oColumn, oData);
                  } else {
                      elCell.innerHTML = '-';
                  }
              }

      All previously empty <td>'s are 'Invalid Date' and now display as hyphens.

      While the formatting works, the sorting suddenly is goofy. Some of the Invalid Dates appear among the otherwise properly sorted values.
      To check the values in the cells, I just removed the date formatter and I can see all the 'invalid date's.

      Any help would be greatly appreciated...martin
    • Lucas Smith
      ... display ... parser ... parser:YAHOO.util.DataSource.parseDate} ... space or ... oColumn, ... hyphens. ... and I ... The sorting is done using the
      Message 2 of 3 , Aug 21, 2008
      • 0 Attachment
        --- In ydn-javascript@yahoogroups.com, "Martin Tschofen"
        <martin.tschofen@...> wrote:
        >
        > I don't understand how to handle non existing dates so they do not
        display
        > NaN/NaN/NaN in my datatable.
        > I looked through previous posts but can't seem to figure it out.
        >
        > I'm pulling my data from markup. A simple HTML table. I specify the
        parser
        > and the formatter to be a date:
        > var myColumnDefs = [
        > {key:"openedDate",
        > label:tblLabels[0].innerHTML,className:tblLabels[0].className,
        > formatter:'date', sortable:true},
        > {key:"status",
        > label:tblLabels[2].innerHTML,className:tblLabels[2].className,
        > formatter:CMW.soal.util.formatStatus, sortable:true},
        > {key:"dueDate",
        > label:tblLabels[3].innerHTML,className:tblLabels[3].className,
        > formatter:'date', sortable:true}
        > ];
        >
        > var resDefs = [
        > {key:"openedDate",
        > parser:YAHOO.util.DataSource.parseDate},
        > {key:"status"},
        > {key:"dueDate",
        parser:YAHOO.util.DataSource.parseDate}
        > ];
        > tblParser.responseSchema = {fields: resDefs};
        >
        > Some <td>s where a date is supposed to go are empty: <td></td>. No
        space or
        > return in it.
        > But these dates display as NaN/NaN/NaN instead of an empty cell.
        >
        > So I figured I'd put a custom formatter on the date cells:
        > formatDate : function(elCell, oRecord, oColumn, oData) {
        > if (oData != 'Invalid Date') {
        > YAHOO.widget.DataTable.formatDate(elCell, oRecord,
        oColumn,
        > oData);
        > } else {
        > elCell.innerHTML = '-';
        > }
        > }
        >
        > All previously empty <td>'s are 'Invalid Date' and now display as
        hyphens.
        >
        > While the formatting works, the sorting suddenly is goofy. Some of the
        > Invalid Dates appear among the otherwise properly sorted values.
        > To check the values in the cells, I just removed the date formatter
        and I
        > can see all the 'invalid date's.
        >
        > Any help would be greatly appreciated...martin
        >

        The sorting is done using the unformatted values in the DataTable's
        RecordSet. That is, the output value of the DataSource parser.

        Probably best in your case to override DataSource.parseDate or provide
        a custom sort function that accounts for the possibility of invalid dates.

        Have a look at this thread for inspiration on the parsing front:
        http://tech.groups.yahoo.com/group/ydn-javascript/message/36475

        Hope this helps,
        Luke
      • Martin Tschofen
        Thank you for your quick response. Here s how I got it to work: Override the default date parser to attach a date in the past instead of null or invalid dates.
        Message 3 of 3 , Aug 22, 2008
        • 0 Attachment
          Thank you for your quick response.

          Here's how I got it to work:
          Override the default date parser to attach a date in the past instead of null or invalid dates. Thus old dates will sort to the bottom or top.

                      YAHOO.util.DataSource.parseDate = function(oData){
                          if(!(oData instanceof Date)){
                              var date = new Date(oData);
                              if(date == 'Invalid Date'){date = new Date('1/11/1900');}
                              return date;
                          } else {return oData;}
                      }
                     
          And then I created a custom formatter that wouldn't display these old dates.

                  formatDate : function(elCell, oRecord, oColumn, oData) {
                      var d = new Date('1/11/1900');
                      if (oData > d) {
                          YAHOO.widget.DataTable.formatDate(elCell, oRecord, oColumn, oData);
                      } else {
                          elCell.innerHTML = '';
                      }
                  },


          I did look at the custom sorting, but I have too many different date columns across a number of datatables to make it work efficiently.

          Do you see any problems with this approach?
          Thanks again for your help...martin
        Your message has been successfully submitted and would be delivered to recipients shortly.