Visual Builder

Get Involved. Join the Conversation.

Topic

    Guy Lior
    List Pagination with custom REST endpointAnswered
    Topic posted September 24, 2019 by Guy LiorBronze Medal: 1,250+ Points, tagged Custom Code, Database, Mobile, REST, Services 
    42 Views, 4 Comments
    Title:
    List Pagination with custom REST endpoint
    Summary:
    List Pagination with custom REST endpoint
    Content:

    Hi,

    I have custom REST API that support limit and offset parameters for pagination.

    How to implement list pagination with SDP and custom REST endpoint using the transform javascript function?

    Do I have to implement also list scrolling event ?

    Any working example or document?

    Thanks

    Best Comment

    Duncan Mills

    Here's a transform that does this - in this case the backend service supports URL parameters called limit and offset to manage paging. As you can see, all that needs to happen in the transform code is to append those values based on the information being fed in via the options.

        Request.prototype.paginate = function(configuration, options) {
          var newUrl = configuration.url;
          newUrl = newUrl + '&limit=' + options.size + '&offset=' + options.offset;
          configuration.url = newUrl;
          return configuration;
        };
    

    On top of this you'll need to make sure that you have a response transform as well so that you can let the data provider know that there are still more rows that could be obtained if required. In my case the body of the response happens to contain both the total number of rows that *could* be returned and a flag called hasMore that tells us if we've reached the end of the data set yet.

        Response.prototype.paginate = function(result) {
          var resultSetInfo = {};
          if (result.body) {
            resultSetInfo.totalSize = result.body.totalResults;
            resultSetInfo.hasMore = result.body.hasMore
          }
          return resultSetInfo;
        };
    

    Comment

     

    • Duncan Mills

      Here's a transform that does this - in this case the backend service supports URL parameters called limit and offset to manage paging. As you can see, all that needs to happen in the transform code is to append those values based on the information being fed in via the options.

          Request.prototype.paginate = function(configuration, options) {
            var newUrl = configuration.url;
            newUrl = newUrl + '&limit=' + options.size + '&offset=' + options.offset;
            configuration.url = newUrl;
            return configuration;
          };
      

      On top of this you'll need to make sure that you have a response transform as well so that you can let the data provider know that there are still more rows that could be obtained if required. In my case the body of the response happens to contain both the total number of rows that *could* be returned and a flag called hasMore that tells us if we've reached the end of the data set yet.

          Response.prototype.paginate = function(result) {
            var resultSetInfo = {};
            if (result.body) {
              resultSetInfo.totalSize = result.body.totalResults;
              resultSetInfo.hasMore = result.body.hasMore
            }
            return resultSetInfo;
          };
      
    • Guy Lior

      Thanks Duncan.

      It works great!

      • Duncan Mills

        Excellent!

        • Guy Lior

          Hi,

          Since the new version of VBCS (19.3.1.2) I am getting the following error in version that worked before:

          Error: getConfig() for "getMachonGetAll" is missing required parameters: "offset", "limit"

          Any idea?

          Thanks