Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

ALV using objects

former_member842213
Participant
0 Kudos

hi,

Im using the class which is mentioned below,

1)i need to display the top_of_page and end_of_page how can i do that?,

2)how could i give user defiined column headings.

for eg:for the first column ,the coumn heading is sales document number.

REPORT ZALV_SAMPLE.

types:begin of ty_final,

vbeln type vbeln_va,

matnr type matnr,

end of ty_final.

DATA:IT_FINAL TYPE TABLE OF TY_FINAL.

select vbeln matnr

FROM VBAP

INTO TABLE IT_FINAL.

********************this is the code for alv (OO concept)

DATA : R_TABLE TYPE REF TO CL_SALV_TABLE,

R_COLUMNS TYPE REF TO CL_SALV_COLUMNS,

R_COLUMN TYPE REF TO CL_SALV_COLUMN.

TRY.

CALL METHOD cl_salv_table=>factory

IMPORTING

R_SALV_TABLE = R_TABLE

CHANGING

t_table = IT_final.

.

CATCH CX_SALV_MSG .

ENDTRY.

R_COLUMNS = R_TABLE->GET_COLUMNS( ).

R_TABLE->DISPLAY( ).

3 REPLIES 3

Former Member
0 Kudos

Maybe refering to the perfect tutorial by Serdar Simsekler <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/an%20easy%20reference%20for%20alv%20grid%20control.pdf">"An Easy Reference for ALV Grid Control"</a> will be helpfull.

Regards,

Renata

Former Member
0 Kudos

Hi

Solution ->

1) <b>Use standard Events in the Class - SET_TABLE_FOR_FIRST_DISPLAY</b>

2)<b>To give user defiined column headings.</b>

for eg:for the first column ,the coumn heading is sales document number. Use text-elements or 'Hard code text' in the report.

<b>NOTE -> In the code below, Instead of Sample text 1, 2, 3... you can type ur text.</b>

----


Sample Code -

*&---------------------------------------------------------------------*
*& Report  Z_ATUL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

report  z_atul no standard page heading .

************************************************************************
*              D A T A B A S E   T A B L E S                           *
************************************************************************

tables :
    vbrk,      "Billing Document: Header Data
    vbrp,      "Billing Document: Item Data
    vbak,      "Sales Document: Header Data
    cskt,      "Cost Center Master Data
    bseg.      "Accounting Document Segment (USING FOR G/L Line Items)

types:

* TYPE Used to capture the records to be displayed
begin of ty_list,
vtweg  like vbrk-vtweg,
spart  like vbrk-spart,
hkont like bseg-hkont,
fkart  like vbrk-fkart,
fkdat  like vbrk-fkdat,
erdat  like vbrk-erdat,
ernam  like vbrk-ernam,
rfbsk  like vbrk-rfbsk,
aedat  like vbrk-aedat,
vbeln  like vbrk-vbeln,
bukrs  like vbrk-bukrs,
kunag  like vbrk-kunag,
aktnr  like vbrp-aktnr,
aubel  like vbrp-aubel,
ktext like  cskt-ktext,
kostl like  cskt-kostl,
end of ty_list,


begin of ty_vbrk,
  vbeln like vbrk-vbeln,
  bukrs like vbrk-bukrs,
  fkart like vbrk-fkart,
  fkdat like vbrk-fkdat,
  ernam like vbrk-ernam,
  erdat like vbrk-erdat,
  kunag like vbrk-kunag,
  aedat like vbrk-aedat,
  gjahr like vbrk-gjahr,
end of ty_vbrk,

begin of ty_vbrp,
  fkart like vbrk-fkart,
  bukrs like vbrk-bukrs,
  ktext like cskt-ktext,
  vbeln like vbrk-vbeln,
  kunag like vbrk-kunag,
  fkdat like vbrk-fkdat,
  ernam like vbrk-ernam,
  erdat like vbrk-erdat,
  aedat like vbrk-aedat,
  aktnr like vbrp-aktnr,
  aubel like vbrp-aubel,
  sonam like vbak-ernam,
  posnr  like vbrp-posnr ,
  netwr like vbrp-netwr,
  rfbsk  like vbrk-rfbsk,
end of ty_vbrp,

begin of ty_vbak,
vbeln like vbak-vbeln,
erdat like vbak-erdat,
ernam like vbak-ernam,

end of ty_vbak,

