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

Re: Initializing paginator to a different page

Expand Messages
  • D. Wayne Fincher
    I m having the same exact problem. I can get one of two results: 1) The datatable shows the correct date for the correct page, but the nav shows the wrong page
    Message 1 of 16 , Dec 1, 2008
    • 0 Attachment
      I'm having the same exact problem. I can get one of two results:

      1) The datatable shows the correct date for the correct page, but the
      nav shows the wrong page number. This happens if I don't use this line
      of code ---> oPayload.pagination.recordOffset = oResponse.meta.startIndex;


      2) If I do use that line of code, the nav shows the correct page
      number, but the datatable incorrectly says there is no data to display.

      this.dTable.handleDataReturnPayload = function(oRequest, oResponse,
      oPayload) {
      oPayload.totalRecords = oResponse.meta.totalRecords;
      oPayload.pagination.recordOffset = oResponse.meta.startIndex;
      return oPayload;
      }

      That's what my Payload handler looks like. There must be something
      else I'm missing here?


      Lasse, sorry, but I didn't understand your suggested solution.


      --- In ydn-javascript@yahoogroups.com, "lassejohnsen"
      <lasse.johnsen@...> wrote:
      >
      > Hi,
      >
      >
      > I'm not sure, but I may have identified the solution.... (It works
      for me and I have a similar
      > problem- would you mind checking if it indeed also works for you?)
      >
      > So.....: My function looks like this:
      >
      > myDataTable.handleDataReturnPayload = function(oRequest, oResponse,
      oPayload) {
      >
      > oPayload.totalRecords = oResponse.meta.totalRecords;
      > oPayload.pagination.recordOffset =
      oResponse.meta.paginationRecordOffset;
      >
      > return oPayload;
      > }
      >
      >
      >
      > However due to a particularity in the Yahoo code the function get
      called too late, so try
      > and redeclare like so:
      >
      > myDataTable.onDataReturnSetRows = function(oRequest, oResponse,
      oPayload) {
      >
      >
      > var lang = YAHOO.lang,
      > util = YAHOO.util,
      > widget = YAHOO.widget,
      >
      > Dom = util.Dom,
      > Ev = util.Event,
      > DT = widget.DataTable;
      >
      > if((this instanceof DT) && this._sId) {
      > this.fireEvent("dataReturnEvent",
      > {request:oRequest,response:oResponse,payload:oPayload});
      >
      > // Pass data through abstract method for any transformations
      > var ok = this.doBeforeLoadData(oRequest, oResponse, oPayload),
      > pag = this.get('paginator'),
      > index = 0;
      >
      > // Data ok to set
      > if(ok && oResponse && !oResponse.error &&
      lang.isArray(oResponse.results)) {
      >
      > // Update state
      > this._handleDataReturnPayload(oRequest, oResponse, oPayload);
      > // ......to here please?
      >
      > // Update Records
      > if (this.get('dynamicData')) {
      > if (oPayload && oPayload.pagination &&
      > lang.isNumber(oPayload.pagination.recordOffset)) {
      > index = oPayload.pagination.recordOffset;
      > } else if (pag) {
      > index = pag.getStartIndex();
      > }
      > }
      >
      > this._oRecordSet.setRecords(oResponse.results, index | 0);
      >
      > // Update state
      > // this._handleDataReturnPayload(oRequest, oResponse,
      oPayload);
      > // Dear Dr. Yahoo, could you move this up.......
      >
      > // Update UI
      > this.render();
      > }
      > // Error
      > else if(ok && oResponse.error) {
      > this.showTableMessage(this.get("MSG_ERROR"), DT.CLASS_ERROR);
      > }
      > }
      > else {
      > YAHOO.log("Instance destroyed before data
      returned.","info",this.toString());
      > }
      > };
      >
      >
      >
      > If it works, or doesn't work or breaks something else that would be
      good to know.
      >
      > Thank you.
      >
      > /Lasse
      >
      > --- In ydn-javascript@yahoogroups.com, "Todd Wells" <ttopwells@> wrote:
      > >
      > > On Tue, Nov 18, 2008 at 7:27 AM, Lucas Smith <lsmith@> wrote:
      > >
      > > >
      > > > You have a couple options for how to proceed:
      > > > 1) pass either recordOffset or initialPage in the constructor as
      well
      > > > as a temporary value for totalRecords to facilitate Paginator
      > > > respecting the provided value.
      > > >
      > > > 2) include the recordOffset value in your data response and
      include a
      > > > metaField for your DataSource to capture it. Then in
      > > > handleDataReturnPayload, assign the value (after parseInt) to
      > > > payload.pagination.recordOffset
      > > >
      > >
      > > Actually (2) is what I was attempting to do that wasn't
      succeeding. One
      > > source of confusion is that oPayload has a totalRecords value
      which seems
      > > unrelated to oPayload.pagination.totalRecords, but here I tried
      setting both
      > > values and I end up with the same result, which is the paginator
      displays
      > > page 2, but the datatable itself says "no records found" and
      displays no
      > > records. Here's what I tried doing:
      > >
      > > resultsTable.handleDataReturnPayload = function (oRequest,
      > > oResponse, oPayload) {
      > > if (oPayload) {
      > > oPayload.totalRecords =
      oResponse.meta.totalCount;
      > > oPayload.pagination.totalRecords =
      > > oResponse.meta.totalCount;
      > > oPayload.pagination.recordOffset =
      > > oResponse.meta.offset;
      > > return oPayload;
      > > } else {
      > > oPayload = {};
      > > oPayload.totalRecords =
      oResponse.meta.totalCount;
      > > return oPayload;
      > > }
      > > };
      > >
      > > It might be worthwhile for you to do some exploratory testing
      around this
      > > scenario: dynamicData with a paginator and the initial request
      being not on
      > > page one. I could probably put this example up online for you
      somewhere,
      > > but it's non-trivial to do as it involves setting up an app server.
      > >
      > > The good news is, I just tried approach (1) and it seems to work.
      It feels
      > > a bit hackish, though, since I don't really know the total number
      of records
      > > before making the query. I suppose I could use the history manager
      to store
      > > the total number of records from the previous time the query was
      made, but
      > > there's not any guarantee that the total number will be the same
      on the next
      > > visit (since data could be modified outside of one particular
      client). I
      > > suppose I could probably make an additional count query before the
      initial
      > > query, but that seems like making the db work more than necessary
      as well as
      > > making an extra round trip to the server.
      > >
      > > I appreciate your willingness to dig in to this!
      > >
      >
    • Lucas Smith
      Here are two solutions: function toQuery(state) { return offset= +state.pagination.recordOffset+ &recs= +state.pagination.rowsPerPage; } function
      Message 2 of 16 , Dec 12, 2008
      • 0 Attachment
        Here are two solutions:

        function toQuery(state) {
            return "offset="+state.pagination.recordOffset+
                   "&recs="+state.pagination.rowsPerPage;
        }
        function handlePayload(req,res,payload) {
            // you should always parseInt meta values for Paginator
            payload.totalRecords = parseInt(res.meta.totalRecords,10);
            return payload;
        }

        // Solution 1: pass the recordOffset
        var initialOffset = 10;

        var paginator = new Paginator({
            rowsPerPage : 10,
            recordOffset: initialOffset,
            totalRecords: Number.MAX_VALUE // large number needed here
        });

        // initialRequest also includes initial recordOffset
        var conf = {
            initialRequest  : 'offset=' + initialOffset + '&recs=10',
            paginator       : paginator,
            dynamicData     : true,
            generateRequest : toQuery
        };

        var dataTable = new DataTable('tbl', colDef, dataSource, conf);
        dataTable.handleDataReturnPayload = handlePayload;


        // Solution 2: pass the initialPage and manually request initial data
        var initialPage = 2;

        var paginator = new Paginator({
            rowsPerPage : 10,
            initialPage : initialPage,
            totalRecords: Number.MAX_VALUE // arbitrary high number
        });

        var conf = {
            initialLoad     : false, // cancel the default send
            paginator       : paginator,
            dynamicData     : true,
            generateRequest : toQuery
        };

        dataTable = new DataTable('tbl', colDef, dataSource, conf);
        dataTable.handleDataReturnPayload = handlePayload;

        // Manually get the data using the DataTable's getState() passed to its generateRequest function
        dataSource.sendRequest(
            dataTable.get('generateRequest')(dataTable.getState()), // this is important
            {
                success  : dataTable.onDataReturnInitializeTable,
                scope    : dataTable,
                argument : dataTable.getState()
            });


        Hope this helps,
        Luke

        --- In ydn-javascript@yahoogroups.com, "D. Wayne Fincher" <slowlychillin@...> wrote:
        >
        > I'm having the same exact problem. I can get one of two results:
        >
        > 1) The datatable shows the correct date for the correct page, but the
        > nav shows the wrong page number. This happens if I don't use this line
        > of code ---> oPayload.pagination.recordOffset = oResponse.meta.startIndex;
        >
        >
        > 2) If I do use that line of code, the nav shows the correct page
        > number, but the datatable incorrectly says there is no data to display.
        >
        > this.dTable.handleDataReturnPayload = function(oRequest, oResponse,
        > oPayload) {
        > oPayload.totalRecords = oResponse.meta.totalRecords;
        > oPayload.pagination.recordOffset = oResponse.meta.startIndex;
        > return oPayload;
        > }
        >
        > That's what my Payload handler looks like. There must be something
        > else I'm missing here?
        >
        >
        > Lasse, sorry, but I didn't understand your suggested solution.
        >
        >
        > --- In ydn-javascript@yahoogroups.com, "lassejohnsen"
        > lasse.johnsen@ wrote:
        > >
        > > Hi,
        > >
        > >
        > > I'm not sure, but I may have identified the solution.... (It works
        > for me and I have a similar
        > > problem- would you mind checking if it indeed also works for you?)
        > >
        > > So.....: My function looks like this:
        > >
        > > myDataTable.handleDataReturnPayload = function(oRequest, oResponse,
        > oPayload) {
        > >
        > > oPayload.totalRecords = oResponse.meta.totalRecords;
        > > oPayload.pagination.recordOffset =
        > oResponse.meta.paginationRecordOffset;
        > >
        > > return oPayload;
        > > }
        > >
        > >
        > >
        > > However due to a particularity in the Yahoo code the function get
        > called too late, so try
        > > and redeclare like so:
        > >
        > > myDataTable.onDataReturnSetRows = function(oRequest, oResponse,
        > oPayload) {
        > >
        > >
        > > var lang = YAHOO.lang,
        > > util = YAHOO.util,
        > > widget = YAHOO.widget,
        > >
        > > Dom = util.Dom,
        > > Ev = util.Event,
        > > DT = widget.DataTable;
        > >
        > > if((this instanceof DT) && this._sId) {
        > > this.fireEvent("dataReturnEvent",
        > > {request:oRequest,response:oResponse,payload:oPayload});
        > >
        > > // Pass data through abstract method for any transformations
        > > var ok = this.doBeforeLoadData(oRequest, oResponse, oPayload),
        > > pag = this.get('paginator'),
        > > index = 0;
        > >
        > > // Data ok to set
        > > if(ok && oResponse && !oResponse.error &&
        > lang.isArray(oResponse.results)) {
        > >
        > > // Update state
        > > this._handleDataReturnPayload(oRequest, oResponse, oPayload);
        > > // ......to here please?
        > >
        > > // Update Records
        > > if (this.get('dynamicData')) {
        > > if (oPayload && oPayload.pagination &&
        > > lang.isNumber(oPayload.pagination.recordOffset)) {
        > > index = oPayload.pagination.recordOffset;
        > > } else if (pag) {
        > > index = pag.getStartIndex();
        > > }
        > > }
        > >
        > > this._oRecordSet.setRecords(oResponse.results, index | 0);
        > >
        > > // Update state
        > > // this._handleDataReturnPayload(oRequest, oResponse,
        > oPayload);
        > > // Dear Dr. Yahoo, could you move this up.......
        > >
        > > // Update UI
        > > this.render();
        > > }
        > > // Error
        > > else if(ok && oResponse.error) {
        > > this.showTableMessage(this.get("MSG_ERROR"), DT.CLASS_ERROR);
        > > }
        > > }
        > > else {
        > > YAHOO.log("Instance destroyed before data
        > returned.","info",this.toString());
        > > }
        > > };
        > >
        > >
        > >
        > > If it works, or doesn't work or breaks something else that would be
        > good to know.
        > >
        > > Thank you.
        > >
        > > /Lasse
        > >
        > > --- In ydn-javascript@yahoogroups.com, "Todd Wells" <ttopwells@> wrote:
        > > >
        > > > On Tue, Nov 18, 2008 at 7:27 AM, Lucas Smith <lsmith@> wrote:
        > > >
        > > > >
        > > > > You have a couple options for how to proceed:
        > > > > 1) pass either recordOffset or initialPage in the constructor as
        > well
        > > > > as a temporary value for totalRecords to facilitate Paginator
        > > > > respecting the provided value.
        > > > >
        > > > > 2) include the recordOffset value in your data response and
        > include a
        > > > > metaField for your DataSource to capture it. Then in
        > > > > handleDataReturnPayload, assign the value (after parseInt) to
        > > > > payload.pagination.recordOffset
        > > > >
        > > >
        > > > Actually (2) is what I was attempting to do that wasn't
        > succeeding. One
        > > > source of confusion is that oPayload has a totalRecords value
        > which seems
        > > > unrelated to oPayload.pagination.totalRecords, but here I tried
        > setting both
        > > > values and I end up with the same result, which is the paginator
        > displays
        > > > page 2, but the datatable itself says "no records found" and
        > displays no
        > > > records. Here's what I tried doing:
        > > >
        > > > resultsTable.handleDataReturnPayload = function (oRequest,
        > > > oResponse, oPayload) {
        > > > if (oPayload) {
        > > > oPayload.totalRecords =
        > oResponse.meta.totalCount;
        > > > oPayload.pagination.totalRecords =
        > > > oResponse.meta.totalCount;
        > > > oPayload.pagination.recordOffset =
        > > > oResponse.meta.offset;
        > > > return oPayload;
        > > > } else {
        > > > oPayload = {};
        > > > oPayload.totalRecords =
        > oResponse.meta.totalCount;
        > > > return oPayload;
        > > > }
        > > > };
        > > >
        > > > It might be worthwhile for you to do some exploratory testing
        > around this
        > > > scenario: dynamicData with a paginator and the initial request
        > being not on
        > > > page one. I could probably put this example up online for you
        > somewhere,
        > > > but it's non-trivial to do as it involves setting up an app server.
        > > >
        > > > The good news is, I just tried approach (1) and it seems to work.
        > It feels
        > > > a bit hackish, though, since I don't really know the total number
        > of records
        > > > before making the query. I suppose I could use the history manager
        > to store
        > > > the total number of records from the previous time the query was
        > made, but
        > > > there's not any guarantee that the total number will be the same
        > on the next
        > > > visit (since data could be modified outside of one particular
        > client). I
        > > > suppose I could probably make an additional count query before the
        > initial
        > > > query, but that seems like making the db work more than necessary
        > as well as
        > > > making an extra round trip to the server.
        > > >
        > > > I appreciate your willingness to dig in to this!
        > > >
        > >
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.