如何浏览到加载到Google AppMaker表中的数据源中的下一页 [英] How to browse to the next page in a datasource that is loaded into table in Google AppMaker

查看:37
本文介绍了如何浏览到加载到Google AppMaker表中的数据源中的下一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个要求,即我有一个名为"emailSearchResults"的数据源,在其中搜索电子邮件元数据并将结果加载到数据源中.

I'm working on a requirement where I have a datasource named 'emailSearchResults' where I search for email messages metadata and load the results in the datasource.

数据源中的字段不相关,但是根据以下屏幕截图,我将数据源设置为每页50条记录:

The fields in the datasource are not relevant, however I set the datasource to have 50 records per page as per the below screenshot:

查询字段中显示了我用来加载数据源的脚本,该脚本调用以下脚本:

The script I used to load the datasource is shown in the query field, that call the following script:

function getMessageDetails(userId, msgID)
{
  var messageDetails = [];
  var messageData;
  var msgID_,subject_,from_,date_;

  messageData=Gmail.Users.Messages.get(userId,msgID,{format:"metadata", metadataHeaders:["Message-ID", "Subject", "From", "Date"]});

  console.log(messageData.payload.headers);

  //console.log(msgID);
  //console.log(messageData.payload.headers[3].value);

  date_="<na>";
  from_="<na>";
  subject_="<na>";
  msgID_="<na>";

  for (var counter =0;counter<4;counter++)
  {
    if (messageData.payload.headers[counter].name=="Message-ID")
    {
      msgID_=messageData.payload.headers[counter].value;
    }

    if (messageData.payload.headers[counter].name=="Subject")
    {
      subject_=messageData.payload.headers[counter].value;
    }

    if (messageData.payload.headers[counter].name=="From")
    {
      from_=messageData.payload.headers[counter].value;
    }

    if (messageData.payload.headers[counter].name=="Date")
    {
      date_=messageData.payload.headers[counter].value;
    }
  }

  messageDetails.push(date_);
  messageDetails.push(from_);
  messageDetails.push(subject_);
  messageDetails.push(msgID_);

  return messageDetails;
}


function searchMessages(userId,condition)
{
  //
  // first we build the conditions
  // we can make it fixed
  // or we can make it dynamic
  var searchResult;
  var deleteResult;
  var currentMessage;
  var results = [];
  var pageToken;
  var params = {};
  var _stat;
  var options = {
    includeSpamTrash: "true",
    pageToken: pageToken
  };
  var msgRecord = [];

  do
  {
    searchResult=Gmail.Users.Messages.list(userId,options);

    for (var i = 0; i < searchResult.messages.length; i++)
    {
      var record=app.models.emailSearchResults.newRecord();
      msgRecord=getMessageDetails(userId,searchResult.messages[i].id);

      record.msgMainID=searchResult.messages[i].id;
      record.msgID=msgRecord[3];
      record.subject=msgRecord[2];
      record.senderAddress=msgRecord[1];
      record.msgDate=msgRecord[0];

      /*console.log(searchResult.messages[i].id);
      console.log(msgRecord[3]);
      console.log(msgRecord[2]);
      console.log(msgRecord[1]);
      console.log(msgRecord[0]);

      return;*/
      results.push(record);
      msgRecord=null;
    }

    if (searchResult.nextPageToken) {
      options.pageToken = searchResult.nextPageToken;
    }
  } while (searchResult.pageToken);

  searchResult=null;

  return results;
}

在主页上,我放置了一个表格并将其链接到数据源,并在该表格上启用了分页功能,因此我在表格底部获得了分页器按钮,如下所示:

On the main page I put a table and linked it to the datasource, and I enabled pagination on the table, so I get the pager buttons at the bottom of the table as below:

当我执行应用程序并填充数据源时,我看到正确的首页结果,但是当我想移至下一页时,我单击下一页按钮,一旦加载完成,我发现仍然可以从表格的第一页看到相同的结果.

When I execute the app and the datasource is filled, I see the first page results in a correct way, however when I want to move to the next page, I click the next page button and once the loading is complete I find out that I still see the same results from the first page on the table.

我不熟悉如何使表格显示第二页然后是第三页的结果,而我在此圈了圈...

I am not familiar with how to make the table show the results of the second page then the third page, and I am going in circles on this...

希望说明很清楚并解决了这个问题.

Hope the explanation is clear and addresses the issue..

在此方面,我将不胜感激! 问候

I would really appreciate any help on this! Regards

推荐答案

当前,分页在计算出的数据源中无法正常工作.但是,您可以构建自己的.要完成此操作,您需要进行一些更改.首先,您需要将searchMessages函数重构为如下形式:

Currently pagination isn't working as expected with calculated datasources. You can, however, build your own. There are several changes you'll need to make to accomplish this. First you'll want to refactor your searchMessages function to something like this:

function searchMessages(userId, pageToken){

  var results = [];
  var options = {
    includeSpamTrash: "true",
    pageToken: pageToken,
    maxResults: 50
  };

    var searchResult = Gmail.Users.Messages.list(userId, options);

    for (var i = 0; i < searchResult.messages.length; i++){
      var record = app.models.emailSearchResults.newRecord();
      var msgRecord = getMessageDetails(userId,searchResult.messages[i].id);

      record.msgMainID = searchResult.messages[i].id;
      record.msgID = msgRecord[3];
      record.subject = msgRecord[2];
      record.senderAddress = msgRecord[1];
      record.msgDate = msgRecord[0];

      results.push(record);
    }

  return {records: results, nextPageToken: searchResult.nextPageToken};
}

然后,您将要更改数据源查询.您需要添加一个名为page的数字参数.

Then you'll want to change your datasource query. You'll need to add a number parameter called page.

var cache = CacheService.getUserCache();
var page = query.parameters.page || 1;
var pageToken;
if(page > 1){
  pageToken = cache.get('pageToken' + page.toString());
}

var results = searchMessages('me', pageToken);

var nextPage = (page + 1).toString();
cache.put('pageToken' + nextPage, results.nextPageToken);

return results.records;

您需要修改分页小部件的各种属性.这是上一个/下一个点击功能:

You'll need to modify the pagination widget's various attributes. Here are the previous/next click functions:

上一个:

widget.datasource.query.pageIndex--;
widget.datasource.query.parameters.page = widget.datasource.query.pageIndex;
widget.datasource.load();

下一步:

widget.datasource.query.pageIndex++;
widget.datasource.query.parameters.page = widget.datasource.query.pageIndex;
widget.datasource.load();

您应该可以从那里拿走它.

You should be able to take it from there.

这篇关于如何浏览到加载到Google AppMaker表中的数据源中的下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