begin of ty_cskt,
kostl like cskt-kostl,
ktext like cskt-ktext,
end of ty_cskt,

begin of ty_bseg,
belnr like bseg-belnr,
koart like bseg-koart,
buzid like bseg-buzid,
hkont like bseg-hkont,
shkzg like bseg-shkzg,
end of ty_bseg.


data :

ts_list type standard table of ty_list,
ts_vbrk type standard table of ty_vbrk,
ts_vbrp type standard table of ty_vbrp,
ts_vbak type standard table of ty_vbak,
ts_cskt type standard table of ty_cskt,

ts_bseg type standard table of ty_bseg.


data:

wa_list like line of ts_list,
wa_vbrk like line of ts_vbrk,
wa_vbrp like line of ts_vbrp,
wa_vbak like line of ts_vbak,
wa_cskt like line of ts_cskt,
*WA_ZT308 LIKE LINE OF TS_ZT308,
wa_bseg like line of ts_bseg.



************************************************************************
*              ALV GRID COMPONENTS
************************************************************************
data : ok_code like sy-ucomm,
       g_container type scrfname value 'Z361_GRID_CONTAINER' ,
       grid type ref to cl_gui_alv_grid,
       g_custom_container type ref to cl_gui_custom_container,
       gs_layout type lvc_s_layo,
       gt_fieldcatalog type lvc_t_fcat,
       gs_print type lvc_s_prnt.
data : st_fieldcat type lvc_t_fcat .

*&---------------------------------------------------------------------*
*&       Class LCL_EVENT_RECEIVER
*&---------------------------------------------------------------------*
class lcl_event_receiver definition.

  public section.

    methods :
    t_double_click
    for event double_click of cl_gui_alv_grid
    importing e_row e_column.

endclass.               "LCL_EVENT_RECEIVER


class lcl_event_receiver definition deferred.

data : g_event_rec type ref to lcl_event_receiver.

*selection for user input
*First block
selection-screen begin of block sales with frame title text-009.

select-options s_vkorg for vbrk-vkorg.
select-options s_vtweg for vbrk-vtweg.
select-options s_spart for vbrk-spart.
select-options s_fkart for vbrk-fkart.
select-options s_fkdat for vbrk-fkdat.

select-options s_erdat for vbrk-erdat.
select-options s_ernam for vbrk-ernam.
select-options s_aktnr for vbrp-aktnr.
select-options s_aubel for vbrp-aubel.

select-options s_rfbsk for vbrk-rfbsk.
select-options s_aedat for vbrk-aedat.
select-options s_kunag for vbrk-kunag.
select-options s_vbeln for vbrk-vbeln.

selection-screen end of block sales.
*Second block
selection-screen begin of block output with frame title text-008.
parameter : p_vari type slis_vari.
selection-screen end of block output.

***********************************************************************
**** INITIALIZATION
***********************************************************************

***********************************************************************
**** AT SELECTION SCREEN OUTPUT
***********************************************************************
at selection-screen on end of s_vbeln .

  if       s_vkorg[] is initial
    and    s_vtweg[] is initial
    and    s_spart[] is initial
    and    s_fkart[] is initial
    and    s_fkdat[] is initial

    and    s_erdat[]  is initial
    and    s_ernam[]  is initial
    and    s_aktnr[] is initial
    and    s_aubel[] is initial

    and    s_rfbsk[] is initial
    and    s_aedat[]   is initial
    and    s_kunag[]    is initial
    and    s_vbeln[] is initial .
    message 'Please enter some selection criteria' type 'E'.
  endif.

***********************************************************************
**** START OF SELECTION
***********************************************************************

start-of-selection.

  perform get_data.




***********************************************************************
**** END OF SELECTION
***********************************************************************
*IF TS_LIST[] IS INITIAL.

  if ts_vbrp[] is initial.  "TEMP TO BE DELETED
    message 'No data found for the selection.' type 'S'.
    exit.
  else.
    call screen 1001.
  endif.

