Wednesday, April 18, 2007

Another help for ABAPpers

Hey you can use this template for ALV



use "REUSE_ALV_GRID_DISPLAY" function to show itab below.
I calculated Z fields as X/Y*100
Customer X Y Z
1 1 2 50
2 3 4 75
3 2 5 40
4 1 10 10

When I set grid to show totals by customer It works like that.

Customer X Y Z
1 1 2 50
2 3 4 75
3 2 5 40
4 1 10 10
TOTAL 7 21 175






--------------------------------------------------------------------------------

*&---------------------------------------------------------------------*
*& Report ZALVTEST1 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT ZALVTEST1 .


TYPE-POOLS: SLIS.
Data: v_temp type p decimals 2.
Data: v_f1_sum type i,
v_f2_sum type i.
dATA: BEGIN OF struct_customer,
CUSTOMER LIKE KNA1-KUNNR,
FIELD1 TYPE I,
FIELD2 TYPE I,
field3 type i,
END OF struct_customer.

DATA: I_LAYOUT TYPE SLIS_LAYOUT_ALV,
I_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV,
I_HEADING TYPE SLIS_T_LISTHEADER,
I_EVENTS TYPE SLIS_T_EVENT.

DATA: RS_SORT TYPE SLIS_T_SORTINFO_ALV.

DATA: V_CUST TYPE KNA1-KUNNR.
DATA: BEGIN OF ITAB OCCURS 0,
CUSTOMER LIKE KNA1-KUNNR,
FIELD1 TYPE I,
FIELD2 TYPE I,
field3 type i,
END OF ITAB.
DATA: BEGIN OF ITAB1 OCCURS 0,
CUSTOMER LIKE KNA1-KUNNR,
FIELD1 TYPE I,
FIELD2 TYPE I,
field3 type i,
END OF ITAB1.
DATA: FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'.
DATA: FORMNAME_SUBTOTAL_TEXT TYPE SLIS_FORMNAME VALUE 'SUBTOTAL_TEXT'.
DATA: FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE'.
DATA: FORMNAME_END_OF_LIST TYPE SLIS_FORMNAME VALUE 'END_OF_LIST'.

DATA: FORMNAME_AFTER_LINE_OUTPUT TYPE SLIS_FORMNAME VALUE 'LINE_OUTPUT'.


START-OF-SELECTION.

PERFORM FILL_ITAB.
PERFORM FILL_CAT.
PERFORM SET_SORT.
PERFORM FILL_EVENT.
PERFORM DISPLAY_DATA.

END-OF-SELECTION.





