Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - GMail API pagination use of nextPageToken

Have spent hours searching Google et. al. for an answer, sure it's simple but how do you create pagination with the GMail api using nextPageToken? what ever I do cannot get pagination to work (back and forth that is).

Assume 'authorised user' and access with the right scopes I call

 gapi.client.load('gmail','v1',displayInbox);

then

function displayInbox(){
    var request = gapi.client.gmail.users.messages.list({
    'userId':'me',
    'maxResults':10,
    });

  request.execute(function(response){
    $.each(response.messages,function(){
      var messageRequest = gapi.client.gmail.users.messages.get({
        'userId':'me',
        'id':this.id
      });
      messageRequest.execute(appendMessageRow);
    });
  });
}

appendMessageRow simply lays out the list in a table e.g.

function appendMessageRow(message){
   var txt = '<tr>';
   txt +='<td>'+getHeader(message.payload.headers, 'From')+'</td>';
   txt +='<td>';
   txt +='<a href="#message-modal-'+ message.id +'" data-toggle="modal" id="message-link-' + message.id+'">' +getHeader(message.payload.headers, 'Subject') +'</a>';
   txt +='</td>';
   txt +='<td class="text-xs-right">'+moment(parseInt(message.internalDate)).format('HH:mm')+'</td>';
   txt +='</tr>';
   $('table tbody').append(txt);
}

When I console.log request.execute I see nextPageToken as an object key What I cannot do and need to do is add pagination buttons - messageRequest.execute does not pass the nextPageToken plus there does not seem to be a way to create/obtain a 'previousPageToken'.

Sorry if simple but is it me or is there far more to it than that? The GMail API docs appear very poor (to me) on this subject and I have not found a stackoverflow answer that helps.

To recap - how do I add pagination buttons and pass the appropriate variables to call/recall displayInbox().

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could save the next page token on every request and use it in your next request. If there is no next page token in the response, you know that you have gotten all messages:

function listMessages(pageToken) {
  return new Promise(function(resolve) {
    var options = {
      userId: 'me',
      maxResults: 10
    };
    if (pageToken) {
      options.pageToken = pageToken;
    }
    var request = gapi.client.gmail.users.messages.list(options);
    request.execute(resolve);
  });
}

function getMessage(message) {
  return new Promise(function(resolve) {
    var messageRequest = gapi.client.gmail.users.messages.get({
      userId: 'me',
      id: message.id
    });
    messageRequest.execute(resolve);
  });
}

var pageToken;
function displayInbox(){
  listMessages(pageToken).then(function (response) {
    if (response.nextPageToken) {
      pageToken = response.nextPageToken; // Get the next page next time
    } else {
      console.log('No more pages left!');
    }
    if (response.messages) {
      Promise.all(response.messages.map(getMessage)).then(function (messages) {
        messages.forEach(appendMessageRow);
      });
    }
  })
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...