之前写过一篇简单的ALV显示信息,为了增强对ALV几个查关函数的理解,故又写了一个很简单的小程式就叫他为之简单的ALV显示信息(二)吧。就显示出物料编号及物料名称,够简单吧,我喜欢简单,就像这样过着简单的生活。
REPORT ZZWEI_ALV_MYDEMO.
TYPE-POOLS:SLIS. "引用类型池
TABLES:MARA,MAKT.
DATA:i_fieldcat TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
i_layout TYPE SLIS_LAYOUT_ALV,
i_repid TYPE SY-REPID.
SELECT-OPTIONS:s_matnr FOR MARA-MATNR OBLIGATORY. "选择屏幕,选择物料编号进行查询,必须填写
DATA imatnr LIKE MARA-MATNR.
DATA:BEGIN OF imara OCCURS 0, "存储物料信息的内表
MATNR LIKE MARA-MATNR,
MAKTX LIKE MAKT-MAKTX,
END OF imara.
*&--------------------------------------------------------------
*& Start of screen
*&--------------------------------------------------------------
START-OF-SELECTION.
PERFORM get_Data.
PERFORM fieldcat_Build.
PERFORM layout_Build.
PERFORM alv_Display.
*&--------------------------------------------------------------
*& Get Material Data
*&--------------------------------------------------------------
FORM get_Data.
SELECT * FROM MARA WHERE MATNR IN s_matnr.
imatnr = MARA-MATNR.
SELECT SINGLE * FROM MAKT WHERE MATNR EQ imatnr.
IF SY-SUBRC = 0.
imara-MATNR = MARA-MATNR.
imara-MAKTX = MAKT-MAKTX.
APPEND imara.
ENDIF.
ENDSELECT.
ENDFORM.
*&--------------------------------------------------------------
*& FieldCat Build
*&--------------------------------------------------------------
FORM fieldCat_Build.
i_repid = SY-REPID.
CLEAR i_fieldcat.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = i_repid
"I_STRUCTURE_NAME = 'IMARA'
I_INTERNAL_TABNAME = 'IMARA' "按内表结构返回FIELDCAT
I_INCLNAME = i_repid "传递程序名
CHANGING
CT_FIELDCAT = i_fieldcat[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* LOOP AT i_fieldcat.
* IF i_fieldcat-FIELDNAME = 'MATNR'.
* i_fieldcat-NO_OUT = 'X'. "如果从内表中生成FIELDCAT,而ALV显示出来不需要某个字段,可以将其隐藏
* MODIFY i_fieldcat.
* ENDIF.
* ENDLOOP.
* i_fieldcat-COL_POS = 1.
* i_fieldcat-SELTEXT_M = '物料編號'.
* i_fieldcat-FIELDNAME = 'MATNR'.
* APPEND i_fieldcat.
*
* i_fieldcat-COL_POS = 2.
* i_fieldcat-SELTEXT_M = '物料名稱'.
* i_fieldcat-FIELDNAME = 'MAKTX'.
* APPEND i_fieldcat.
ENDFORM.
*&--------------------------------------------------------------
*& Layout Build
*&--------------------------------------------------------------
FORM layout_Build.
i_layout-ZEBRA = 'X'.
"i_layout-DETAIL_TITLEBAR = '詳細內容'.
ENDFORM.
*&--------------------------------------------------------------
*& Display ALV
*&--------------------------------------------------------------
FORM alv_Display.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = i_repid
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
IS_LAYOUT = i_layout
IT_FIELDCAT = i_fieldcat[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = imara
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM.
就这样写完后按CTRL+F2进行语法检查没有问题(心里的喜悦不言面喻),那么,按F8执行程序吧。秀一下界面。
程式执行查询后ALV显示出数据,如上图。
革命尚未成功,同志(我)还需努力呀!今天喝了点家乡绿茶,感觉挺好就是肚子有点饿了。保存闪人。