*&---------------------------------------------------------------------*
*& Form FILL_ITAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM FILL_ITAB . DATA: L_CTR TYPE I. ITAB-CUSTOMER = 1.ITAB-FIELD1 = 1.ITAB-FIELD2 = 2.APPEND ITAB. CLEAR ITAB. ITAB-CUSTOMER = 2.ITAB-FIELD1 = 3.ITAB-FIELD2 = 4.APPEND ITAB. CLEAR ITAB. ITAB-CUSTOMER = 3.ITAB-FIELD1 = 2.ITAB-FIELD2 = 5.APPEND ITAB. CLEAR ITAB. ITAB-CUSTOMER = 4.ITAB-FIELD1 = 1.ITAB-FIELD2 = 10.APPEND ITAB. CLEAR ITAB. LOOP AT ITAB. v_temp = itab-field1 / itab-field2. itab-field3 = v_temp * 100. modify itab. V_f1_sum = v_f1_sum + itab-field1. V_f2_sum = v_f2_sum + itab-field2. ENDLOOP. ENDFORM. " FILL_ITAB *&---------------------------------------------------------------------* *& Form FILL_CAT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM FILL_CAT . DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV. CLEAR L_FIELDCAT. L_FIELDCAT-COL_POS = 1. L_FIELDCAT-TABNAME = 'ITAB'. L_FIELDCAT-FIELDNAME = 'CUSTOMER'. L_FIELDCAT-REF_TABNAME = 'KNA1'. L_FIELDCAT-REF_FIELDNAME = 'KUNNR'. L_FIELDCAT-KEY = 'X'. APPEND L_FIELDCAT TO I_FIELDTAB. CLEAR L_FIELDCAT. CLEAR L_FIELDCAT. L_FIELDCAT-COL_POS = 3. L_FIELDCAT-TABNAME = 'ITAB'. L_FIELDCAT-FIELDNAME = 'FIELD1'. * L_FIELDCAT-DO_SUM = 'X'. L_FIELDCAT-No_zero = 'X'. L_FIELDCAT-seltext_l ='VALUE1'. L_FIELDCAT-seltext_M ='VALUE1'. L_FIELDCAT-seltext_S ='VALUE1'. * L_FIELDCAT-KEY = 'X'. APPEND L_FIELDCAT TO I_FIELDTAB. CLEAR L_FIELDCAT. L_FIELDCAT-COL_POS = 4. L_FIELDCAT-TABNAME = 'ITAB'. L_FIELDCAT-FIELDNAME = 'FIELD2'. * L_FIELDCAT-DO_SUM = 'X'. L_FIELDCAT-No_zero = 'X'. L_FIELDCAT-seltext_l ='VALUE2'. L_FIELDCAT-seltext_M ='VALUE2'. L_FIELDCAT-seltext_S ='VALUE2'. * L_FIELDCAT-KEY = 'X'. APPEND L_FIELDCAT TO I_FIELDTAB. CLEAR L_FIELDCAT. L_FIELDCAT-COL_POS = 5. L_FIELDCAT-TABNAME = 'ITAB'. L_FIELDCAT-FIELDNAME = 'FIELD3'. * L_FIELDCAT-DO_SUM = 'X'. L_FIELDCAT-No_zero = 'X'. L_FIELDCAT-seltext_l ='VALUE3'. L_FIELDCAT-seltext_M ='VALUE3'. L_FIELDCAT-seltext_S ='VALUE3'. APPEND L_FIELDCAT TO I_FIELDTAB. ENDFORM. " FILL_CAT *&---------------------------------------------------------------------* *& Form DISPLAY_DATA *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM DISPLAY_DATA . CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = 'ZALVTEST2' IS_LAYOUT = I_LAYOUT IT_FIELDCAT = I_FIELDTAB I_DEFAULT = 'X' I_SAVE = ' ' IT_EVENTS = I_EVENTS[] TABLES T_OUTTAB = ITAB * 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. " DISPLAY_DATA
*&---------------------------------------------------------------------*
*& Form SET_SORT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM SET_SORT . I_LAYOUT-ZEBRA = 'X'. DATA: LS_SORT TYPE SLIS_SORTINFO_ALV. CLEAR LS_SORT. LS_SORT-SPOS = 1. LS_SORT-FIELDNAME = 'CUSTOMER'. LS_SORT-TABNAME = 'ITAB'. LS_SORT-UP = 'X'. APPEND LS_SORT TO RS_SORT. ENDFORM. " SET_SORT *&---------------------------------------------------------------------* *& Form FILL_EVENT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text
* <-- p2 text *----------------------------------------------------------------------* FORM FILL_EVENT . DATA: L_I_EVENT TYPE SLIS_ALV_EVENT. CALL FUNCTION 'REUSE_ALV_EVENTS_GET' EXPORTING I_LIST_TYPE = 0 IMPORTING ET_EVENTS = I_EVENTS. READ TABLE I_EVENTS WITH KEY NAME = SLIS_EV_AFTER_LINE_OUTPUT INTO L_I_EVENT. IF SY-SUBRC = 0. MOVE FORMNAME_AFTER_LINE_OUTPUT TO L_I_EVENT-FORM. MODIFY I_EVENTS FROM L_I_EVENT INDEX SY-TABIX. ENDIF. ENDFORM. " FILL_EVENT *&---------------------------------------------------------------------* *& Form AFTER_LINE_OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* FORM LINE_OUTPUT using RS_LINEINFO TYPE SLIS_LINEINFO. DATA: L_TABIX LIKE SY-TABIX. DATA: L_TABIX1 LIKE SY-TABIX. data: str_kunnr like struct_customer. check RS_LINEINFO-tabindex ne 0. L_TABIX = RS_LINEINFO-tabindex. read table itab index l_tabix. l_tabix1 = RS_LINEINFO-tabindex + 1. struct_customer-customer = itab-customer. read table itab into str_kunnr index l_tabix1 transporting customer. if sy-subrc <> 0.

v_temp = ( v_f1_sum / v_f2_sum ) * 100.

uline at (45).

skip.

uline at (45).

write: / sy-vline , 'Total',
12 sy-vline , (9) v_f1_sum,
23 sy-vline , (9) v_f2_sum,
34 sy-vline , (9) v_temp decimals 0,
45 sy-vline.

*uline at (46).

endif.

ENDFORM. " AFTER_LINE_OUTPUT

