Application Development and Automation 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: 
Read only

ALV with differents fields depending on a button

former_member384574
Active Participant
0 Likes
1,184

Hi experts!!!

I need to create a new ALV with some fields depending on the button pressed by the user, for example if the user press the button names, I have to create an ALV with the fields names, but if the user press the button age the alv will display the names and the ages for all pernr.

Does anybody know how to do this? In a different way that using structure.

Thanks a lot

Regards,

Rebeca

Hi experts!!!

I need to create a new ALV with some fields depending on the button pressed by the user, for example if the user press the button names, I have to create an ALV with the fields names, but if the user press the button age the alv will display the names and the ages for all pernr.

Does anybody know how to do this? In a different way that using structure.

Thanks a lot

Regards,

Rebeca

7 REPLIES 7
Read only

Former Member
0 Likes
1,138

Hi

Try this code.

In this code,you will get 2 push buttons on selection screen.'SFLIGHT' and 'SBOOK'.

When you clicked sbook ,an alv will be created with sbook details,and when sflight is clicked an alv

will be created with sflight details.

REPORT  zalv.
TABLES sscrfields.

DATA:
BEGIN OF t_SFLIGHT OCCURS 0.
 INCLUDE STRUCTURE sflight.
DATA END OF t_SFLIGHT.
DATA:
BEGIN OF t_Sbook OCCURS 0.
 INCLUDE STRUCTURE sbook.
DATA END OF t_Sbook.
DATA:
r_container TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid.

DATA:
r_container1 TYPE REF TO cl_gui_custom_container,
r_grid1 TYPE REF TO cl_gui_alv_grid.


DATA:
 t_fcat TYPE lvc_t_fcat,
 fs_fcat TYPE lvc_s_fcat.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
SELECTION-SCREEN PUSHBUTTON /1(10)  text-001
                            USER-COMMAND fcode.

SELECTION-SCREEN PUSHBUTTON 15(10) text-002
                            USER-COMMAND fcode1.

SELECTION-SCREEN END OF BLOCK b1.


AT SELECTION-SCREEN.

  CASE sy-ucomm.

    WHEN 'FCODE'.
      SELECT * FROM sflight INTO TABLE t_SFLIGHT UP TO 100 ROWS.

      PERFORM test.
    WHEN 'FCODE1'.
    select * from sbook into table t_sbook.

      PERFORM test2.
  ENDCASE.

*&---------------------------------------------------------------------*
*&      Form  TEST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM test .
  CALL SCREEN 100.
ENDFORM.                    " TEST

*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'SFLIGHT'.
SET TITLEBAR 'SFLIGHT_TITLE'.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  LIST  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE list OUTPUT.
  CREATE OBJECT r_container
    EXPORTING
      container_name = 'CONTAINER'.

  CREATE OBJECT r_grid
    EXPORTING
      i_parent = r_container.

  CALL METHOD r_grid->set_table_for_first_display
    EXPORTING
      i_structure_name = 'SFLIGHT'
    CHANGING
      it_outtab        = t_SFLIGHT[].



ENDMODULE.                 " LIST  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  TEST2
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM test2 .
  CALL SCREEN 200..

ENDFORM.                                                    " TEST2

*&---------------------------------------------------------------------*
*&      Module  STATUS_0200  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module STATUS_0200 output.
  SET PF-STATUS 'SBOOK'.
  SET TITLEBAR 'SBOOK_TITLE'.

endmodule.                 " STATUS_0200  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  LIST2  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module LIST2 output.
 CREATE OBJECT r_container1
    EXPORTING
      container_name = 'CONTAINER1'.

  CREATE OBJECT r_grid1
    EXPORTING
      i_parent = r_container1.

  CALL METHOD r_grid1->set_table_for_first_display
    EXPORTING
      i_structure_name = 'SBOOK'
    CHANGING
      it_outtab        = t_sbook[].
endmodule.                 " LIST2  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0200  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module USER_COMMAND_0200 input.
 CASE sy-ucomm.

    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
  ENDCASE.

endmodule.                 " USER_COMMAND_0200  INPUT

regards

Hareesh

Read only

Former Member
0 Likes
1,138

hi

create a subroutine for creating field catalog.

form f_fieldcatlog using p_field type c p_seltext type c p_table type c.

i_fieldcat-fieldname = p_fieldname.

i_fieldcat-seltext_l = p_seltext.

i_fieldcat-tabname = p_table.

append i_fielcat.

endform.

write perform statements accoring to ur requirement.

eg

case sy-ucomm.

