用alv实现
时间: 2024-02-17 07:03:13 浏览: 132
abap 自定义alv的一个小例子
以下是一个简单的 ABAP 代码示例,使用 ALV 技术实现物料数据报表的显示,包括物料号、物料描述、批次号和库存数量。
```
REPORT z_material_report_alv.
TABLES: mara, mard.
DATA: BEGIN OF itab OCCURS 0,
matnr TYPE mara-matnr,
maktx TYPE mara-maktx,
charg TYPE mard-charg,
labst TYPE mard-labst,
END OF itab.
DATA: wa_layout TYPE lvc_s_layo,
wa_fieldcat TYPE lvc_s_fcat.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
SELECT mara~matnr
mara~maktx
mard~charg
mard~labst
FROM mara
JOIN mard
ON mara~matnr = mard~matnr
INTO TABLE itab
WHERE mara~matnr IN s_matnr.
IF sy-subrc = 0.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_top_of_page = 'TOP-OF-PAGE'
it_fieldcat = wa_fieldcat
i_save = 'A'
TABLES
t_outtab = itab
EXCEPTIONS
program_error = 1
OTHERS = 2.
ELSE.
WRITE: / '没有符合条件的数据。'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form TOP-OF-PAGE
*&---------------------------------------------------------------------*
* 输出表格标题
*----------------------------------------------------------------------*
FORM top_of_page.
DATA: wa_columns TYPE lvc_s_col,
it_columns TYPE lvc_t_scol.
CLEAR wa_columns.
wa_columns-col_pos = 1.
wa_columns-fieldname = 'MATNR'.
wa_columns-ref_fieldname = 'MATNR'.
wa_columns-tabname = 'ITAB'.
wa_columns-outputlen = 18.
APPEND wa_columns TO it_columns.
CLEAR wa_columns.
wa_columns-col_pos = 2.
wa_columns-fieldname = 'MAKTX'.
wa_columns-ref_fieldname = 'MAKTX'.
wa_columns-tabname = 'ITAB'.
wa_columns-outputlen = 40.
APPEND wa_columns TO it_columns.
CLEAR wa_columns.
wa_columns-col_pos = 3.
wa_columns-fieldname = 'CHARG'.
wa_columns-ref_fieldname = 'CHARG'.
wa_columns-tabname = 'ITAB'.
wa_columns-outputlen = 15.
APPEND wa_columns TO it_columns.
CLEAR wa_columns.
wa_columns-col_pos = 4.
wa_columns-fieldname = 'LABST'.
wa_columns-ref_fieldname = 'LABST'.
wa_columns-tabname = 'ITAB'.
wa_columns-outputlen = 15.
APPEND wa_columns TO it_columns.
wa_layout-grid_title = '物料数据报表'.
wa_layout-zebra = 'X'.
wa_layout-no_key = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_top_of_page = ' '
it_fieldcat = it_columns
is_layout = wa_layout
TABLES
t_outtab = itab
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. "top_of_page
```
这段代码与前面的示例类似,不同之处在于使用了 REUSE_ALV_GRID_DISPLAY 函数来显示数据。首先定义了一个内部表 itab,然后使用 SELECT 语句从 MARA 和 MARD 表中检索所需的数据,并将其存储在内部表中。接着,定义了一个 ALV 表格布局 wa_layout 和字段目录 wa_fieldcat,用于设置表格的显示格式和字段属性。最后,在 TOP-OF-PAGE 事件中使用 REUSE_ALV_GRID_DISPLAY 函数来显示数据,并设置表格标题、斑马线效果和无键选项等。
阅读全文