*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_data .

 select k~fkart k~fkdat k~erdat k~ernam k~rfbsk k~aedat k~vbeln k~bukrs
 k~kunag
 p~aktnr p~aubel p~posnr
 p~netwr a~ernam
 from  ( vbrk as k
 inner join vbrp  as p
       on  p~vbeln = k~vbeln
 inner join vbak  as a
       on  a~vbeln = p~aubel )
 into corresponding fields of table ts_vbrp
 where
       k~vkorg in s_vkorg
       and k~vtweg in s_vtweg
       and k~spart in s_spart
       and k~fkart in s_fkart
       and k~fkdat in s_fkdat
       and k~erdat in s_erdat
       and k~ernam in s_ernam
       and k~rfbsk in s_rfbsk
       and k~aedat in s_aedat
       and k~vbeln in s_vbeln
       and k~kunag in s_kunag

       and p~aktnr in s_aktnr
       and p~aubel in s_aubel.


* Transferring Billing docu. no. & Company code to internal table

  loop at ts_vbrp into wa_vbrp.

    move    wa_vbrp-vbeln to wa_vbrk-vbeln.
    move    wa_vbrp-bukrs to wa_vbrk-bukrs.

    append wa_vbrk to ts_vbrk.

  endloop.

  sort ts_vbrk by vbeln bukrs.

  delete adjacent duplicates from ts_vbrk comparing vbeln bukrs.

  sort ts_vbrk by fkart ascending vbeln bukrs descending.

endform.                    " get_data

*&---------------------------------------------------------------------*
*&       Class (Implementation)  LCL_EVENT_RECEIVER
*&---------------------------------------------------------------------*

class lcl_event_receiver implementation.

  method t_double_click.
    message 'Double click successful' type 'I' .
  endmethod.                    "t_double_click

endclass.               "LCL_EVENT_RECEIVER

*&---------------------------------------------------------------------*
*&      Form  create_and_display_container
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form create_and_display_container .

*Create custom container control for our ALV Control
  create object g_custom_container
     exporting
        container_name = g_container
     exceptions
        cntl_error = 1
        cntl_system_error = 2
        create_error = 3
        lifetime_error = 4
        lifetime_dynpro_dynpro_link = 5.

*Create instance of alv control
  create object grid
     exporting
       i_parent = g_custom_container.

*Create instance of event-->double click
  create object g_event_rec.
  set handler g_event_rec->t_double_click for grid.


*Set a titlebar for the grid control
  gs_layout-grid_title = 'Supplier Funding Billing Documents'.

*Return field descriptions and rename as report titles
  data: lt_fieldcat type lvc_t_fcat.
  perform set_fieldcatalog_init using lt_fieldcat.
  gs_layout-zebra = 'X'.
  st_fieldcat = lt_fieldcat.

*Display container
  call method grid->set_table_for_first_display
    exporting
      is_print        = gs_print
      is_layout       = gs_layout
    changing
      it_outtab       = ts_vbrp[]        
      it_fieldcatalog = lt_fieldcat.

  call method cl_gui_control=>set_focus
    exporting
      control = grid.


endform.                    " create_and_display_container
*&---------------------------------------------------------------------*
*&      Form  exit_program
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form exit_program .

  call method g_custom_container->free.

  leave to screen 0.

endform.                    " exit_program
*&---------------------------------------------------------------------*
*&      Module  STATUS_1001  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module status_1001 output.

  set pf-status 'MAIN1001'.
  set titlebar '1001'.

  if g_custom_container is initial.
*Create container and set up grid data for display
    perform create_and_display_container.
  endif.

endmodule.                 " STATUS_1001  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_1001  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module user_command_1001 input.

  constants:      c_exit    like sy-ucomm value 'EXIT',
                  c_back    like sy-ucomm value 'BACK',
                  c_cancel  like sy-ucomm value 'CANCEL'.

  ok_code = sy-ucomm.
  case ok_code.
    when c_exit or c_back or c_cancel.
      perform exit_program.
    when others.
  endcase.

endmodule.                 " USER_COMMAND_1001  INPUT
*&---------------------------------------------------------------------*
*&      Form  set_fieldcatalog_init
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_FIELDCAT  text
*----------------------------------------------------------------------*
form set_fieldcatalog_init  using  lt_fieldcat type lvc_t_fcat.

  field-symbols : <fc> like line of lt_fieldcat.

  data : ls_fieldcatalog type lvc_s_fcat.

  constants : c_fkart(5)    type c value 'FKART',
              c_bukrs(5)    type c value 'BUKRS',
              c_rptgr(5) type c value 'RPTGR',
              c_ktext(5) type c value 'KTEXT',
              c_vbeln(5) type c value 'VBELN',
              c_kunag(5) type c value 'KUNAG',

              c_fkdat(5) type c value 'FKDAT',
              c_ernam(5) type c value 'ERNAM',
              c_erdat(5) type c value 'ERDAT',
              c_aedat(5) type c value 'AEDAT',
              c_aktnr(5) type c value 'AKTNR',

              c_aubel(5) type c value 'AUBEL',
              c_sonam(5) type c value 'SONAM',
              c_posnr(5) type c value 'POSNR',
              c_brand(5) type c value 'BRAND',
              c_att_value(9) type c value 'ATT_VALUE',
              c_netwr(5) type c value 'NETWR' ,
              c_rfbsk(5) type c value 'RFBSK'.