when 'age'.

perform f_fieldcatlog 'age' 'age of a person' 'itab'.

..........

endcase.

Read only

0 Likes
1,138

Hi again experts,

the problem I've to solve is how to do that with differents fields of differents tables, and also with differents buttons. Also I can't create any new structure or variants....can anybody help me?

Thanks a lot,

Regards,

Rebeca

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
1,138

Hi,

Say you have take button for name with function code as NAME and for age as AGE.

Use:-


*FIELD CATALOG
DATA : it_field TYPE slis_t_fieldcat_alv,
       wa_field TYPE slis_fieldcat_alv.

START-OF-SELECTION.
  PERFRRM get_data. "select data into internal table
  PERFORM field_catalog. "create field catalog

END-OF-SELECTION.
  PEFORM display_alv. "pass field catalog and internal table

FORM field_catalog.
  CASE sy-ucomm.
    WHEN 'NAME'.
      wa_field-fieldname = '<field_name>'. "for name
      wa_field-tabname = 'ITAB'. "internal table
      wa_field-outputlen = 10.
      wa_field-seltext_l = '<text>'.
      APPEND wa_field TO it_field.
      CLEAR wa_field.
    WHEN 'AGE'.
      wa_field-fieldname = '<field_name>'. "for age
      wa_field-tabname = 'ITAB'. "internal table
      wa_field-outputlen = 3.
      wa_field-seltext_l = '<text>'.
      APPEND wa_field TO it_field.
      CLEAR wa_field.
  ENDCASE.
ENDFORM.

Similarly you can append more field in the field catalog table to be displayed in the internal table.

Hope this helps you.

Regards,

Tarun

Read only

0 Likes
1,138

Thanks for your answer, Tarun, only one more question, the perform get_data... collect all the data you're going to show in the ALV? If yes... Where you save this data? In a dictionary data you've create? I can't do that using structures or tables....

For example, imagine that I have two buttons, the first one must show this data pernr, vorna, ename (all from infotype pa0002) and the second button must show the data pernr, ename and SCHKZ, how you do that?

Thanks a lot!!!!

Regards,

Rebeca

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
1,138

Hi,

First declare all the infotypes that you need to use.

Create a final internal table fields:- PERNR, VORNA, ENAME, SCHKZ

In PERFORM get_data use:-


GET pernr. "to get all the pernr

Now use RP PROVIDE statement to get the reqd data from infotypes as declared above.

Now move the record details from infotype to the final internal table in sub-routine get_data and append it.

Before END-OF-SELECTION you will have all the data from pernr in the final internal table.

Based on the button clicked by the user for displaying records in alv, create associated field catalogs.


CASE sy-ucomm.
  WHEN 'BTN1'.
    "refresh field catalog table
    "append field catalog for pernr vorna and ename
  WHEN 'BTN2'.
    "refresh field catalog table
    "append field catalog for pernr ename and schkz
ENDCASE.

Now in perform alv_display call FM REUSE_ALV_GRID_DISPLAY and pass field catalog and internal table to display the reqd data.

Hope this helps you.

Regards,

Tarun

Read only

Former Member
0 Likes
1,138

Hi Rebecca,

In the user command, you will have to get the reference of the ALV object ( if you are using REUSE_ALV*), and use the methods get and set fieldcatalog to make the changes to the frontend fieldcatalog to show and hide the columns you need.

Something like this :


FORM user_command USING r_ucomm TYPE sy-ucomm
                        rs_selfield TYPE slis_selfield.

  IF r_ucomm = 'NAME'.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
   IMPORTING
*     ET_EXCLUDING             =
*     E_REPID                  =
*     E_CALLBACK_PROGRAM       =
*     E_CALLBACK_ROUTINE       =
     E_GRID                   =   e_grid
*     ET_FIELDCAT_LVC          =
*     ER_TRACE                 =
*     E_FLG_NO_HTML            =
*     ES_LAYOUT_KKBLO          =
*     ES_SEL_HIDE              =
*     ET_EVENT_EXIT            =
            .

  CALL METHOD e_grid->get_frontend_fieldcatalog
    IMPORTING
      ET_FIELDCATALOG = fcat_tab.

 read table fcat_tab with key FIELDNAME = 'AGE'
       transporting no fields.
if sy-subrc = '0'.
   delete fcat_tab index sy-index.

   CALL METHOD e_grid->set_frontend_fieldcatalog
    exporting
      it_FIELDCATALOG = fcat_tab.

endif.
     
  endif.

endform.

Hope this helps.

regards,

Advait