Subscribe

RSS Feed (xml)



Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Tuesday, November 16, 2010

ALV Programs

Adding PF-Status, Header and Footer in ALV using class CL_SALV

Introduction

We can develop ALV using different ways like using type pool SLIS or using the class Cl_GUI_ALV_GRID.

In this article we will see how we can add PF-Status, Header and Footer to the ALV which is developed using class CL_SALV .

Code Snippet

In this program we will see adding PF-STATUS, Header and Footer to ALV using a single class CL_SALV_TABLE.

REPORT  ZNAG_SAMPLE_01.
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_REPORT DEFINITION.
  PUBLIC SECTION.
*----------------------------------------------------------------------*
* Final Output Table
*----------------------------------------------------------------------*
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MATNR,
ERSDA TYPE ERSDA,
MATKL TYPE MATKL,
MTART TYPE MTART,
LVORM TYPE LVORM,
END OF TY_MARA.
    DATA: O_ALV TYPE REF TO CL_SALV_TABLE,              " ALV Reference
T_MARA TYPE STANDARD TABLE OF TY_MARA.
*----------------------------------------------------------------------*
* Methods to Fetch Data and Generate Output
*----------------------------------------------------------------------*
METHODS: GET_DATA, "Data Selection
GENERATE_OUTPUT. "Generating Output
  PRIVATE SECTION.
*----------------------------------------------------------------------*
* Methods to Set PF-Status, Header and Footer
*----------------------------------------------------------------------*
METHODS: SET_PF_STATUS
CHANGING
CO_ALV TYPE REF TO CL_SALV_TABLE, " Default Pf Status
             SET_TOP_OF_PAGE
CHANGING
CO_ALV TYPE REF TO CL_SALV_TABLE, " Set Top of page
             SET_END_OF_PAGE
CHANGING
CO_ALV TYPE REF TO CL_SALV_TABLE. " Set End of page
ENDCLASS.                    "lcl_report DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS LCL_REPORT IMPLEMENTATION.
*----------------------------------------------------------------------*
* Data selection
*----------------------------------------------------------------------*
METHOD GET_DATA.
SELECT MATNR ERSDA MATKL MTART LVORM INTO TABLE T_MARA
FROM MARA UP TO 20 ROWS.
ENDMETHOD. "get_data
*----------------------------------------------------------------------*
* Generating Output
*----------------------------------------------------------------------*
METHOD GENERATE_OUTPUT.
*Exception Class
DATA: LC_MSG TYPE REF TO CX_SALV_MSG.
*----------------------------------------------------------------------*
* We are calling the static Factory method which will give back
* the ALV object reference.
*----------------------------------------------------------------------*
TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY
IMPORTING
R_SALV_TABLE = O_ALV
CHANGING
T_TABLE = T_MARA.
CATCH CX_SALV_MSG INTO LC_MSG .
ENDTRY.
************************************************************************
* In this area we will call the methods which will set the
* different properties to the ALV
************************************************************************
* Calling Set PF status method
CALL METHOD SET_PF_STATUS
CHANGING
CO_ALV = O_ALV. "set_end_of_page
* Calling the top of page method
CALL METHOD SET_TOP_OF_PAGE
CHANGING
CO_ALV = O_ALV.
* Calling the End of page method
CALL METHOD SET_END_OF_PAGE
CHANGING
CO_ALV = O_ALV.
************************************************************************
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
************************************************************************
O_ALV->DISPLAY( ).
ENDMETHOD. "generate_output
************************************************************************
* In this area we will implement the methods which are defined in
* the class definition
************************************************************************
* Setting Default PF-Status
METHOD SET_PF_STATUS.
DATA: LO_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS_LIST.
* Default functions
LO_FUNCTIONS = CO_ALV->GET_FUNCTIONS( ).
LO_FUNCTIONS->SET_DEFAULT( ABAP_TRUE ).
ENDMETHOD. "set_pf_status
* Setting Top_of_page
METHOD SET_TOP_OF_PAGE.
DATA: LO_HEADER TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
LO_H_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
LO_H_FLOW TYPE REF TO CL_SALV_FORM_LAYOUT_FLOW.
* Header object
CREATE OBJECT LO_HEADER.
*----------------------------------------------------------------------*
* To create a Label or Flow we have to specify the target
* row and column number where we need to set up the output
* text.
*----------------------------------------------------------------------*
* Information in Bold
LO_H_LABEL = LO_HEADER->CREATE_LABEL( ROW = 1 COLUMN = 1 ).
LO_H_LABEL->SET_TEXT('Header of the ALV Output in Bold').
* Information in tabular format
LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 2 COLUMN = 1 ).
LO_H_FLOW->CREATE_TEXT( TEXT = 'This is text of flow in Header' ).
    LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 3 COLUMN = 1 ).
