一段比較好的動態生成alv的例子 (轉)
&---------------------------------------------------------------------*
*& Report ZTESTDYNMICINTERNALTABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTESTDYNMICINTERNALTABLE.
*REPORT Z_BARRY_TEST_FIELDSYMOLS .
TYPE-POOLS: SLIS.
TABLES: MSEG,MKPF.
DATA: GD_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: SLA TYPE SLIS_LAYOUT_ALV ,
IVARIANT LIKE DISVARIANT,
I_REPID LIKE SY-REPID ,
I_EXCLUDING TYPE SLIS_T_EXTAB.
**define structure
TYPES: BEGIN OF TY_SUM ,
WERKS LIKE MSEG-WERKS , " Plant
MATNR LIKE MSEG-MATNR , " Material
LGORT LIKE MSEG-LGORT , " Location
BWART LIKE MSEG-BWART , " Movetype
* MENGE LIKE MSEG-MENGE , " Qty
* MENGE TYPE P DECIMALS 0 ,
MENGE TYPE P ,
END OF TY_SUM.
** 定議內表itab 來接查詢出來的資料
DATA : ITAB TYPE STANDARD TABLE OF TY_SUM WITH HEADER LINE .
** 定議雜湊內表並指明key
DATA : HS_SUM TYPE HASHED TABLE OF TY_SUM
WITH UNIQUE KEY WERKS MATNR LGORT BWART WITH HEADER LINE .
DATA : ITAB_SUM TYPE STANDARD TABLE OF TY_SUM WITH HEADER LINE .
**要把movetype 對應的數量做交叉分析
TYPES: BEGIN OF TY_BWART ,
BWART LIKE MSEG-BWART , " Movetype
END OF TY_BWART.
DATA : HS_BWART TYPE HASHED TABLE OF TY_BWART
WITH UNIQUE KEY BWART WITH HEADER LINE .
DATA : ITAB_BWART TYPE STANDARD TABLE OF TY_BWART WITH HEADER LINE .
FIELD-SYMBOLS: ,
TYPE STANDARD TABLE ,
.
DATA: MAX_COUNT TYPE I ,
DY_OUT TYPE REF TO DATA ,
DY_OUT_WA TYPE REF TO DATA,
XFC TYPE LVC_S_FCAT,
IFC TYPE LVC_T_FCAT.
DATA: C(2) TYPE N ,
TXT(20) TYPE C .
SELECT-OPTIONS: S_MBLNR FOR MSEG-MBLNR ,
S_BUDAT FOR MKPF-BUDAT .
PARAMETERS: P_SUM AS CHECKBOX .
START-OF-SELECTION.
PERFORM GETDATA. " 讀取資料
** Form FIXDATA 把資料處理成交叉表格式.
PERFORM FIXDATA.
IF P_SUM = 'X'.
PERFORM OUTDATA. " 顯示未處理前的資料傳ITAB_SUM 給ALF function
ELSE.
PERFORM OUTDATA2. " 顯示交叉表格式傳 給ALV function
ENDIF.
*&---------------------------------------------------------------------*
*& Form GETDATA
*&---------------------------------------------------------------------*
FORM GETDATA.
SELECT WERKS MATNR LGORT BWART MENGE INTO TABLE ITAB
FROM MKPF INNER JOIN MSEG ON MKPF~MBLNR = MSEG~MBLNR AND
MKPF~MJAHR = MSEG~MJAHR
WHERE MKPF~MBLNR IN S_MBLNR AND
MKPF~BUDAT IN S_BUDAT .
* SORT ITAB BY WERKS MATNR .
ENDFORM. " GETDATA
*&---------------------------------------------------------------------*
*& Form FIXDATA
*&---------------------------------------------------------------------*
FORM FIXDATA.
DATA: INDEX LIKE SY-TABIX .
LOOP AT ITAB.
HS_SUM = ITAB.
COLLECT HS_SUM.
HS_BWART = ITAB-BWART .
COLLECT HS_BWART.
ENDLOOP.
SORT HS_BWART.
ITAB_BWART[] = HS_BWART[].
ITAB_SUM[] = HS_SUM[].
PERFORM CREAT_ITAB_OUT.
**SORT
DATA NAME1(10) TYPE C.
DATA NAME2(10) TYPE C.
NAME1 = 'WERKS'.
NAME2 = 'MATNR'.
LOOP AT ITAB_SUM.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE TO .
= ITAB_SUM-WERKS.
ASSIGN COMPONENT 'MATNR' OF STRUCTURE TO .
= ITAB_SUM-MATNR.
ASSIGN COMPONENT 'LGORT' OF STRUCTURE TO .
= ITAB_SUM-LGORT.
READ TABLE ITAB_BWART WITH KEY BWART = ITAB_SUM-BWART .
INDEX = SY-TABIX + 3.
ASSIGN COMPONENT INDEX OF STRUCTURE TO .
= ITAB_SUM-MENGE.
ASSIGN COMPONENT 'LINE_SUM' OF STRUCTURE TO .
= ITAB_SUM-MENGE.
COLLECT INTO .
CLEAR.
ENDLOOP.
SORT BY (NAME1) (NAME2).
ENDFORM. " FIXDATA
*&---------------------------------------------------------------------*
*& Form outdata
*&---------------------------------------------------------------------*
FORM OUTDATA.
PERFORM FIELDCAT_INIT .
SLA-COLWIDTH_OPTIMIZE = 'X'.
SLA-ZEBRA = 'X'.
I_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = I_REPID
IT_FIELDCAT = GD_FIELDCAT[]
I_SAVE = 'A'
IS_VARIANT = IVARIANT
IS_LAYOUT = SLA
IT_EXCLUDING = I_EXCLUDING
* i_callback_user_command = 'USER_COMMAND'
TABLES
T_OUTTAB = ITAB_SUM
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. " outdata
*---------------------------------------------------------------------*
* FORM fieldcat_init *
*---------------------------------------------------------------------*
FORM FIELDCAT_INIT .
PERFORM FRM_CATLG_SET USING: 'WERKS' 'WERKS1' 'X' ,
'MATNR' 'MATNR1' 'X' ,
'LGORT' 'LGORT1' 'X' ,
'BWART' 'BWART1' '' ,
'MENGE' 'MENGE1' '' .
ENDFORM. "fieldcat_init
*---------------------------------------------------------------------*
* FORM fieldcat_init2 *
*---------------------------------------------------------------------*
FORM FIELDCAT_INIT2 .
CLEAR C .
PERFORM FRM_CATLG_SET USING: 'WERKS' 'WERKS1' 'X' ,
'MATNR' 'MATNR1' 'X' ,
'LGORT' 'LGORT1' 'X' .
LOOP AT HS_BWART .
C = C + 1.
CONCATENATE 'MENGE' C INTO TXT.
PERFORM FRM_CATLG_SET USING: TXT HS_BWART-BWART '' .
ENDLOOP.
PERFORM FRM_CATLG_SET USING: 'LINE_SUM' 'SUM' '' .
ENDFORM. "fieldcat_init
*---------------------------------------------------------------------*
* FORM frm_catlg_set *
*---------------------------------------------------------------------*
FORM FRM_CATLG_SET USING P_FIELD
P_TEXT
P_KEY.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
LS_FIELDCAT-FIELDNAME = P_FIELD.
LS_FIELDCAT-SELTEXT_L = P_TEXT.
LS_FIELDCAT-KEY = P_KEY.
LS_FIELDCAT-NO_ZERO = 'X'.
IF P_FIELD = 'LINE_SUM'.
LS_FIELDCAT-EMPHASIZE = 'C700'.
ENDIF.
IF P_FIELD = 'LINE_SUM'.
LS_FIELDCAT-DECIMALS_OUT = '0'.
ENDIF.
APPEND LS_FIELDCAT TO GD_FIELDCAT .
CLEAR LS_FIELDCAT .
ENDFORM. "frm_catlg_set
*&--------------------------------------------------------------------*
*& Form user_command
*&--------------------------------------------------------------------*
*FORM user_command USING r_ucomm LIKE sy-ucomm
* rs_selfield TYPE slis_selfield.
* READ TABLE itab INDEX rs_selfield-tabindex.
* CASE r_ucomm.
* WHEN '&IC1'. "雙擊
*
* WHEN 'EXIT'.
* LEAVE PROGRAM.
* ENDCASE.
** rs_selfield-refresh = 'X'. "自動重新整理
*ENDFORM. "user_com
*---------------------------------------------------------------------*
* FORM outdata2 *
*---------------------------------------------------------------------*
FORM OUTDATA2.
PERFORM FIELDCAT_INIT2 .
SLA-COLWIDTH_OPTIMIZE = 'X'.
SLA-ZEBRA = 'X'.
I_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = I_REPID
IT_FIELDCAT = GD_FIELDCAT[]
I_SAVE = 'A'
IS_VARIANT = IVARIANT
IS_LAYOUT = SLA
IT_EXCLUDING = I_EXCLUDING
TABLES
T_OUTTAB =
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. " outdata
*---------------------------------------------------------------------*
* FORM creat_itab_out *
*---------------------------------------------------------------------*
FORM CREAT_ITAB_OUT.
CLEAR C.
PERFORM BUILD_ITAB_STRU USING: 'WERKS' 'MSEG' 'WERKS' ,
'MATNR' 'MSEG' 'MATNR' ,
'LGORT' 'MSEG' 'LGORT' .
LOOP AT HS_BWART.
C = C + 1.
CONCATENATE 'MENGE' C INTO TXT.
PERFORM BUILD_ITAB_STRU USING: TXT 'MSEG' 'MENGE' .
ENDLOOP.
PERFORM BUILD_ITAB_STRU USING: 'LINE_SUM' 'MSEG' 'MENGE' .
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IFC
IMPORTING
EP_TABLE = DY_OUT.
ASSIGN DY_OUT->* TO.
CREATE DATA DY_OUT_WA LIKE LINE OF.
ASSIGN DY_OUT_WA->* TO.
ENDFORM.
*---------------------------------------------------------------------*
* FORM build_itab_stru *
*---------------------------------------------------------------------*
FORM BUILD_ITAB_STRU USING FDNAME REF_T REF_F.
XFC-FIELDNAME = FDNAME .
XFC-REF_TABLE = REF_T.
XFC-REF_FIELD = REF_F.
APPEND XFC TO IFC.
ENDFORM.
*& Report ZTESTDYNMICINTERNALTABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZTESTDYNMICINTERNALTABLE.
*REPORT Z_BARRY_TEST_FIELDSYMOLS .
TYPE-POOLS: SLIS.
TABLES: MSEG,MKPF.
DATA: GD_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: SLA TYPE SLIS_LAYOUT_ALV ,
IVARIANT LIKE DISVARIANT,
I_REPID LIKE SY-REPID ,
I_EXCLUDING TYPE SLIS_T_EXTAB.
**define structure
TYPES: BEGIN OF TY_SUM ,
WERKS LIKE MSEG-WERKS , " Plant
MATNR LIKE MSEG-MATNR , " Material
LGORT LIKE MSEG-LGORT , " Location
BWART LIKE MSEG-BWART , " Movetype
* MENGE LIKE MSEG-MENGE , " Qty
* MENGE TYPE P DECIMALS 0 ,
MENGE TYPE P ,
END OF TY_SUM.
** 定議內表itab 來接查詢出來的資料
DATA : ITAB TYPE STANDARD TABLE OF TY_SUM WITH HEADER LINE .
** 定議雜湊內表並指明key
DATA : HS_SUM TYPE HASHED TABLE OF TY_SUM
WITH UNIQUE KEY WERKS MATNR LGORT BWART WITH HEADER LINE .
DATA : ITAB_SUM TYPE STANDARD TABLE OF TY_SUM WITH HEADER LINE .
**要把movetype 對應的數量做交叉分析
TYPES: BEGIN OF TY_BWART ,
BWART LIKE MSEG-BWART , " Movetype
END OF TY_BWART.
DATA : HS_BWART TYPE HASHED TABLE OF TY_BWART
WITH UNIQUE KEY BWART WITH HEADER LINE .
DATA : ITAB_BWART TYPE STANDARD TABLE OF TY_BWART WITH HEADER LINE .
FIELD-SYMBOLS:
DATA: MAX_COUNT TYPE I ,
DY_OUT TYPE REF TO DATA ,
DY_OUT_WA TYPE REF TO DATA,
XFC TYPE LVC_S_FCAT,
IFC TYPE LVC_T_FCAT.
DATA: C(2) TYPE N ,
TXT(20) TYPE C .
SELECT-OPTIONS: S_MBLNR FOR MSEG-MBLNR ,
S_BUDAT FOR MKPF-BUDAT .
PARAMETERS: P_SUM AS CHECKBOX .
START-OF-SELECTION.
PERFORM GETDATA. " 讀取資料
** Form FIXDATA 把資料處理成交叉表格式.
PERFORM FIXDATA.
IF P_SUM = 'X'.
PERFORM OUTDATA. " 顯示未處理前的資料傳ITAB_SUM 給ALF function
ELSE.
PERFORM OUTDATA2. " 顯示交叉表格式傳
ENDIF.
*&---------------------------------------------------------------------*
*& Form GETDATA
*&---------------------------------------------------------------------*
FORM GETDATA.
SELECT WERKS MATNR LGORT BWART MENGE INTO TABLE ITAB
FROM MKPF INNER JOIN MSEG ON MKPF~MBLNR = MSEG~MBLNR AND
MKPF~MJAHR = MSEG~MJAHR
WHERE MKPF~MBLNR IN S_MBLNR AND
MKPF~BUDAT IN S_BUDAT .
* SORT ITAB BY WERKS MATNR .
ENDFORM. " GETDATA
*&---------------------------------------------------------------------*
*& Form FIXDATA
*&---------------------------------------------------------------------*
FORM FIXDATA.
DATA: INDEX LIKE SY-TABIX .
LOOP AT ITAB.
HS_SUM = ITAB.
COLLECT HS_SUM.
HS_BWART = ITAB-BWART .
COLLECT HS_BWART.
ENDLOOP.
SORT HS_BWART.
ITAB_BWART[] = HS_BWART[].
ITAB_SUM[] = HS_SUM[].
PERFORM CREAT_ITAB_OUT.
**SORT
DATA NAME1(10) TYPE C.
DATA NAME2(10) TYPE C.
NAME1 = 'WERKS'.
NAME2 = 'MATNR'.
LOOP AT ITAB_SUM.
ASSIGN COMPONENT 'WERKS' OF STRUCTURE
ASSIGN COMPONENT 'MATNR' OF STRUCTURE
ASSIGN COMPONENT 'LGORT' OF STRUCTURE
READ TABLE ITAB_BWART WITH KEY BWART = ITAB_SUM-BWART .
INDEX = SY-TABIX + 3.
ASSIGN COMPONENT INDEX OF STRUCTURE
ASSIGN COMPONENT 'LINE_SUM' OF STRUCTURE
COLLECT
CLEAR
ENDLOOP.
SORT
ENDFORM. " FIXDATA
*&---------------------------------------------------------------------*
*& Form outdata
*&---------------------------------------------------------------------*
FORM OUTDATA.
PERFORM FIELDCAT_INIT .
SLA-COLWIDTH_OPTIMIZE = 'X'.
SLA-ZEBRA = 'X'.
I_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = I_REPID
IT_FIELDCAT = GD_FIELDCAT[]
I_SAVE = 'A'
IS_VARIANT = IVARIANT
IS_LAYOUT = SLA
IT_EXCLUDING = I_EXCLUDING
* i_callback_user_command = 'USER_COMMAND'
TABLES
T_OUTTAB = ITAB_SUM
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. " outdata
*---------------------------------------------------------------------*
* FORM fieldcat_init *
*---------------------------------------------------------------------*
FORM FIELDCAT_INIT .
PERFORM FRM_CATLG_SET USING: 'WERKS' 'WERKS1' 'X' ,
'MATNR' 'MATNR1' 'X' ,
'LGORT' 'LGORT1' 'X' ,
'BWART' 'BWART1' '' ,
'MENGE' 'MENGE1' '' .
ENDFORM. "fieldcat_init
*---------------------------------------------------------------------*
* FORM fieldcat_init2 *
*---------------------------------------------------------------------*
FORM FIELDCAT_INIT2 .
CLEAR C .
PERFORM FRM_CATLG_SET USING: 'WERKS' 'WERKS1' 'X' ,
'MATNR' 'MATNR1' 'X' ,
'LGORT' 'LGORT1' 'X' .
LOOP AT HS_BWART .
C = C + 1.
CONCATENATE 'MENGE' C INTO TXT.
PERFORM FRM_CATLG_SET USING: TXT HS_BWART-BWART '' .
ENDLOOP.
PERFORM FRM_CATLG_SET USING: 'LINE_SUM' 'SUM' '' .
ENDFORM. "fieldcat_init
*---------------------------------------------------------------------*
* FORM frm_catlg_set *
*---------------------------------------------------------------------*
FORM FRM_CATLG_SET USING P_FIELD
P_TEXT
P_KEY.
DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
LS_FIELDCAT-FIELDNAME = P_FIELD.
LS_FIELDCAT-SELTEXT_L = P_TEXT.
LS_FIELDCAT-KEY = P_KEY.
LS_FIELDCAT-NO_ZERO = 'X'.
IF P_FIELD = 'LINE_SUM'.
LS_FIELDCAT-EMPHASIZE = 'C700'.
ENDIF.
IF P_FIELD = 'LINE_SUM'.
LS_FIELDCAT-DECIMALS_OUT = '0'.
ENDIF.
APPEND LS_FIELDCAT TO GD_FIELDCAT .
CLEAR LS_FIELDCAT .
ENDFORM. "frm_catlg_set
*&--------------------------------------------------------------------*
*& Form user_command
*&--------------------------------------------------------------------*
*FORM user_command USING r_ucomm LIKE sy-ucomm
* rs_selfield TYPE slis_selfield.
* READ TABLE itab INDEX rs_selfield-tabindex.
* CASE r_ucomm.
* WHEN '&IC1'. "雙擊
*
* WHEN 'EXIT'.
* LEAVE PROGRAM.
* ENDCASE.
** rs_selfield-refresh = 'X'. "自動重新整理
*ENDFORM. "user_com
*---------------------------------------------------------------------*
* FORM outdata2 *
*---------------------------------------------------------------------*
FORM OUTDATA2.
PERFORM FIELDCAT_INIT2 .
SLA-COLWIDTH_OPTIMIZE = 'X'.
SLA-ZEBRA = 'X'.
I_REPID = SY-REPID.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = I_REPID
IT_FIELDCAT = GD_FIELDCAT[]
I_SAVE = 'A'
IS_VARIANT = IVARIANT
IS_LAYOUT = SLA
IT_EXCLUDING = I_EXCLUDING
TABLES
T_OUTTAB =
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM. " outdata
*---------------------------------------------------------------------*
* FORM creat_itab_out *
*---------------------------------------------------------------------*
FORM CREAT_ITAB_OUT.
CLEAR C.
PERFORM BUILD_ITAB_STRU USING: 'WERKS' 'MSEG' 'WERKS' ,
'MATNR' 'MSEG' 'MATNR' ,
'LGORT' 'MSEG' 'LGORT' .
LOOP AT HS_BWART.
C = C + 1.
CONCATENATE 'MENGE' C INTO TXT.
PERFORM BUILD_ITAB_STRU USING: TXT 'MSEG' 'MENGE' .
ENDLOOP.
PERFORM BUILD_ITAB_STRU USING: 'LINE_SUM' 'MSEG' 'MENGE' .
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IFC
IMPORTING
EP_TABLE = DY_OUT.
ASSIGN DY_OUT->* TO
CREATE DATA DY_OUT_WA LIKE LINE OF
ASSIGN DY_OUT_WA->* TO
ENDFORM.
*---------------------------------------------------------------------*
* FORM build_itab_stru *
*---------------------------------------------------------------------*
FORM BUILD_ITAB_STRU USING FDNAME REF_T REF_F.
XFC-FIELDNAME = FDNAME .
XFC-REF_TABLE = REF_T.
XFC-REF_FIELD = REF_F.
APPEND XFC TO IFC.
ENDFORM.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16794144/viewspace-743315/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 靜態路由和動態路由的比較路由
- 動態企業建模DEM與ERP的比較(轉)
- 動態內表及動態ALV顯示(轉)
- Go和Python比較的話,哪個比較好?GoPython
- shell動態指令碼和pl/sql動態指令碼的比較指令碼SQL
- 別人搭建的比較好的框架框架
- 前端面試比較好的回答前端面試
- 全網比較好的 Markdown 教程
- 一些比較好的網站網站
- 一個比較好的shell指令碼指令碼
- 一個比較好的oracle blogOracle
- alv動態顯示列
- 使用動態變數進行動態資料比較變數
- Java動態代理 jdk和cglib的實現比較JavaJDKCGLib
- 動態的實現任意表的ALV顯示02
- 動態的實現任意表的ALV顯示01
- 有哪些比較好的外匯平臺?
- 關閉資料庫比較好的方法資料庫
- GML、SVG、VML的比較 (轉)SVG
- 比較perl模組的版本(轉)
- 流行語言的比較 (轉)
- flutter仿boss直聘,一個比較完整的例子(一)Flutter
- 網站設計:“瀑布流”比較好 還是“下一頁”比較好?網站
- 幾個動態代理Proxy工具效能比較
- Apache與Nginx的優缺點、效能比較,到底選擇哪個比較好?ApacheNginx
- golang的比較好的一些超連結Golang
- 北京比較好的Java培訓班有哪些Java
- 什麼牌子的高階遊戲本比較好?遊戲
- Hibernate與 MyBatis的比較(轉)MyBatis
- DELPHI的萬用字元比較 (轉)字元
- 獲取檔案字尾名的比較好的方法
- 找到的比較好的工作面試題筆試題面試題筆試
- 一段讀取資料表的例子
- 深入理解Java物件例項生成的例子!(轉)Java物件
- 哪款軟體的資源管理功能比較好?
- 2W左右的遊戲本哪款比較好?遊戲
- asp.net core 比較好的開源專案ASP.NET
- ASP.NET 與 JAVA 的比較 (轉)ASP.NETJava