上一节学习了$top, $skip, and $inlinecount,这一节学习的$orderby的使用。
更详细学习请参看:SAP Press – OData and SAP NetWeaver Gateway
ODATA学习(SAP Press – OData and SAP NetWeaver Gateway)第6.5.5章节,第280页
The $orderby query option allows you to define the sorting of your
result set. Order -by parameters are provided by the framework in your
Get_EntitySet method via the import table I T_ORDER. The table contains
an entry for each property provided in the $orderby clause.
一、地址http://bwnep:8917/sap/opu/odata/sap/ZSALE_SRV/ZTSALE_H?$orderby=Zmemid,Zsaleid,中传入的orderby 参数在Get_EntitySet类中的传入参数IT_ORDER中,有的英文的文档又说存在io_tech_request_context中,如下取得,得我使用的是IT_ORDER参数。
1 2 |
DATA: lt_tech_order TYPE /iwbep/t_mgw_tech_order. lt_tech_order = io_tech_request_context->get_orderby( ). |
ODATA 中的Get_EntitySet方法基础实现,包含ORDER BY的使用,可参数SAP标准例子:https://help.sap.com/saphelp_gateway20sp08/helpdata/en/71/d07b52a3566f54e10000000a44176d/content.htm
二、在使用中,由于不确认传入需要排序的字段是几个,及传排序的字段名是什么,为了能用,所以新建立了一个通用的对内表进行排序的方法ZCL_ODATA_PUBLIC->ORDERBY。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
* ET_ORDER TYPE /IWBEP/T_MGW_SORTING_ORDER * IT_DATA TYPE STANDARD TABLE DATA: LT_OTAB TYPE ABAP_SORTORDER_TAB, LS_OTAB TYPE LINE OF ABAP_SORTORDER_TAB, LS_OLINE TYPE ABAP_SORTORDER. DATA: LS_ORDER LIKE LINE OF ET_ORDER. CONSTANTS:BEGIN OF LCS_SORTING_ORDER, DESCENDING TYPE STRING VALUE 'desc', ASCENDING TYPE STRING VALUE 'asc', END OF LCS_SORTING_ORDER. LOOP AT ET_ORDER INTO LS_ORDER. LS_OTAB-NAME = LS_ORDER-PROPERTY. IF LS_ORDER-ORDER = LCS_SORTING_ORDER-DESCENDING. LS_OTAB-DESCENDING = ABAP_TRUE. ELSE. LS_OTAB-DESCENDING = ABAP_FALSE. ENDIF. APPEND LS_OTAB TO LT_OTAB. ENDLOOP. SORT IT_DATA BY (LT_OTAB). ENDMETHOD. |
三、在[size=13.3333px]Get_EntitySet方法的最后增加入下调用,就能进行排序了,
1 2 3 4 5 6 7 8 9 10 11 |
DATA: ZCL_ODATA_PUBLIC TYPE REF TO ZODATA_PUBLIC. IF IT_ORDER IS NOT INITIAL. CREATE OBJECT ZCL_ODATA_PUBLIC. CALL METHOD ZCL_ODATA_PUBLIC->ORDERBY EXPORTING ET_ORDER = IT_ORDER CHANGING IT_DATA = ET_ENTITYSET . ENDIF. |
更多的查询选项请参看$select , $filter , $top , $skip , $inlinecount , and$orderby。