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

Any examples of DataTable.getPreviousTrEl/formatRow use?

Expand Messages
  • Andy Stevens
    Hi, Are there any examples anywhere of using DataTable.getPreviousTrEl(), ideally within a formatRow function? I m currently trying var summaryOptions = {
    Message 1 of 6 , Jan 27, 2009
      Hi,

      Are there any examples anywhere of using DataTable.getPreviousTrEl(),
      ideally within a formatRow function? I'm currently trying

      var summaryOptions = {
      formatRow: function(elRow, oRecord) {
      var previousRow = this.getPreviousTrEl(elRow);
      var firstRowInGroup = (previousRow == null) ||
      (this.getRecord(previousRow).getData("groupKey")
      != oRecord.getData("groupKey"));
      if (firstRowInGroup) {
      YAHOO.util.Dom.addClass(elRow,'first');
      YAHOO.util.Dom.removeClass(elRow,'next');
      } else {
      YAHOO.util.Dom.addClass(elRow,'next');
      YAHOO.util.Dom.removeClass(elRow,'first');
      }
      return true;
      }
      };
      var summaryDataTable = new YAHOO.widget.DataTable("summaryTableContainer",
      summaryColumnDefs, summaryDataSource, summaryOptions);

      but get an error "this.getPreviousTrEl is not a function". If I
      change it to use summaryDataTable.getPreviousTrEl(elRow) and
      summaryDataTable.getRecord(previousRow) instead, I get
      "summaryDataTable has no properties".
      Is this just something I don't grok with the scoping? Or should I not
      expect getPreviousTrEl to work in this context, and the only way is to
      keep track of the previous value in a separate variable like
      http://satyam.com.ar/yui/2.6.0/Grouping.html does?


      Andy
      --
      http://pseudoq.sourceforge.net/ Open source java Sudoku application
    • Satyam
      By the time the formatRow function is called, the element is not yet appended to the so there are no previous or next rows to it. One of the
      Message 2 of 6 , Jan 28, 2009
        By the time the formatRow function is called, the <tr> element is not
        yet appended to the <table> so there are no previous or next rows to
        it. One of the options of formatRow is to cancel the rendering of the
        whole row by returning false which, in fact, means that the row will
        only be added to the DataTable if formatRow returns true.

        'this' is not set, which is quite inconvenient. It would make sense
        that 'this' would point to the DataTable instance, at this point, it
        doesn't. It is reported fixed for 2.7:

        http://yuilibrary.com/projects/yui2/ticket/2351549

        When the DataTable is first build, you cannot use summaryDataTable to
        reach those DataTable methods since the DataTable is being instanced at
        the time and that instance has not been assigned to summaryDataTable yet.

        Satyam


        Andy Stevens wrote:
        > Hi,
        >
        > Are there any examples anywhere of using DataTable.getPreviousTrEl(),
        > ideally within a formatRow function? I'm currently trying
        >
        > var summaryOptions = {
        > formatRow: function(elRow, oRecord) {
        > var previousRow = this.getPreviousTrEl(elRow);
        > var firstRowInGroup = (previousRow == null) ||
        > (this.getRecord(previousRow).getData("groupKey")
        > != oRecord.getData("groupKey"));
        > if (firstRowInGroup) {
        > YAHOO.util.Dom.addClass(elRow,'first');
        > YAHOO.util.Dom.removeClass(elRow,'next');
        > } else {
        > YAHOO.util.Dom.addClass(elRow,'next');
        > YAHOO.util.Dom.removeClass(elRow,'first');
        > }
        > return true;
        > }
        > };
        > var summaryDataTable = new YAHOO.widget.DataTable("summaryTableContainer",
        > summaryColumnDefs, summaryDataSource, summaryOptions);
        >
        > but get an error "this.getPreviousTrEl is not a function". If I
        > change it to use summaryDataTable.getPreviousTrEl(elRow) and
        > summaryDataTable.getRecord(previousRow) instead, I get
        > "summaryDataTable has no properties".
        > Is this just something I don't grok with the scoping? Or should I not
        > expect getPreviousTrEl to work in this context, and the only way is to
        > keep track of the previous value in a separate variable like
        > http://satyam.com.ar/yui/2.6.0/Grouping.html does?
        >
        >
        > Andy
        >
        > ------------------------------------------------------------------------
        >
        >
        > No virus found in this incoming message.
        > Checked by AVG - http://www.avg.com
        > Version: 8.0.176 / Virus Database: 270.10.14/1918 - Release Date: 27/01/2009 7:26
        >
        >
      • Andy Stevens
        ... Ah yes, I can see how that could be a problem... Is formatRow ever called outside of building the entire table/page? i.e. is it safe to assume that the row
        Message 3 of 6 , Jan 29, 2009
          2009/1/29 Satyam <satyam@...>:
          > By the time the formatRow function is called, the <tr> element is not
          > yet appended to the <table> so there are no previous or next rows to
          > it. One of the options of formatRow is to cancel the rendering of the
          > whole row by returning false which, in fact, means that the row will
          > only be added to the DataTable if formatRow returns true.

          Ah yes, I can see how that could be a problem...
          Is formatRow ever called outside of building the entire table/page?
          i.e. is it safe to assume that the row being formatted is always to be
          added (assuming I return true) at the end of the table body, and I can
          just look up the last existing one?
          Even if that's not the case (I'm guessing it's also used if you
          subsequently insert a row programmatically, unless that just rebuilds
          the whole lot), at the point it's called the caller must have some
          idea where in the table the new will be inserted - is that "new" row
          index available somehow (or could it be made so) within the function?
          That way I could just subtract one to get the previous row's index,
          and look that up.


          Andy
          --
          http://pseudoq.sourceforge.net/
        • Satyam
          ... There is no stated rule as to when formatRow would be called and whatever you find out in this version might be invalid in the next version. The YUI team
          Message 4 of 6 , Jan 29, 2009
            Andy Stevens wrote:
            > 2009/1/29 Satyam <satyam@...>:
            >
            >> By the time the formatRow function is called, the <tr> element is not
            >> yet appended to the <table> so there are no previous or next rows to
            >> it. One of the options of formatRow is to cancel the rendering of the
            >> whole row by returning false which, in fact, means that the row will
            >> only be added to the DataTable if formatRow returns true.
            >>
            >
            > Ah yes, I can see how that could be a problem...
            > Is formatRow ever called outside of building the entire table/page?
            > i.e. is it safe to assume that the row being formatted is always to be
            > added (assuming I return true) at the end of the table body, and I can
            > just look up the last existing one?
            > Even if that's not the case (I'm guessing it's also used if you
            > subsequently insert a row programmatically, unless that just rebuilds
            > the whole lot), at the point it's called the caller must have some
            > idea where in the table the new will be inserted - is that "new" row
            > index available somehow (or could it be made so) within the function?
            > That way I could just subtract one to get the previous row's index,
            > and look that up.
            >
            >
            > Andy
            There is no stated rule as to when formatRow would be called and
            whatever you find out in this version might be invalid in the next
            version. The YUI team is constantly looking for ways to improve
            performance. DOM manipulation is particularly slow on IE so it is one
            of the first things to play with when dealing with performance. IE8
            might improve this, DOM manipulation might not be so important, or it
            might have some particular way around it that the current version does
            not. If you make your code dependent on such circumstantial sequence of
            events, you are likely to be sorry for that in a not so far future.

            Satyam
          • Satyam
            ... I am sorry, allow me to correct this piece of misinformation. Returning false does not prevent the Row from being added, it simply prevents the individual
            Message 5 of 6 , Jan 29, 2009
              Satyam wrote:
              > By the time the formatRow function is called, the <tr> element is not
              > yet appended to the <table> so there are no previous or next rows to
              > it. One of the options of formatRow is to cancel the rendering of the
              > whole row by returning false which, in fact, means that the row will
              > only be added to the DataTable if formatRow returns true.
              >
              I am sorry, allow me to correct this piece of misinformation.

              Returning false does not prevent the Row from being added, it simply
              prevents the individual cell formatters from being called, the row might
              or might not be already added to the DataTable but, if it isn't, it will
              be added, eventually. To prevent a row from showing, set its className
              to something that will make it invisible, like:

              YAHOO.util.Dom.addClass(elRow,'hidden');
              return false;

              You might as well return false to dispense with the time wasted in
              formatting cells that won't be shown.

              Satyam

              > 'this' is not set, which is quite inconvenient. It would make sense
              > that 'this' would point to the DataTable instance, at this point, it
              > doesn't. It is reported fixed for 2.7:
              >
              > http://yuilibrary.com/projects/yui2/ticket/2351549
              >
              > When the DataTable is first build, you cannot use summaryDataTable to
              > reach those DataTable methods since the DataTable is being instanced at
              > the time and that instance has not been assigned to summaryDataTable yet.
              >
              > Satyam
              >
              >
              > Andy Stevens wrote:
              >
              >> Hi,
              >>
              >> Are there any examples anywhere of using DataTable.getPreviousTrEl(),
              >> ideally within a formatRow function? I'm currently trying
              >>
              >> var summaryOptions = {
              >> formatRow: function(elRow, oRecord) {
              >> var previousRow = this.getPreviousTrEl(elRow);
              >> var firstRowInGroup = (previousRow == null) ||
              >> (this.getRecord(previousRow).getData("groupKey")
              >> != oRecord.getData("groupKey"));
              >> if (firstRowInGroup) {
              >> YAHOO.util.Dom.addClass(elRow,'first');
              >> YAHOO.util.Dom.removeClass(elRow,'next');
              >> } else {
              >> YAHOO.util.Dom.addClass(elRow,'next');
              >> YAHOO.util.Dom.removeClass(elRow,'first');
              >> }
              >> return true;
              >> }
              >> };
              >> var summaryDataTable = new YAHOO.widget.DataTable("summaryTableContainer",
              >> summaryColumnDefs, summaryDataSource, summaryOptions);
              >>
              >> but get an error "this.getPreviousTrEl is not a function". If I
              >> change it to use summaryDataTable.getPreviousTrEl(elRow) and
              >> summaryDataTable.getRecord(previousRow) instead, I get
              >> "summaryDataTable has no properties".
              >> Is this just something I don't grok with the scoping? Or should I not
              >> expect getPreviousTrEl to work in this context, and the only way is to
              >> keep track of the previous value in a separate variable like
              >> http://satyam.com.ar/yui/2.6.0/Grouping.html does?
              >>
              >>
              >> Andy
              >>
              >> ------------------------------------------------------------------------
              >>
              >>
              >> No virus found in this incoming message.
              >> Checked by AVG - http://www.avg.com
              >> Version: 8.0.176 / Virus Database: 270.10.14/1918 - Release Date: 27/01/2009 7:26
              >>
              >>
              >>
              >
              > ------------------------------------
              >
              > Yahoo! Groups Links
              >
              >
              >
              > ------------------------------------------------------------------------
              >
              >
              > No virus found in this incoming message.
              > Checked by AVG - www.avg.com
              > Version: 8.0.233 / Virus Database: 270.10.14/1920 - Release Date: 01/27/09 18:15:00
              >
              >
            • Andy Stevens
              ... Okay, so I might need to rewrite things if & when I migrate to a newer version. But that might just as likely be the case for the way you ve done it in
              Message 6 of 6 , Jan 29, 2009
                2009/1/29 Satyam <satyam@...>:
                > There is no stated rule as to when formatRow would be called and
                > whatever you find out in this version might be invalid in the next
                > version. The YUI team is constantly looking for ways to improve
                > performance. DOM manipulation is particularly slow on IE so it is one
                > of the first things to play with when dealing with performance. IE8
                > might improve this, DOM manipulation might not be so important, or it
                > might have some particular way around it that the current version does
                > not. If you make your code dependent on such circumstantial sequence of
                > events, you are likely to be sorry for that in a not so far future.

                Okay, so I might need to rewrite things if & when I migrate to a newer
                version. But that might just as likely be the case for the way you've
                done it in http://www.satyam.com.ar/yui/2.6.0/Grouping.html (which is
                how I've currently got it working). In there you just keep track of
                the previous row's values (var previousRow = {}) between calls to the
                formatter, but who's to say that the rows/cells will be formatted in
                sequence? It might turn out that it's quicker to add the rows in
                reverse, or some other indeterminate order, in which case previousRow
                might not be the previous row...

                In fact, if you add
                dt.addRow(dt.getRecord(dt.getRecordIndex(1)).getData(), 1);
                at the end of the onSuccess function in your Grouping example, the new
                row is displayed as a separate group to the initial line(s) rather
                than being grouped with them as you would ideally expect.

                It seems to me the only "safe" way is to loop over the rows/cells and
                decorate them after everything has finished being added. Perhaps,
                rather than using formtter/formatRow, I should subscribe a function to
                the postRenderEvent and post-process all the rows in there to add my
                CSS classes? Only question is whether or not the Dom.addClass
                manipulation I do in there will cause another renderEvent ("Fired when
                the DataTable's DOM is rendered or modified.") to trigger, leading to
                another postRenderEvent, leading to an infinite loop if I'm not
                careful...


                Andy
                --
                http://pseudoq.sourceforge.net/ Open source java sudoku creator
              Your message has been successfully submitted and would be delivered to recipients shortly.