点击此处---> 群内免费提供SAP练习系统(在群公告中)
加入QQ群:457200227(SAP S4 HANA技术交流) 群内免费提供SAP练习系统(在群公告中)
树型ALV tree 实现
*&---------------------------------------------------------------------* *& REPORT ZRSD020 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT ZTEST_TREEALV. "1,画出屏幕定制控件 CONTAINER1 "2.类 变量的声明 TYPES: BEGIN OF TY_WEEK, EBELN TYPE LIKP-VBELN, POSNR TYPE LIPS-POSNR, MATNR TYPE MARA-MATNR, MENGE TYPE STRING , END OF TY_WEEK. DATA:GT_TOTAL TYPE TABLE OF TY_WEEK, GS_TOTAL TYPE TY_WEEK, GT_LIPS TYPE TABLE OF TY_WEEK, GS_LIPS TYPE TY_WEEK, GT_VBAP TYPE TABLE OF TY_WEEK, GS_VBAP TYPE TY_WEEK. DATA:OK_CODE TYPE SY-UCOMM, "获取0100屏幕触发的T-CODE GV_CODE TYPE SY-UCOMM, "系统T-CODE GR_ALV_TREE TYPE REF TO CL_GUI_ALV_TREE, "树型AKV类的引用 GR_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER . "定制容器类的引用 DATA: GT_FIELDCAT TYPE LVC_T_FCAT, GS_FIELDCAT TYPE LVC_S_FCAT. DATA: GV_NUM TYPE I VALUE 1 . "OUTPUT STRING FOR MONTH "3选择界面 "4.事件类的声明 CLASS LCL_TREE_EVENT_RECEIVER DEFINITION. PUBLIC SECTION. "定义双击节点事件触发时的处理方法 METHODS HANDLE_NODE_DCLICK FOR EVENT NODE_DOUBLE_CLICK OF CL_GUI_ALV_TREE IMPORTING NODE_KEY SENDER. "其中SENDER这个参数是一个隐式的事件参数,是由ABAP对象运行系统提供,它指向了触发这个事件的实例,可以直接使用它来调用这个实例的方法。 METHODS: ON_FUNCTION_SELECTED "获取选中行 FOR EVENT FUNCTION_SELECTED OF CL_GUI_TOOLBAR IMPORTING FCODE. METHODS:ON_TOOLBAR_DROPDOWN "标题栏 FOR EVENT DROPDOWN_CLICKED OF CL_GUI_TOOLBAR IMPORTING FCODE POSX POSY. METHODS HANDLE_ITEM_DOUBLE_CLICK "双击行项目 FOR EVENT ITEM_DOUBLE_CLICK OF CL_GUI_ALV_TREE IMPORTING FIELDNAME NODE_KEY . ENDCLASS. "5.事件类的实施 CLASS LCL_TREE_EVENT_RECEIVER IMPLEMENTATION. "定义处理方法的具体实施 METHOD HANDLE_NODE_DCLICK. DATA: LT_CHILDREN TYPE LVC_T_NKEY. *检查被点击的NODE几点下面有无子节点,有则展开节点。 CALL METHOD SENDER->GET_CHILDREN EXPORTING I_NODE_KEY = NODE_KEY IMPORTING ET_CHILDREN = LT_CHILDREN. IF NOT LT_CHILDREN IS INITIAL. CALL METHOD SENDER->EXPAND_NODE EXPORTING I_NODE_KEY = NODE_KEY I_LEVEL_COUNT = 2. ENDIF. ENDMETHOD. METHOD ON_FUNCTION_SELECTED. ENDMETHOD. METHOD HANDLE_ITEM_DOUBLE_CLICK. ENDMETHOD. "HANDLE_ITEM_DOUBLE_CLICK METHOD ON_TOOLBAR_DROPDOWN. ENDMETHOD. ENDCLASS . START-OF-SELECTION . "6.调用0100屏幕 CALL SCREEN 0100. *&---------------------------------------------------------------------* *& MODULE STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'STANDARD_FULLSCREEN'. SET TITLEBAR 'TITLE'. IF GR_ALV_TREE IS INITIAL. "判断容器类引用是否被引用 PERFORM INIT_TREE. "7,初始化ALV 字段目录 取数 节点 事件注册 CALL METHOD CL_GUI_CFW=>FLUSH "刷新ALV EXCEPTIONS CNTL_SYSTEM_ERROR = 1 CNTL_ERROR = 2. IF SY-SUBRC <> 0. CALL FUNCTION 'POPUP_TO_INFORM' EXPORTING TITEL = 'AUTOMATION QUEUE FAILURE' TXT1 = 'INTERNAL ERROR:' TXT2 = 'A METHOD IN THE AUTOMATION QUEUE' TXT3 = 'CAUSED A FAILURE.'. ENDIF. ENDIF. ENDMODULE. *&---------------------------------------------------------------------* *& MODULE USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* * TEXT *----------------------------------------------------------------------* MODULE USER_COMMAND_0100 INPUT. GV_CODE = OK_CODE. CLEAR OK_CODE. CASE GV_CODE. WHEN '&F03' OR '&F12' OR '&F15'. "当退出ALV时,释放容器内存资源,并离开程序 CALL METHOD GR_CONTAINER->FREE. LEAVE PROGRAM . WHEN OTHERS. CALL METHOD CL_GUI_CFW=>DISPATCH. "为正确调用工具栏按钮功能,必须调用该方法 ENDCASE. CALL METHOD CL_GUI_CFW=>FLUSH. ENDMODULE. *&---------------------------------------------------------------------* *& FORM INIT_TREE *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P1 TEXT *& <-- P2 TEXT *&---------------------------------------------------------------------* FORM INIT_TREE . DATA LS_HEADER_HIERARCHY TYPE TREEV_HHDR. "结构表头 CREATE OBJECT GR_CONTAINER "通过容器名称 创造容器对象 EXPORTING CONTAINER_NAME = 'CONTAINER1' EXCEPTIONS CNTL_ERROR = 1 CNTL_SYSTEM_ERROR = 2 CREATE_ERROR = 3 LIFETIME_ERROR = 4 LIFETIME_DYNPRO_DYNPRO_LINK = 5. IF SY-SUBRC <> 0. MESSAGE X208(00) WITH 'ERROR'(100). ENDIF. CREATE OBJECT GR_ALV_TREE "通过容器作为父类对象、 选择模式默认为0 创造容器对象 EXPORTING PARENT = GR_CONTAINER NODE_SELECTION_MODE = CL_GUI_COLUMN_TREE=>NODE_SEL_MODE_SINGLE ITEM_SELECTION = 'X' NO_HTML_HEADER = 'X' NO_TOOLBAR = '' EXCEPTIONS CNTL_ERROR = 1 CNTL_SYSTEM_ERROR = 2 CREATE_ERROR = 3 LIFETIME_ERROR = 4 ILLEGAL_NODE_SELECTION_MODE = 5 FAILED = 6 ILLEGAL_COLUMN_NAME = 7. IF SY-SUBRC <> 0. MESSAGE X208(00) WITH 'ERROR'. ENDIF. PERFORM FRM_HEADER_HIERARCHY CHANGING LS_HEADER_HIERARCHY. "给结构表头赋值 PERFORM BUILD_FIELDCAT. "字段 CLEAR GT_TOTAL. CALL METHOD GR_ALV_TREE->SET_TABLE_FOR_FIRST_DISPLAY "执行ALV对象展示方法 EXPORTING * I_STRUCTURE_NAME = * IS_VARIANT = * I_SAVE = 'A' * I_DEFAULT = 'X' IS_HIERARCHY_HEADER = LS_HEADER_HIERARCHY * IS_EXCEPTION_FIELD = * IT_SPECIAL_GROUPS = * IT_LIST_COMMENTARY = * I_LOGO = * I_BACKGROUND_ID = * IT_TOOLBAR_EXCLUDING = * IT_EXCEPT_QINFO = CHANGING IT_OUTTAB = GT_TOTAL "必须为空的 全局变量 * IT_FILTER = IT_FIELDCATALOG = GT_FIELDCAT . PERFORM FRM_GETDATA. "获取数据填充各节点 PERFORM REGISTER_EVENTS. "事件注册 * 更新汇总字段 CALL METHOD GR_ALV_TREE->UPDATE_CALCULATIONS. * 前端显示数据 CALL METHOD GR_ALV_TREE->FRONTEND_UPDATE. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_HEADER_HIERARCHY *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& <-- LS_HEADER_HIERARCHY *&---------------------------------------------------------------------* FORM FRM_HEADER_HIERARCHY CHANGING P_LS_HEADER_HIERARCHY TYPE TREEV_HHDR . P_LS_HEADER_HIERARCHY-HEADING = P_LS_HEADER_HIERARCHY-HEADING = '代号'. P_LS_HEADER_HIERARCHY-TOOLTIP = '节点依据'. P_LS_HEADER_HIERARCHY-WIDTH = 50. P_LS_HEADER_HIERARCHY-WIDTH_PIX = ''. ENDFORM. *&---------------------------------------------------------------------* *& FORM BUILD_FIELDCAT *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P1 TEXT *& <-- P2 TEXT *&---------------------------------------------------------------------* FORM BUILD_FIELDCAT . PERFORM FRM_FIELDCAT_BUILD USING : 'EBELN' '' '' '单据号' , 'POSNR' 'X' '' '行项目号' , 'MATNR' '' '' '物料编码' , 'MENGE' '' 'SUM' '数量' . ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_FIELDCAT_BUILD *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P_ *& --> P_ *&---------------------------------------------------------------------* FORM FRM_FIELDCAT_BUILD USING P_FIELDCAT P_NOZERO P_HFTYPE P_REPTEXT. CLEAR GS_FIELDCAT. GS_FIELDCAT-FIELDNAME = P_FIELDCAT. GS_FIELDCAT-NO_ZERO = P_NOZERO. GS_FIELDCAT-H_FTYPE = P_HFTYPE. GS_FIELDCAT-REPTEXT = GS_FIELDCAT-SCRTEXT_L = GS_FIELDCAT-SCRTEXT_M = GS_FIELDCAT-SCRTEXT_S = P_REPTEXT. APPEND GS_FIELDCAT TO GT_FIELDCAT . ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_GETDATA *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P1 TEXT *& <-- P2 TEXT *&---------------------------------------------------------------------* FORM FRM_GETDATA . DATA: LT_TOTAL TYPE TABLE OF TY_WEEK, LS_TOTAL TYPE TY_WEEK, LV_DIR_KEY TYPE LVC_NKEY, LV_OR_KEY TYPE LVC_NKEY, LV_OR_KEY1 TYPE LVC_NKEY, LV_TOP_KEY TYPE LVC_NKEY, LV_LAST_KEY TYPE LVC_NKEY . SELECT VBELN, POSNR, MATNR, FKIMG, AUBEL, AUPOS, VGBEL, VGPOS FROM VBRP INTO TABLE @DATA(LT_VBRP) UP TO 100 ROWS WHERE AUBEL <> '' AND VGBEL <> ''. IF LT_VBRP IS NOT INITIAL . SELECT VBELN AS EBELN , POSNR, MATNR, KWMENG AS MENGE FROM VBAP INTO TABLE @DATA(LT_VBAP) FOR ALL ENTRIES IN @LT_VBRP WHERE VBELN = @LT_VBRP-AUBEL AND POSNR = @LT_VBRP-AUPOS. LOOP AT LT_VBAP INTO DATA(LS_VBAP). MOVE-CORRESPONDING LS_VBAP TO GS_VBAP. APPEND GS_VBAP TO GT_VBAP. ENDLOOP. SELECT VBELN AS EBELN, POSNR, MATNR, LFIMG AS MENGE FROM LIPS INTO TABLE @DATA(LT_LIPS) FOR ALL ENTRIES IN @LT_VBRP WHERE VBELN = @LT_VBRP-VGBEL AND POSNR = @LT_VBRP-VGPOS. LOOP AT LT_LIPS INTO DATA(LS_LIPS). MOVE-CORRESPONDING LS_LIPS TO GS_LIPS. APPEND GS_LIPS TO GT_LIPS. ENDLOOP. ENDIF. CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = '' I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD * IS_OUTTAB_LINE = * IS_NODE_LAYOUT = * IT_ITEM_LAYOUT = I_NODE_TEXT = 'SO完整执行流程' IMPORTING E_NEW_NODE_KEY = LV_TOP_KEY * EXCEPTIONS * RELAT_NODE_NOT_FOUND = 1 * NODE_NOT_FOUND = 2 * OTHERS = 3 . IF SY-SUBRC <> 0. * IMPLEMENT SUITABLE ERROR HANDLING HERE ENDIF. "将上一个节点导入 得到下一个节点 LOOP AT LT_VBRP INTO DATA(LS_VBRP). PERFORM FRM_CHILDREN_NODE USING LV_TOP_KEY LS_VBRP-VBELN CHANGING LV_DIR_KEY . LOOP AT GT_LIPS INTO GS_LIPS WHERE EBELN = LS_VBRP-VGBEL AND POSNR = LS_VBRP-VGPOS. PERFORM FRM_CHILDREN_NODE_2 USING LV_DIR_KEY GS_LIPS CHANGING LV_OR_KEY . PERFORM FRM_LAST_NODE USING LV_OR_KEY GS_VBAP CHANGING LV_LAST_KEY . ENDLOOP. LOOP AT GT_VBAP INTO GS_VBAP WHERE EBELN = LS_VBRP-AUBEL AND POSNR = LS_VBRP-AUPOS. PERFORM FRM_CHILDREN_NODE_2_1 USING LV_DIR_KEY GS_LIPS CHANGING LV_OR_KEY1 . PERFORM FRM_LAST_NODE_1 USING LV_OR_KEY1 GS_VBAP CHANGING LV_LAST_KEY . ENDLOOP. * LOOP AT GT_VBAP INTO GS_VBAP WHERE EBELN = LS_VBRP-AUBEL AND POSNR = LS_VBRP-AUPOS. * PERFORM FRM_LAST_NODE USING LV_OR_KEY GS_VBAP CHANGING LV_LAST_KEY . * ENDLOOP. ENDLOOP. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_CHILDREN_NODE *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P1 TEXT *& <-- P2 TEXT *&---------------------------------------------------------------------* FORM FRM_CHILDREN_NODE USING P_LV_TOP_KEY P_VBELN CHANGING P_LV_DOR_KEY. DATA: LV_NODE_TEXT TYPE LVC_VALUE, LS_TOTAL TYPE TY_WEEK. LV_NODE_TEXT = 'BN号' && '->' && P_VBELN . CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = P_LV_TOP_KEY I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD I_NODE_TEXT = LV_NODE_TEXT IS_OUTTAB_LINE = LS_TOTAL IMPORTING E_NEW_NODE_KEY = P_LV_DOR_KEY. GV_NUM = GV_NUM + 1 . IF GV_NUM > 3 . GV_NUM = 1 . ENDIF. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_CHILDREN_NODE_2 *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> LV_DIR_KEY *& <-- LV_OR_KEY *&---------------------------------------------------------------------* FORM FRM_CHILDREN_NODE_2 USING P_LV_DIR_KEY P_LS_LIPS CHANGING P_LV_OR_KEY. DATA: LV_NODE_TEXT TYPE LVC_VALUE, LS_TOTAL TYPE TY_WEEK. LV_NODE_TEXT = 'DN号'. CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = P_LV_DIR_KEY I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD I_NODE_TEXT = LV_NODE_TEXT IS_OUTTAB_LINE = LS_TOTAL IMPORTING E_NEW_NODE_KEY = P_LV_OR_KEY. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_CHILDREN_NODE_2_1 *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> LV_DIR_KEY *& --> GS_LIPS *& <-- LV_OR_KEY *&---------------------------------------------------------------------* FORM FRM_CHILDREN_NODE_2_1 USING P_LV_DIR_KEY P_GS_LIPS CHANGING P_LV_OR_KEY. DATA: LV_NODE_TEXT TYPE LVC_VALUE, LS_TOTAL TYPE TY_WEEK. LV_NODE_TEXT = 'SO号'. CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = P_LV_DIR_KEY I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD I_NODE_TEXT = LV_NODE_TEXT IS_OUTTAB_LINE = LS_TOTAL IMPORTING E_NEW_NODE_KEY = P_LV_OR_KEY. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_LAST_NODE *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> LV_OR_KEY *& <-- LV_LAST_KEY *&---------------------------------------------------------------------* FORM FRM_LAST_NODE USING P_LV_OR_KEY P_LS_LIPS CHANGING P_LV_LAST_KEY. DATA: LV_NODE_TEXT TYPE LVC_VALUE, LS_TOTAL TYPE TY_WEEK. LV_NODE_TEXT = 'DN号'. CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = P_LV_OR_KEY I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD I_NODE_TEXT = LV_NODE_TEXT IS_OUTTAB_LINE = P_LS_LIPS IMPORTING E_NEW_NODE_KEY = P_LV_LAST_KEY. ENDFORM. *&---------------------------------------------------------------------* *& FORM FRM_LAST_NODE *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> LV_OR_KEY *& <-- LV_LAST_KEY *&---------------------------------------------------------------------* FORM FRM_LAST_NODE_1 USING P_LV_OR_KEY P_LS_VBAP CHANGING P_LV_LAST_KEY. DATA: LV_NODE_TEXT TYPE LVC_VALUE, LS_TOTAL TYPE TY_WEEK. LV_NODE_TEXT = 'SO号'. CALL METHOD GR_ALV_TREE->ADD_NODE EXPORTING I_RELAT_NODE_KEY = P_LV_OR_KEY I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD I_NODE_TEXT = LV_NODE_TEXT IS_OUTTAB_LINE = P_LS_VBAP IMPORTING E_NEW_NODE_KEY = P_LV_LAST_KEY. ENDFORM. *&---------------------------------------------------------------------* *& FORM REGISTER_EVENTS *&---------------------------------------------------------------------* *& TEXT *&---------------------------------------------------------------------* *& --> P1 TEXT *& <-- P2 TEXT *&---------------------------------------------------------------------* FORM REGISTER_EVENTS . "注册前端 后端事件 DATA: LT_EVENTS TYPE CNTL_SIMPLE_EVENTS, LS_EVENT TYPE CNTL_SIMPLE_EVENT, LR_EVENT_RECEIVER TYPE REF TO LCL_TREE_EVENT_RECEIVER. *1。获取已注册的前端事件 CALL METHOD GR_ALV_TREE->GET_REGISTERED_EVENTS IMPORTING EVENTS = LT_EVENTS. "2.添加前端双击时间 LS_EVENT-EVENTID = CL_GUI_COLUMN_TREE=>EVENTID_NODE_DOUBLE_CLICK. APPEND LS_EVENT TO LT_EVENTS. *3.重新设置前端注册时间 CALL METHOD GR_ALV_TREE->SET_REGISTERED_EVENTS "事件的注册 , 前面书写完毕的事件 EXPORTING EVENTS = LT_EVENTS EXCEPTIONS CNTL_ERROR = 1 CNTL_SYSTEM_ERROR = 2 ILLEGAL_EVENT_COMBINATION = 3. IF SY-SUBRC <> 0. MESSAGE E208(00) WITH '注册前端事件失败!'. "#EC NOTEXT ENDIF. *-------------------- "4.注册后端事件 CREATE OBJECT LR_EVENT_RECEIVER. SET HANDLER LR_EVENT_RECEIVER->HANDLE_NODE_DCLICK FOR GR_ALV_TREE. ENDFORM.