Netweaver Gateway Development

how to implement Client-Side paging in SAP OData service?

              In mobile devices space to display results in list is very limited.Also scrolling is very boring.There is no point in getting large set of data here.It also reduces payload therefore increase performance.
             OData developers can make use of query options $top and $skip for client-side paging.These two query options are used together in almost every situation.$top means how many entries you need to get from backend.$skip means how many number of entries to be skipped.$top and $skip query options allows us to add paging to result set of GET_ENTITYSET implementation.
In this tutorial we will use GET_ENTITYSET method from our earlier tutorial mentioned above.

Syntax:
    https://<server>:<port>/.../<service_name>/Products?$top=5&$skip=5



Steps

  1. Open our project and expand service implementation node and right click on GetEntitySet(Query) and click on Goto ABAP Workbench.
  2. Open our FLIGHTSET_GET_ENTITYSET method.Inside this method paste below code.Structure is_paging at runtime contain $top and $skip values.
  3.     data:lt_entityset  TYPE ZCL_ZTEST_ODATA_01_MPC=>TT_FLIGHT,
             ls_entity     like LINE OF lt_entityset,
             lv_table_size TYPE i.
        data:lv_top  TYPE int4,
             lv_skip TYPE int4,
             lv_max  TYPE int4.
        lv_top = is_paging-top.
        lv_skip = is_paging-skip.
        lv_max = lv_top + lv_skip.   
    
  4. Fetch data based on our variable lv_max which is sum of values $top and $skip.
  5.  SELECT * FROM SCARR UP TO lv_max ROWS INTO CORRESPONDING FIELDS OF TABLE lt_entityset ORDER BY carrid.   
    
  6. Apply the logic for Client-Side paging.
  7.     IF lv_top IS NOT INITIAL OR
           lv_skip IS NOT INITIAL.
          LOOP AT lt_entityset INTO ls_entity.
            IF sy-tabix > lv_skip.
              APPEND ls_entity TO et_entityset.
              DESCRIBE TABLE et_entityset LINES lv_table_size.
              IF lv_top IS NOT INITIAL AND
                 lv_table_size >= lv_top.
                EXIT.
              ENDIF.
            ENDIF.
          ENDLOOP.
          "No Paging
        ELSE.
          et_entityset = lt_entityset.
        ENDIF.   
    

  8. Now we can test  in gateway client( /IWFND/GW_CLIENT).Our service url look like this.http://<host>:<port>/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet?$top=1&$skip=1.Press execute button.


Note:You can eliminate the LOOP if you use below code instead.
    IF lv_top IS NOT INITIAL OR
       lv_skip IS NOT INITIAL.
       Delete lt_entityset from 0 to lv_skip.
       et_entityset = lt_entityset.
      "No Paging
    ELSE.
      et_entityset = lt_entityset.
    ENDIF.   
You can also access $top and $skip values using below method.

If you enjoyed this post, Please Share!!

10 comments :

  1. I went through many documentations. However, your explanation gave me a clear idea..
    Still I have a question open as I am not from ABAP background.
    Can you tell me how we conclude that lv_max = lv_top + lv_skip

    ReplyDelete
    Replies
    1. this is just simple math.If we need 3 records from 6th to 9th row of a table.(here skip = 6 and top = 3).then we have to fetch 9 records and delete first 6 rows right?That is why i have used lv_max = lv_top + lv_skip(here 9 = 6 + 3).
      Hope you understand my logic.
      Regards

      Delete
    2. Got it.. Thanks..
      Also in continuation to your response, I put the code in debug mode to understand in depth and it is clear now.

      I have one more query which would be an extension of this tutorial. If you can give me a url which explains it like you did with this blog, that would be really great. (I am novice in ABAP, so please consider it accordingly). My question is:

      How can I combine $filter, $skip and $top logic together in a GET_ENTITYSET method?

      Delete
    3. For this use below url and make use of external break point.
      http://:/sap/opu/odata/SAP/ZTEST_ODATA_SRV_01/FlightSet?$top=3&$skip=3&$filter=Carrid eq 'AC' and Carrname eq 'yourValue'
      Search 'odata url conventions' to learn how to use OData urls.See when combining usage of & sign in above url.

      Delete
  2. About statement -> IF sy-tabix > lv_skip -> in a loop .

    I prefer to delete before loop at once

    Delete lt_entityset from 0 to lv_skip .

    ReplyDelete
    Replies
    1. Thanks for the suggestion.This eliminate the use of loop and it can improve performance.right?I will update the blog

      Delete
  3. i tried to replace the loop, restricting records based on $top & $skip, with a direct delete on internal table
    if lv_skip ge 1.
    delete lt_entityset from 1 to lv_skip.
    delete lt_entityset from ( lv_top + lv_skip ).
    elseif lv_top ge 1.
    delete lt_entityset from ( lv_top + 1 ).
    endif.

    ReplyDelete
    Replies
    1. Thanks for sharing code.This will be helpful for someone 👍

      Delete
  4. I think you can replace the loop, with following statement:
    IF lv_skip GE 1.
    DELETE et_entityset FROM 1 TO lv_skip.
    ENDIF.
    IF lv_top GE 1.
    DELETE et_entityset FROM ( lv_top + 1 ).
    ENDIF.

    ReplyDelete
    Replies
    1. Yes you are right.Replacing loop will improve performance.We should avoid loops.I wrote this blog in my early learning stages.
      Thanks for commenting Georgi Petrov

      Delete

Powered by Blogger.