Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Rows as columns and dynamic alv display

Hi everyone,

I need to build a intenal table dynamically as well as dispaly few rows as columns in alv output.

I have seen posts where internal table has been created dynamicaaly as well seen the posts where rows of the table are displayed as columns but, can we achieve both the functionalities at same time.

I have table as:


SO     PO     Date    MAT    QTY   
1        A       X    Y1      10  
                       K1      15
                       M2      11
2       B       X1     Y2       5
                       M1      12
3       C       1x     Z1      15
                       K1       6
                       L1      10

Now the ouput has to be :

SO     PO   Date     MAT-Y1    MAT-K1    MAT-Y2   MAT-L1     Mat-Z1    MAT-M1   MAT-M2
1        A     X      10          15                                               11             
2        B     X1                         5                             12                 
3        C     1X                  6                10          15                                        

I will not be knowing the number of materials untill runtime(dynamic) and as well the rows has to be displayed as columns. Is it possible to do that.

Regards

Edited by: Madhu Posanipalli on Jun 6, 2011 4:51 PM

Former Member
replied

Hi Madhu Posanipalli,

I think my first answer is still applicable to your question, maybe I wasn't clear enough.

Here is the general algorithm how I would do it: The following procedure reads the input internal table by transposing certain fields, you provide the field names on the left (SO, PO, date), on the top (material), on the intersection (quantity), and it returns a new internal table, that you will then display by using ALV (SALV classes are the simplest to use).

FORM transpose_matrix
      USING
        it_data TYPE ANY TABLE
        it_top  TYPE ty_gt_fieldname
        it_left TYPE ty_gt_fieldname
        it_intersection TYPE ty_gt_fieldname
      CHANGING
        ert_data TYPE REF TO DATA.
* create table for containing TOP distinct values
LOOP AT it_TOP_field ASSIGNING <l_top_field>.
  ASSIGN COMPONENT <l_top_field> of structure <ls_any> TO <L_field>.
  ...
ENDLOOP.
L_ref_s_dyn_col = Cl_abap_structdescr=>create( ).
Assign l_ref_s_dyn_col->* TO <ls_dyn_col>.
L_ref_t_dyn_col = Cl_abap_tabledescr=>create( L_ref_s_dyn_col ).
Assign l_ref_t_dyn_col->* TO <lt_dyn_col>.

* fill internal table of TOP distinct values -> it will give the number of "dynamic columns"
* multiply these columns by the number of fields at the intersection
LOOP AT it_data ASSIGNING <ls_data>
  LOOP AT it_TOP_field ASSIGNING <l_top_field>.
    ASSIGN COMPONENT <l_top_field> of structure <ls_data> TO <L_data_field>.
    ASSIGN COMPONENT <l_top_field> of structure <ls_dyn_col> TO <L_dyn_col_field>.
    <l_dyn_col_field> = <l_data_field>.
  ENDLOOP.
  COLLECT <ls_dyn_col> INTO <lt_dyn_col>.
ENDLOOP.

* create the new internal table
LOOP AT <lt_dyn_col>
  LOOP AT it_intersection
*    new column of the new internal table
  ENDLOOP.
ENDLOOP.
cl_abap_structdescr( )
cl_abap_tabledescr( )

* fill the new internal table
LOOP AT it_data
* many dynamic ASSIGN again
ENDLOOP.

ENDFORM.

Have fun!!!

Sandra

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question