LO_H_FLOW->CREATE_TEXT( TEXT = 'Date of List Generation' ).
    LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 3 COLUMN = 2 ).
LO_H_FLOW->CREATE_TEXT( TEXT = sy-datum ).
* Set the top of list using the header for Online
CO_ALV->SET_TOP_OF_LIST( LO_HEADER ).
* Set the top of list using the header for Print
CO_ALV->SET_TOP_OF_LIST_PRINT( LO_HEADER ).
  ENDMETHOD.                    "set_top_of_page
* Setting End_Of_page
METHOD SET_END_OF_PAGE.
DATA: LO_FOOTER TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
LO_F_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
LO_F_FLOW TYPE REF TO CL_SALV_FORM_LAYOUT_FLOW.
* Footer Object
CREATE OBJECT LO_FOOTER.
* Information in Bold
LO_F_LABEL = LO_FOOTER->CREATE_LABEL( ROW = 1 COLUMN = 1 ).
LO_F_LABEL->SET_TEXT('Footer of the ALV in Bold').
* Tabular Information
LO_F_FLOW = LO_FOOTER->CREATE_FLOW( ROW = 2 COLUMN = 1 ).
LO_F_FLOW->CREATE_TEXT( TEXT = 'This is text of flow in footer' ).
* Set the end of list using the header for Online
CO_ALV->SET_END_OF_LIST( LO_FOOTER ).
* Set the End of list using the header for Print
CO_ALV->SET_END_OF_LIST_PRINT( LO_FOOTER ).
ENDMETHOD. "set_end_of_page
ENDCLASS.                    "lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*----------------------------------------------------------------------*
DATA: LO_REPORT TYPE REF TO LCL_REPORT.
  CREATE OBJECT LO_REPORT.
  LO_REPORT->GET_DATA( ).
LO_REPORT->GENERATE_OUTPUT( ).

ALV Output:

Spool Output:

Creating dynamic ALV with dynamic editable columns and dynamic colors to the columns based on condition

This Program will help to create dynamic ALV with dynamic editable columns and dynamic colors to the columns based on condition.

*&---------------------------------------------------------------------*
*& Report YSHU_SAI_ALV_DYNAMIC
*&---------------------------------------------------------------------*
REPORT YSHU_SAI_ALV_DYNAMIC.
TYPE-POOLS : ABAP.
FIELD-SYMBOLS: TYPE STANDARD TABLE,
TYPE ANY.
DATA: DYN_TABLE TYPE REF TO DATA,
DYN_LINE
TYPE REF TO DATA,
WA_FIELDCAT
TYPE LVC_S_FCAT,
IT_FIELDCAT
TYPE LVC_T_FCAT,
IT_FIELDCAT_1
TYPE LVC_T_FCAT,
OK_CODE
TYPE SY-UCOMM.
TYPES: BEGIN OF TY_1,
TIME TYPE T,
REMARKS
TYPE CHAR50,
END OF TY_1.
DATA: LV_FLD_1 TYPE TY_1.

START-
OF-SELECTION.
* Create dynamic table stricture..
PERFORM GET_TABLE_STRUCTURE.
*Create dynamic internale table..
PERFORM CREATE_ITAB_DYNAMICALLY.
* filling the data into dynamic internal table..
PERFORM GET_DATA.
END-OF-SELECTION.
* display dynamic interal data.
PERFORM DISPLAY_ALV_REPORT.