* Assign AUBEL column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_aubel.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c15. " Sample txt8
  <fc>-coltext   = text-c15. " Sample txt8
  <fc>-seltext   = text-c15. " Sample txt8
  <fc>-tooltip   = text-c15.  " Sample txt8
  <fc>-outputlen = '10'.
  <fc>-key       = 'X'.
  append <fc> to lt_fieldcat.

* Assign SONAM column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_sonam.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c16. " Sample txt7
  <fc>-coltext   = text-c16. " Sample txt7
  <fc>-seltext   = text-c16. " Sample txt7
  <fc>-tooltip   = text-c16. " Sample txt7
  <fc>-outputlen = '12'.
  append <fc> to lt_fieldcat.

* Assign POSNR column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_posnr.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c17. " Sample txt6
  <fc>-coltext   = text-c17. " Sample txt6
  <fc>-seltext   = text-c17. " Sample txt6
  <fc>-tooltip   = text-c17. " Sample txt6
  <fc>-outputlen = '6'.
  append <fc> to lt_fieldcat.

* Assign BRAND column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_brand.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c18. " Sample txt5
  <fc>-coltext   = text-c18. " Sample txt5
  <fc>-seltext   = text-c18. " Sample txt5
  <fc>-tooltip   = text-c18. " Sample txt5
  <fc>-outputlen = '3'.
  append <fc> to lt_fieldcat.

* Assign ATT_VALUE column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_att_value.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c19. " Sample txt4
  <fc>-coltext   = text-c19. " Sample txt4
  <fc>-seltext   = text-c19. " Sample txt4
  <fc>-tooltip   = text-c19. " Sample txt4
  <fc>-outputlen = '18'.
  append <fc> to lt_fieldcat.

* Assign NETWR column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_netwr.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c20. " Sample txt3
  <fc>-coltext   = text-c20. " Sample txt3
  <fc>-seltext   = text-c20. " Sample txt3
  <fc>-tooltip   = text-c20. " Sample txt3
  <fc>-outputlen = '12'.
  append <fc> to lt_fieldcat.

* Assign RFBSK column
  clear ls_fieldcatalog.
  assign ls_fieldcatalog to <fc>.
  <fc>-fieldname = c_rfbsk.
  <fc>-datatype  = 'C'.
  <fc>-reptext   = text-c21. " Sample txt2
  <fc>-coltext   = text-c21. " Sample txt2
  <fc>-seltext   = text-c21. " Sample txt2
  <fc>-tooltip   = text-c21. " Sample txt2
  <fc>-outputlen = '1'.
  append <fc> to lt_fieldcat.

  clear ls_fieldcatalog.

endform.             " set_fieldcatalog_init

----


Hope this will help.

Please reward suitable points.

Regards

- Atul

Former Member
0 Kudos

***********************************************************************

* Class definition :
***********************************************************************

*---------------------------------------------------------------------*
*       CLASS v_lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
CLASS v_lcl_event_receiver DEFINITION.

  PUBLIC SECTION.

    METHODS:

    handle_print_top_of_page FOR EVENT print_top_of_page OF
                                       cl_gui_alv_grid,

    handle_top_of_page FOR EVENT top_of_page OF
                                 cl_gui_alv_grid.

ENDCLASS.
*---------------------------------------------------------------------*
*       CLASS V_LCL_EVENT_RECEIVER IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS v_lcl_event_receiver IMPLEMENTATION.

  METHOD handle_print_top_of_page.
    IF sy-pagno = 1.
        PERFORM top_of_page.
    ENDIF.
  ENDMETHOD.
  METHOD handle_top_of_page.
      PERFORM top_of_page.
  ENDMETHOD.

ENDCLASS.

