Skip to Content
avatar image
Former Member

Create dynamic structure for ALV

Hi Experts,

I would like to ask some help with regards to my requirement.

The requirement is to make the ALV output dynamic, I had already the idea but my problem is how to make the structure or internal table dynamic instead of declaring manually a maximum number of fields. Currently I used the function module 'REUSE_ALV_FIELDCATALOG_MERGE' which is to build the field catalog. I created a structure which contains all the fields I need to display in the ALV but I need to change it now to make it dynamic because there are columns which will only display depends of the data inputted on the selection-screen which is of data type a988-kschl. Do you have any idea? Your suggestions is very much appreciated and especially sample code is provided.

This is how I declare may parameters:

SELECT-OPTIONS: s_kschl FOR a988-kschl.

Regards,

Ekitzv

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • Mar 21, 2011 at 04:16 AM

    i think instead of using funtion module to build field catalog, fill field catalog manually.

    i guess that would solve your probelm??

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi,

      No, because they don't want to modify the program everytime a nec KSCHL is created. If you will create a manual build of field catalog you need to fix the field.

  • avatar image
    Former Member
    Mar 21, 2011 at 05:52 AM

    Hi

    Please find the below code Here first it will create fields dynamically.But How many fields u want to create first the number of records and pass that record number to LV_COUNT variable

    DO lv_count TIMES.

    count = count + 1.

    CONCATENATE 'SGPA' count INTO v_var. "Colum name

    CONCATENATE 'ADM_PERID' count INTO l_name. "Here we can mention the fields

    wa_flname = l_name.

    wa_it_fldcat-fieldname = wa_flname.

    wa_it_fldcat-seltext = v_var.

    wa_it_fldcat-intlen = 10.

    APPEND wa_it_fldcat TO t_fldcat.

    CLEAR v_var.

    • ENDLOOP.

    ENDDO.

    CALL METHOD cl_alv_table_create=>create_dynamic_table "Here creates the internal table dynamcally

    EXPORTING

    it_fieldcatalog = t_fldcat

    IMPORTING

    ep_table = t_newtable.

    ASSIGN t_newtable->* TO <t_dyntable>.

    • Create dynamic work area and assign to FS

    CREATE DATA t_newline LIKE LINE OF <t_dyntable>.

    ASSIGN t_newline->* TO <fs_dyntable>.

    Regards,

    Muralii

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 06:16 AM

    Hi Ekitzv ,

    Below is the sample code to demonstrate dynamic creation of table. Check the code as it may help you solve your problem. But to display the table in an ALV using the standard funtion module , the Tables parametre will not accept the field-symbol of type any table. it has to be assigned to either importing, exporting or changing parametrs. so, i have used cl_salv_table factory method to display the ALV Data and i also think that even you have to follow this way to solve your problem.

    
    * This program display the data of the table  name that you have
    * entered on the selection-screen.
    
    PARAMETERS: tab_name TYPE dd02l-tabname.
    
    DATA: gr_table TYPE REF TO cl_salv_table.
    
    DATA: dref TYPE REF TO data.
    FIELD-SYMBOLS : <fs> TYPE ANY TABLE.
    
    START-OF-SELECTION.
    * To achieve the required funtionality internal table should be created
    *  dynamically as it is not static.
      CREATE DATA dref TYPE TABLE OF (tab_name).
    * dereference it so that the contents in the address dref are transferrd to field symbol <fs>
    * which is of type table and assign it
      ASSIGN dref->* TO <fs>.
    * select the data from the database table
      SELECT * FROM (tab_name) INTO TABLE <fs>.
    
    * Display the data
    *  TRY.
      CALL METHOD cl_salv_table=>factory
    *    EXPORTING
    *      LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
    *      R_CONTAINER    =
    *      CONTAINER_NAME =
        IMPORTING
          r_salv_table   = gr_table
        CHANGING
          t_table        = <fs>
          .
    *   CATCH CX_SALV_MSG .
    *  ENDTRY.
    
      gr_table->display( ).
    
    
    

    Cheers,

    Ravi.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 06:37 AM

    Hi Ekitzv,

    I think you could solve this problem by setting the filedcatlog property " NO_OUT ". By setting this property to 'X' this could be achived. But in this case you need to modify the fieldcatlog after creating the fieldcatlog using 'REUSE_ALV_FIELDCATALOG_MERGE' .

    Sample code :

    WHEN c_total .

    <fs_fieldcat>-no_out = c_x.

    Thanks and Regards,

    Sri Hari Anand Kumar

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 06:41 AM

    Hello

    The following Wiki coding

    [Creating Flat and Complex Internal Tables Dynamically using RTTI|http://wiki.sdn.sap.com/wiki/display/Snippets/CreatingFlatandComplexInternalTablesDynamicallyusingRTTI]

    demonstrates the use of dynamic itabs (even complex ones) for the new fieldcatalog structure (LVC_S_FCAT).

    Regards

    Uwe

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 06:51 AM

    hi,

    Please Refer the code in following link

    http://bit.ly/ewFnNj

    Hope this will help you.

    Regards,

    Renuka S.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 06:56 AM
    type-pools: slis.
      DATA : lv_repid TYPE sy-repid,
             i_structure_name TYPE  dd02l-tabname VALUE 'Z_your_structure',
             ct_fieldcat   TYPE          slis_t_fieldcat_alv,
             wa_fieldcat LIKE LINE OF ct_fieldcat.
    
      lv_repid = sy-repid.
      CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
        EXPORTING
          i_program_name         = lv_repid
          i_structure_name       = i_structure_name
        CHANGING
          ct_fieldcat            = ct_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.
      
      LOOP AT ct_fieldcat INTO wa_fieldcat.
        IF wa_fieldcat-fieldname = ' FIELD_1'.  "Suppose you don't want to output FIELD_1
          wa_fieldcat-no_out = 'X'.
          MODIFY ct_fieldcat FROM wa_fieldcat INDEX sy-tabix.
        ELSEIF wa_fieldcat-fieldname = ' FIELD_2'.
          .....
          .....
        ENDIF.
    
      ENDLOOP.
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 21, 2011 at 09:04 AM

    Hi

    As per my understanding you are getting the data in your internal table for the whole structure you have declared for your ALV.

    Now after field-catalog merge you can loop through your fieldcat table and put conditions and set no_out as X or Not X.

    and modify your fieldcatlog table.

    and pass this to your ALV grid Display.

    I hope this will help you.

    or

    what you can check after getting data in your final table.

    check particular field is not in your select option

    like

    suppose I have a table ITAB

    and there are fields matnr and werks

    also I have select options for matnr and werks as S_MATNR and S_WERKS

    so check if itab-matnr not in s_matnr or

    itab-werks not in s_werks.

    clear itab or rfresh itab.

    endif.

    this must be something like this.

    Thanks

    LG

    Add comment
    10|10000 characters needed characters exceeded