CALL SCREEN 2000.


*&---------------------------------------------------------------------*
*& Form get_table_structure
*&---------------------------------------------------------------------*
* Get structure of an SAP table
*----------------------------------------------------------------------*
FORM GET_TABLE_STRUCTURE.
DATA : REF_TABLE_DESCR TYPE REF TO CL_ABAP_STRUCTDESCR,
IT_TABDESCR
TYPE ABAP_COMPDESCR_TAB,
WA_TABDESCR
TYPE ABAP_COMPDESCR,
LV_NO_DAYS
TYPE P,
LV_DATE
TYPE SY-DATUM,
LV_DATUM
TYPE SY-DATUM,
LV_DAY
TYPE TEXT40,
LV_WEEK_DAY
TYPE TEXT10,
LIN
TYPE SY-TFILL,
C_REM
TYPE STRING VALUE 'Remarks' .
* fill the data into fieldcatlog of static field
PERFORM BUILD_FIELDCATALOG USING IT_FIELDCAT.
* Return structure of the table.
REF_TABLE_DESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( LV_FLD_1 ).
IT_TABDESCR[] = REF_TABLE_DESCR->COMPONENTS[].

* Get no of days in a month..
CALL FUNCTION 'HR_E_NUM_OF_DAYS_OF_MONTH'
EXPORTING
P_FECHA = SY-DATUM
IMPORTING
NUMBER_OF_DAYS = LV_NO_DAYS.

* get no of lines..
DESCRIBE TABLE IT_FIELDCAT LINES LIN .

LV_DATUM = SY-DATUM.
LV_DATUM+
6(2) = 1.
WA_FIELDCAT-COL_POS = LIN.
* fill dynamic field into fields cate..
DO LV_NO_DAYS TIMES.
LV_DATE = LV_DATUM + SY-
INDEX - 1.
CALL FUNCTION 'DATE_TO_DAY'
EXPORTING
DATE = LV_DATE
IMPORTING
WEEKDAY = LV_WEEK_DAY.

CONCATENATE LV_DATE+6(2) ',' LV_WEEK_DAY INTO LV_DAY.

LOOP AT IT_TABDESCR INTO WA_TABDESCR.
WA_FIELDCAT-COL_POS = WA_FIELDCAT-COL_POS +
1 .
IF SY-TABIX = 1.
WA_FIELDCAT-FIELDNAME = LV_DATE .
WA_FIELDCAT-COLTEXT = LV_DAY.
ELSE.
CONCATENATE LV_DATE '_R' INTO WA_FIELDCAT-FIELDNAME.
WA_FIELDCAT-COLTEXT = C_REM.
"'Remarks'.
ENDIF.
WA_FIELDCAT-INTTYPE = WA_TABDESCR-TYPE_KIND.
WA_FIELDCAT-INTLEN = WA_TABDESCR-LENGTH /
2.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
ENDLOOP.
ENDDO.

ENDFORM. "get_table_structure

*&---------------------------------------------------------------------*
*& Form create_itab_dynamically
*&---------------------------------------------------------------------*
* Create internal table dynamically
*----------------------------------------------------------------------*
FORM CREATE_ITAB_DYNAMICALLY.
* Create dynamic internal table and assign to Field-Symbol
CLEAR WA_FIELDCAT.
*move fields from IT_FIELDCAT into IT_FIELDCAT_1.
IT_FIELDCAT_1 = IT_FIELDCAT.
* Use ref table CALENDAR_TYPE and ref field 'COLTAB'

WA_FIELDCAT-FIELDNAME = 'CELLCOLOR'.
WA_FIELDCAT-REF_TABLE =
'CALENDAR_TYPE'.
WA_FIELDCAT-REF_FIELD =
'COLTAB'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.



* create a table type 'ZCELLSTYL' with field 'CELLSTYLE' of type LVC_T_STYL
WA_FIELDCAT-FIELDNAME =
'CELLSTYLE'.
WA_FIELDCAT-REF_TABLE =
'ZCELLSTYL'.
WA_FIELDCAT-REF_FIELD =
'CELLSTYLE'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.