DATA:        v_event_receiver      TYPE REF TO v_lcl_event_receiver.

FORM top_of_page.
  WRITE: text-020,
           / 

ENDFORM.                    " top_of_page


In PBo of the screen
   DATA: v_split            TYPE REF TO cl_gui_easy_splitter_container,
         v_contnr_top       TYPE REF TO cl_gui_container,
         v_contnr_bot       TYPE REF TO cl_gui_container,
         v_grid_02          TYPE REF TO cl_gui_alv_grid,
         v_html             TYPE REF TO cl_dd_document,
         v_text20(255)      TYPE c,
         v_text16(255)      TYPE c,

FORM f9000_objects_create.
  IF cl_gui_alv_grid=>offline( ) IS INITIAL.
Create a container
    CREATE OBJECT o_dockingcontainer
      EXPORTING
        ratio                       = '95'
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5
        others                      = 6.
    IF sy-subrc NE 0.
      MESSAGE i000 WITH text-e01."Error in creating Docking container
      LEAVE LIST-PROCESSING.
    ENDIF.

    CREATE OBJECT v_split
         EXPORTING
           parent            = o_dockingcontainer
*          ORIENTATION       = 0
           sash_position     = 25
           with_border       = 0
         EXCEPTIONS
           cntl_error        = 1
           cntl_system_error = 2
           others            = 3.
    IF sy-subrc NE 0.
      MESSAGE i000 WITH text-e01."Error in creating Docking container
      LEAVE LIST-PROCESSING.
    ENDIF.
*   Get the containers of the splitter control
    v_contnr_top = v_split->top_left_container.
    v_contnr_bot = v_split->bottom_right_container.

    CREATE OBJECT o_alvgrid
   EXPORTING
     i_parent = o_dockingcontainer.

*   Create an instance of alv control
    CREATE OBJECT o_alvgrid
         EXPORTING i_parent = v_contnr_bot.

*   Object for display of selection parameters in HTML top container
    CREATE OBJECT v_html
         EXPORTING
           style = 'ALV_GRID'.


*   Must be after the SET HANDLER for TOP_OF_PAGE and foreground only
    CALL METHOD o_alvgrid->list_processing_events
                     EXPORTING i_event_name = 'TOP_OF_PAGE'
                               i_dyndoc_id  = v_html.

    v_text20 = text-020(summary Record counts)Any text.

    CALL METHOD v_html->add_gap
                EXPORTING
                  width         = 120.
    CALL METHOD v_html->add_text
           EXPORTING
             text          = v_text20.
    CALL METHOD v_html->new_line.
** Display Text-016
    v_text16 = text-016.

    CALL METHOD v_html->add_gap
                EXPORTING
                  width         = 1.
    CALL METHOD v_html->add_text
           EXPORTING
             text          = v_text16.

    v_text16 = v_sap_recon.
    CALL METHOD v_html->add_gap
                EXPORTING
                  width         = 1.
    CALL METHOD v_html->add_text
           EXPORTING
             text          = v_text16.
    CALL METHOD v_html->new_line.

* Display the data
    CALL METHOD v_html->display_document
      EXPORTING
         parent             = v_contnr_top.

*   Handle the event
    CALL METHOD o_alvgrid->list_processing_events
                        EXPORTING i_event_name = 'PRINT_TOP_OF_PAGE'.
 IN PBO while populating in the output table
FORM f9004_display_data TABLES   p_report_tab
                                 p_fieldcat.
  CALL METHOD o_alvgrid->set_table_for_first_display
    EXPORTING
       is_variant                    = w_variant
       i_save                        = c_a
       is_layout                     = w_layout
    CHANGING
       it_outtab                     = p_report_tab[]
       it_fieldcatalog               = p_fieldcat[]
    EXCEPTIONS
       invalid_parameter_combination = 1
       program_error                 = 2
       too_many_lines                = 3
       OTHERS                        = 4.

  IF sy-subrc <> 0.
    MESSAGE i000 WITH text-e06."Error in ALV report display
    LEAVE LIST-PROCESSING.
  ENDIF.

* Create object
  IF v_event_receiver IS INITIAL.
    CREATE OBJECT v_event_receiver.
  ENDIF.

  SET HANDLER v_event_receiver->handle_print_top_of_page FOR o_alvgrid.
  SET HANDLER v_event_receiver->handle_top_of_page FOR o_alvgrid.