1 comment:

  1. I downloaded this program and modified it to enable it to run in a Sandbox region. The main corrections were in FORM DISPLAY_DATA.

    Following is the entire corrected program :
    REPORT ZALVTEST1 .


    TYPE-POOLS: SLIS.
    Data: v_temp type p decimals 2.
    Data: v_f1_sum type i,
    v_f2_sum type i.
    dATA: BEGIN OF struct_customer,
    CUSTOMER LIKE KNA1-KUNNR,
    FIELD1 TYPE I,
    FIELD2 TYPE I,
    field3 type i,
    END OF struct_customer.

    DATA: I_LAYOUT TYPE SLIS_LAYOUT_ALV,
    I_FIELDTAB TYPE SLIS_T_FIELDCAT_ALV,
    I_HEADING TYPE SLIS_T_LISTHEADER,
    I_EVENTS TYPE SLIS_T_EVENT.

    DATA: RS_SORT TYPE SLIS_T_SORTINFO_ALV.

    DATA: V_CUST TYPE KNA1-KUNNR.
    DATA: BEGIN OF ITAB OCCURS 0,
    CUSTOMER LIKE KNA1-KUNNR,
    FIELD1 TYPE I,
    FIELD2 TYPE I,
    field3 type i,
    END OF ITAB.
    DATA: BEGIN OF ITAB1 OCCURS 0,
    CUSTOMER LIKE KNA1-KUNNR,
    FIELD1 TYPE I,
    FIELD2 TYPE I,
    field3 type i,
    END OF ITAB1.
    DATA: FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE'.
    DATA: FORMNAME_SUBTOTAL_TEXT TYPE SLIS_FORMNAME VALUE 'SUBTOTAL_TEXT'.
    DATA: FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE'.
    DATA: FORMNAME_END_OF_LIST TYPE SLIS_FORMNAME VALUE 'END_OF_LIST'.

    DATA: FORMNAME_AFTER_LINE_OUTPUT TYPE SLIS_FORMNAME VALUE 'LINE_OUTPUT'.


    START-OF-SELECTION.

    PERFORM FILL_ITAB.
    PERFORM FILL_CAT.
    PERFORM SET_SORT.
    PERFORM FILL_EVENT.
    PERFORM DISPLAY_DATA.

    END-OF-SELECTION.





    *&---------------------------------------------------------------------*
    *& Form FILL_ITAB
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text *--------------------------------------------------------*
    FORM FILL_ITAB .
    DATA: L_CTR TYPE I.
    ITAB-CUSTOMER = 1.
    ITAB-FIELD1 = 1.
    ITAB-FIELD2 = 2.
    APPEND ITAB.
    CLEAR ITAB.
    ITAB-CUSTOMER = 2.
    ITAB-FIELD1 = 3. ITAB-FIELD2 = 4.
    APPEND ITAB.
    CLEAR ITAB. ITAB-CUSTOMER = 3.ITAB-FIELD1 = 2.
    ITAB-FIELD2 = 5.APPEND ITAB. CLEAR ITAB. ITAB-CUSTOMER = 4.
    ITAB-FIELD1 = 1. ITAB-FIELD2 = 10. APPEND ITAB.
    CLEAR ITAB.

    LOOP AT ITAB.
    v_temp = itab-field1 / itab-field2.
    itab-field3 = v_temp * 100.
    modify itab. V_f1_sum = v_f1_sum + itab-field1.
    V_f2_sum = v_f2_sum + itab-field2.
    ENDLOOP.
    ENDFORM. " FILL_ITAB

    *&---------------------------------------------------------------------*
    *& Form FILL_CAT *&----------------------------------------------------*
    * text *---------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text *--------------------------------------------------------*
    FORM FILL_CAT .
    DATA: L_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
    CLEAR L_FIELDCAT.
    L_FIELDCAT-COL_POS = 1.
    L_FIELDCAT-TABNAME = 'ITAB'.
    L_FIELDCAT-FIELDNAME = 'CUSTOMER'.
    L_FIELDCAT-REF_TABNAME = 'KNA1'.
    L_FIELDCAT-REF_FIELDNAME = 'KUNNR'.
    L_FIELDCAT-KEY = 'X'.
    APPEND L_FIELDCAT TO I_FIELDTAB.
    CLEAR L_FIELDCAT.
    CLEAR L_FIELDCAT.
    L_FIELDCAT-COL_POS = 3.
    L_FIELDCAT-TABNAME = 'ITAB'.
    L_FIELDCAT-FIELDNAME = 'FIELD1'.
    L_FIELDCAT-DO_SUM = 'X'.
    L_FIELDCAT-No_zero = 'X'.
    L_FIELDCAT-seltext_l ='VALUE1'.
    L_FIELDCAT-seltext_M ='VALUE1'.
    L_FIELDCAT-seltext_S ='VALUE1'.
    L_FIELDCAT-KEY = 'X'.
    APPEND L_FIELDCAT TO I_FIELDTAB. CLEAR L_FIELDCAT.
    L_FIELDCAT-COL_POS = 4. L_FIELDCAT-TABNAME = 'ITAB'.
    L_FIELDCAT-FIELDNAME = 'FIELD2'.
    L_FIELDCAT-DO_SUM = 'X'.
    L_FIELDCAT-No_zero = 'X'. L_FIELDCAT-seltext_l ='VALUE2'.
    L_FIELDCAT-seltext_M ='VALUE2'. L_FIELDCAT-seltext_S ='VALUE2'.
    L_FIELDCAT-KEY = 'X'.
    APPEND L_FIELDCAT TO I_FIELDTAB. CLEAR L_FIELDCAT.
    L_FIELDCAT-COL_POS = 5. L_FIELDCAT-TABNAME = 'ITAB'.
    L_FIELDCAT-FIELDNAME = 'FIELD3'.
    L_FIELDCAT-DO_SUM = 'X'.
    L_FIELDCAT-No_zero = 'X'. L_FIELDCAT-seltext_l ='VALUE3'.
    L_FIELDCAT-seltext_M ='VALUE3'.
    L_FIELDCAT-seltext_S ='VALUE3'.
    APPEND L_FIELDCAT TO I_FIELDTAB.
    ENDFORM. " FILL_CAT

    *&---------------------------------------------------------------------*
    *& Form DISPLAY_DATA *&------------------------------------------------*
    * text *---------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text *--------------------------------------------------------*
    FORM DISPLAY_DATA .
    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING I_CALLBACK_PROGRAM = 'ZALVTEST2'
    IS_LAYOUT = I_LAYOUT
    IT_FIELDCAT = I_FIELDTAB
    I_DEFAULT = 'X'
    I_SAVE = ' '
    IT_EVENTS = I_EVENTS[]
    TABLES T_OUTTAB = ITAB.

    * EXCEPTIONS *
    * PROGRAM_ERROR = itab. " 1
    * OTHERS = itab. " 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. " DISPLAY_DATA

    *&---------------------------------------------------------------------*
    *& Form SET_SORT
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text *--------------------------------------------------------*
    FORM SET_SORT .
    I_LAYOUT-ZEBRA = 'X'.
    DATA: LS_SORT TYPE SLIS_SORTINFO_ALV.
    CLEAR LS_SORT. LS_SORT-SPOS = 1. LS_SORT-FIELDNAME = 'CUSTOMER'.
    LS_SORT-TABNAME = 'ITAB'. LS_SORT-UP = 'X'.
    APPEND LS_SORT TO RS_SORT.
    ENDFORM. " SET_SORT

    *&---------------------------------------------------------------------*
    *& Form FILL_EVENT
    *&---------------------------------------------------------------------*
    * text *---------------------------------------------------------------*
    * --> p1 text
    * <-- p2 text *--------------------------------------------------------*
    FORM FILL_EVENT .
    DATA: L_I_EVENT TYPE SLIS_ALV_EVENT.
    CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING I_LIST_TYPE = 0
    IMPORTING ET_EVENTS = I_EVENTS.
    READ
    TABLE I_EVENTS WITH KEY NAME = SLIS_EV_AFTER_LINE_OUTPUT INTO L_I_EVENT.
    IF SY-SUBRC = 0.
    MOVE FORMNAME_AFTER_LINE_OUTPUT TO L_I_EVENT-FORM.
    MODIFY I_EVENTS FROM L_I_EVENT INDEX SY-TABIX.
    ENDIF.
    ENDFORM. " FILL_EVENT

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

    *& Form AFTER_LINE_OUTPUT
    *&---------------------------------------------------------------------*
    * text
    *----------------------------------------------------------------------*
    FORM LINE_OUTPUT using RS_LINEINFO TYPE SLIS_LINEINFO.
    DATA: L_TABIX LIKE SY-TABIX.
    DATA: L_TABIX1 LIKE SY-TABIX.
    data: str_kunnr like struct_customer.
    check RS_LINEINFO-tabindex ne 0.
    L_TABIX = RS_LINEINFO-tabindex.
    read table itab index l_tabix.
    l_tabix1 = RS_LINEINFO-tabindex + 1.
    struct_customer-customer = itab-customer.
    read table itab into str_kunnr index l_tabix1 transporting customer.

    if sy-subrc <> 0.

    v_temp = ( v_f1_sum / v_f2_sum ) * 100.

    uline at (45).

    skip.

    uline at (45).

    write: / sy-vline , 'Total',
    12 sy-vline , (9) v_f1_sum,
    23 sy-vline , (9) v_f2_sum,
    34 sy-vline , (9) v_temp decimals 0,
    45 sy-vline.

    uline at (46).

    endif.

    ReplyDelete

Featured Post

Domain and blog for Sale