*Create a dynamic table with IT_FIELDCAT.
* and Use IT_FIELDCAT_1 to display ALV.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_FIELDCAT
IMPORTING
EP_TABLE = DYN_TABLE.

ASSIGN DYN_TABLE->* TO .
* Create dynamic work area and assign to Field Symbol
CREATE DATA DYN_LINE LIKE LINE OF .
ASSIGN DYN_LINE->* TO .

ENDFORM. "create_itab_dynamically

*&---------------------------------------------------------------------*
*& Form get_data
*&---------------------------------------------------------------------*
* Populate dynamic itab
*----------------------------------------------------------------------*
FORM GET_DATA.

FIELD-SYMBOLS: TYPE ANY,
TYPE ANY,
TYPE LVC_T_SCOL,
TYPE LVC_T_STYL,
TYPE LVC_S_FCAT.

DATA: IT_CELLCOLOR TYPE LVC_T_SCOL,
IT_CELLSTYLE
TYPE LVC_T_STYL,
L_DAY_P
TYPE P,
LV_MOD_DATE
TYPE DATUM,
C_HOL
TYPE CHAR10 VALUE ' Holiday '.

ASSIGN COMPONENT 'PERNR' OF STRUCTURE TO .
=
1 .

LOOP AT IT_FIELDCAT ASSIGNING ."WHERE
* adde color to dynamci fields..
ASSIGN COMPONENT 'CELLCOLOR' OF STRUCTURE TO .
IF -INTTYPE = 'T'.
PERFORM MODIFY_CELL_COLOR USING -FIELDNAME
CHANGING IT_CELLCOLOR.
= IT_CELLCOLOR.
ELSE.

IF -FIELDNAME+8(2) = '_R'.
LV_MOD_DATE = -FIELDNAME+
0(8).
L_DAY_P = LV_MOD_DATE
MOD 7.
IF L_DAY_P > 1.
L_DAY_P = L_DAY_P -
1.
ELSE.
L_DAY_P = L_DAY_P +
6.
ENDIF.

IF L_DAY_P = 6 OR L_DAY_P = 7.
PERFORM MODIFY_CELL_COLOR USING -FIELDNAME
CHANGING IT_CELLCOLOR.
= IT_CELLCOLOR.
ASSIGN COMPONENT -FIELDNAME OF STRUCTURE TO .
= C_HOL.
"' Holiday '.
ENDIF.
ENDIF.
ENDIF.
*Dynamic editable
IF -FIELDNAME+0(8) = SY-DATUM.
ASSIGN COMPONENT 'CELLSTYLE' OF STRUCTURE TO .
PERFORM EDITABLE_CELL USING -FIELDNAME
CHANGING IT_CELLSTYLE.
= IT_CELLSTYLE.
ENDIF.
ENDLOOP.
APPEND TO .

ENDFORM. "get_data

*&---------------------------------------------------------------------*
*& Form modify_cell_color
*&---------------------------------------------------------------------*
* -->P_FIELDNAME text
* -->PT_CELLCOLOR text
*----------------------------------------------------------------------*
FORM MODIFY_CELL_COLOR USING P_FIELDNAME TYPE LVC_FNAME
CHANGING PT_CELLCOLOR TYPE TABLE.

DATA L_CELLCOLOR TYPE LVC_S_SCOL.
DATA : LV_DATE TYPE DATUM.
DATA: DAY_P TYPE P.
CLEAR L_CELLCOLOR.
IF P_FIELDNAME+8(2) = '_R'.
LV_DATE = P_FIELDNAME+
8(2).
ELSE.
LV_DATE = P_FIELDNAME.
ENDIF.

DAY_P = LV_DATE
MOD 7.

IF DAY_P > 1.
DAY_P = DAY_P -
1.
ELSE.
DAY_P = DAY_P +
6.
ENDIF.

