Vous êtes sur la page 1sur 4

*&---------------------------------------------------------------------*

*& Report Z496_ALV_SAMPLE2


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT Z496_ALV_SAMPLE2.
*data definition
TYPE-POOLS: SLIS. " Contains data declaration for slis.
tables:marav. "Table MARA and table MAKT
*---------------------------------------------------------------------*
* Data to be displayed in ALV
* Using the following syntax, REUSE_ALV_FIELDCATALOG_MERGE can auto* matically determine the fieldstructure from this source program
TYPES:
begin of ty_imat,
matnr like marav-matnr, "Material number
maktx like marav-maktx, "Material short text
matkl like marav-matkl, "Material group
ntgew like marav-ntgew, "Net weight, numeric field
gewei like marav-gewei, "weight unit
end of ty_imat.
DATA:imat TYPE STANDARD TABLE OF ty_imat.
*---------------------------------------------------------------------*
* field to check table length
data i_lines like sy-tabix.
data i_repid like sy-repid.
*---------------------------------------------------------------------*
* Data for ALV display & internal tab n workarea for field catalog
data int_fcat type SLIS_T_FIELDCAT_ALV.
data: wa_fcat TYPE slis_fieldcat_alv.
*---------------------------------------------------------------------*
select-options:
s_matnr for marav-matnr matchcode object MAT1.
*---------------------------------------------------------------------*
start-of-selection.
* read data into table imat
select * from marav
into corresponding fields of table imat
where
matnr in s_matnr.
* Check if material was found
clear i_lines.
describe table imat lines i_lines.
if i_lines lt 1.
* Using hardcoded write here for easy upload
write: /
'No materials found.'.
exit.
endif.

end-of-selection.
*---------------------------------------------------------------------*
*
* Now, we start with ALV
*
*---------------------------------------------------------------------*
*
*
* To use ALV, we need a DDIC-structure or a thing called Fieldcatalogue.
* The fieldcatalouge can be generated by FUNCTION
* 'REUSE_ALV_FIELDCATALOG_MERGE' from an internal table from any
* report source, including this report.
* The only problem one might have is that the report and table names
* need to be in capital letters.
*
*
*---------------------------------------------------------------------*
*
*This was the fieldcatlogue
PERFORM buildcatalog.
** Store report name
* i_repid = sy-repid.
*
** Create Fieldcatalogue from internal table
* CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
*
EXPORTING
*
I_PROGRAM_NAME
= i_repid
*
I_INTERNAL_TABNAME
= 'IMAT' "capital letters!
*
I_INCLNAME
= i_repid
*
CHANGING
*
CT_FIELDCAT
= int_fcat
*
EXCEPTIONS
*
INCONSISTENT_INTERFACE = 1
*
PROGRAM_ERROR
= 2
*
OTHERS
= 3.
**explanations:
**
I_PROGRAM_NAME is the program which calls this function
**
**
I_INTERNAL_TABNAME is the name of the internal table which you want
**
to display in ALV
**
**
I_INCLNAME is the ABAP-source where the internal table is defined
**
(DATA....)
**
CT_FIELDCAT contains the Fieldcatalouge that we need later for
**
ALV display
*
*
* IF SY-SUBRC <> 0.
*
write: /
*
'Returncode',
*
sy-subrc,
*
'from FUNCTION REUSE_ALV_FIELDCATALOG_MERGE'.
* ENDIF.
*---------------------------------------------------------------------*
*

* Call for ALV list display


CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING

I_CALLBACK_PROGRAM
IT_FIELDCAT
=
I_SAVE
TABLES
T_OUTTAB
EXCEPTIONS
PROGRAM_ERROR
OTHERS

= i_repid
int_fcat
= 'A'
= imat
= 1
= 2.

*
*explanations:
*
I_CALLBACK_PROGRAM is the program which calls this function
*
*
IT_FIELDCAT
*
*
I_SAVE allows the user to save his own layouts
*
*
T_OUTTAB contains the data to be displayed in ALV
IF SY-SUBRC <> 0.
write: /
'Returncode',
sy-subrc,
'from FUNCTION REUSE_ALV_LIST_DISPLAY'.
ENDIF.
*
*&---------------------------------------------------------------------*
*&
Form buildcatalog
*&---------------------------------------------------------------------*
*
Another way of building catalog
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
form buildcatalog .
wa_fcat-fieldname = 'MATNR'.
wa_fcat-seltext_m = 'No.'.
APPEND wa_fcat TO int_fcat.
wa_fcat-fieldname = 'MAKTX'.
wa_fcat-seltext_m = 'Desc.'.
APPEND wa_fcat TO int_fcat.

" Fieldname in the data table


" Column description in the output

wa_fcat-fieldname = 'MATKL'.
wa_fcat-seltext_m = 'Group'.
APPEND wa_fcat TO int_fcat.
wa_fcat-fieldname = 'NTGEW'.
wa_fcat-seltext_m = 'Weight'.
APPEND wa_fcat TO int_fcat.
wa_fcat-fieldname = 'GEWEI'.
wa_fcat-seltext_m = 'Unit'.
APPEND wa_fcat TO int_fcat.

endform.

" buildcatalog

Vous aimerez peut-être aussi