IF DAY_P = 6 OR DAY_P = 7.
IF P_FIELDNAME+8(2) = '_R'.
L_CELLCOLOR-
COLOR-COL = 7. " Red.
L_CELLCOLOR-
COLOR-INT = 0.
L_CELLCOLOR-
COLOR-INV = 0.
ELSE.
L_CELLCOLOR-
COLOR-COL = 7. " Red.
L_CELLCOLOR-
COLOR-INT = 1.
L_CELLCOLOR-
COLOR-INV = 1.
ENDIF.
L_CELLCOLOR-FNAME = P_FIELDNAME.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.

ELSE.
CLEAR L_CELLCOLOR.
ENDIF.

IF P_FIELDNAME+0(8) = SY-DATUM.

L_CELLCOLOR-FNAME = P_FIELDNAME.
L_CELLCOLOR-
COLOR-COL = 3. " Red.
L_CELLCOLOR-
COLOR-INT = 1.
L_CELLCOLOR-
COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
CONCATENATE P_FIELDNAME '_R' INTO L_CELLCOLOR-FNAME .
L_CELLCOLOR-
COLOR-COL = 3. " Red.
L_CELLCOLOR-
COLOR-INT = 1.
L_CELLCOLOR-
COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
ENDIF.

ENDFORM. " MODIFY_CELL_COLOR

*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
* Build Fieldcatalog for ALV Report, using SAP table structure
*----------------------------------------------------------------------*
FORM BUILD_FIELDCATALOG USING P_IT_FIELDCAT TYPE LVC_T_FCAT."SLIS_T_FIELDCAT_ALV .
** ALV Function module to build field catalog from SAP table structure

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
I_STRUCTURE_NAME =
'ZDYNAMIC_ALV_STR'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
CHANGING
CT_FIELDCAT = P_IT_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.

ENDFORM. " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*& Form DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
* Display report using ALV grid
*----------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT.
DATA :G_GRID_I TYPE REF TO CL_GUI_ALV_GRID,
G_CUSTOM_CONTAINER_I
TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
I_CONTAINER
TYPE SCRFNAME VALUE 'ALV',
GS_LAYOUT
TYPE LVC_S_LAYO.

CREATE OBJECT G_CUSTOM_CONTAINER_I
EXPORTING
CONTAINER_NAME = I_CONTAINER.

CREATE OBJECT G_GRID_I
EXPORTING
I_PARENT = G_CUSTOM_CONTAINER_I.

GS_LAYOUT-COL_OPT =
'X'.
GS_LAYOUT-CWIDTH_OPT =
'X'.
GS_LAYOUT-CTAB_FNAME =
'CELLCOLOR'.
GS_LAYOUT-STYLEFNAME =
'CELLSTYLE'.

CALL METHOD G_GRID_I->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = GS_LAYOUT
* IT_TOOLBAR_EXCLUDING = LT_EXCLUDE[]
CHANGING
IT_FIELDCATALOG = IT_FIELDCAT_1
IT_OUTTAB = .
*For Editable alv...
CALL METHOD G_GRID_I->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT =
1.

ENDFORM. " DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_2000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_2000 INPUT.
CASE OK_CODE.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_2000 INPUT
*&---------------------------------------------------------------------*
*& Module STATUS_2000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_2000 OUTPUT.
SET PF-STATUS 'STATUS_2000'.
* SET TITLEBAR 'xxx'.

ENDMODULE. " STATUS_2000 OUTPUT
*&---------------------------------------------------------------------*
*& Form EDITABLE_CELL
*&---------------------------------------------------------------------** -->P_IT_CELLSTYLE text
*----------------------------------------------------------------------*
FORM EDITABLE_CELL USING P_FIELDNAME TYPE LVC_FNAME
CHANGING P_IT_CELLSTYLE TYPE LVC_T_STYL.

DATA : WA_CELLSTYLE TYPE LVC_S_STYL.
WA_CELLSTYLE-FIELDNAME = P_FIELDNAME .
WA_CELLSTYLE-STYLE = CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.
INSERT WA_CELLSTYLE INTO TABLE P_IT_CELLSTYLE.

ENDFORM. " EDITABLE_CELL

Output:

The output shows current month calendar with all the Saturday and Sundays dynamically red color will apply and for the current date yellow color with Editable fields.


Blog Archive