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: 

CL_SALV_* classes and fixing columns.

bryan_cain
Contributor
0 Kudos

Hi All -

I'm having some trouble with the new OO-ALV classes.

I'm trying to fix the first column in place so that when the user scrolls, the column stays there. In the function module based ALV model, I could just set the fix_column field in the field catalog.

I had thought that the set_key() method of the column object along with the set_key_fixation() of the columns attributes would do it, but I can't it to work.

I can freeze the field manually if I run the report and right click on the column I want to freeze, but I'd like to default it that way in the code.

Do you guys see anything wrong with my code below? Am I missing something?

Thanks in advance.

-Bryan


*----------------------------------------------------------------------*
*   Data definition
*----------------------------------------------------------------------*
* ALV variables.
DATA: oref_table          TYPE REF TO cl_salv_table,
      oref_functions      TYPE REF TO cl_salv_functions,
      oref_columns        TYPE REF TO cl_salv_columns_table,
      oref_column         TYPE REF TO cl_salv_column_table,
      oref_layout         TYPE REF TO cl_salv_layout,
      oref_display        TYPE REF TO cl_salv_display_settings,
      oref_sorts          TYPE REF TO cl_salv_sorts,
      oref_aggregations   TYPE REF TO cl_salv_aggregations,
      g_repid             TYPE sy-repid,
      g_layout_key        TYPE salv_s_layout_key,
      g_layout            TYPE salv_s_layout,
      gt_columns          TYPE salv_t_column_ref.

*----------------------------------------------------------------------*
*   Initialization
*----------------------------------------------------------------------*
INITIALIZATION.

  g_repid = sy-repid.

* instantiate the table and the layout in the initialization section
* so we can use a method of the layout class for the f4 help.
  TRY .
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table   = oref_table
        CHANGING
          t_table        = gt_output
             ).
    CATCH cx_salv_msg.

  ENDTRY.

  oref_layout = oref_table->get_layout( ).
  g_layout_key-report = g_repid.
  oref_layout->set_key( g_layout_key ).

* only let the user save user-specific layouts - this needs to be set in
* init for the f4 to work right
  oref_layout->set_save_restriction( cl_salv_layout=>restrict_user_dependant ).

*----------------------------------------------------------------------*
*   At selection-screen
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
* call the f4 help and set the layout variable.
  g_layout = oref_layout->f4_layouts( ).
  p_vari = g_layout-layout.

*----------------------------------------------------------------------*
*   END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.

  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV
*&---------------------------------------------------------------------*
*       Call ALV related functions.
*----------------------------------------------------------------------*
FORM display_alv .


* create the fucntion object and force all to available.
  oref_functions = oref_table->get_functions( ).
  oref_functions->set_all( abap_true ).

* create the display settings object and set the various attributes
  oref_display = oref_table->get_display_settings( ).
  oref_display->set_striped_pattern( cl_salv_display_settings=>true ).
  oref_display->set_fit_column_to_table_size( cl_salv_display_settings=>true ).
  oref_display->set_list_header( 'Complaints Reporting' ).

* create the columns object and apply the ddic structure to the otuput
  oref_columns = oref_table->get_columns( ).
  oref_columns->apply_ddic_structure( name = c_structure ).
  oref_columns->set_key_fixation( ).

* lock the notification number column
  TRY .
      oref_column ?= oref_columns->get_column( 'QMNUM' ).
    CATCH cx_salv_not_found.
  ENDTRY.
  IF oref_column IS BOUND.
    oref_column->set_key( ).
    oref_column->set_key_presence_required( ).
  ENDIF.

* set the data in the table.
  TRY .
      oref_table->set_data( CHANGING t_table = gt_output   ).
    CATCH cx_salv_no_new_data_allowed.

  ENDTRY.

* set the layout variant if one has been chosen.
  IF NOT p_vari IS INITIAL.
    oref_layout->set_initial_layout( value = p_vari   ).
  ELSE.

* update sort info
    oref_sorts = oref_table->get_sorts( ).
    TRY .
        oref_sorts->add_sort( columnname = 'QMNUM'  ).
      CATCH cx_salv_data_error.
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.

    ENDTRY.
  ENDIF.

* display the table.
  oref_table->display( ).

ENDFORM.                    " DISPLAY_ALV

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

Please try this class CL_SALV_COLUMN and method

set_column_position.

14 REPLIES 14

naimesh_patel
Active Contributor

You need to first set the Key using the SET_KEY mehtod and than use the method SET_KEY_FIXATION to freeze those key columns.

Like:


    DATA: LO_COLS_TAB TYPE REF TO CL_SALV_COLUMNS_TABLE,
          LO_COL_TAB  TYPE REF TO CL_SALV_COLUMN_TABLE.

*   get Columns object
    LO_COLS_TAB = O_ALV1->GET_COLUMNS( ).

*   Get MATNR column
    TRY.
        LO_COL_TAB ?= LO_COLS_TAB->GET_COLUMN( 'MATNR' ).
      CATCH CX_SALV_NOT_FOUND.
    ENDTRY.

*   Set the Key Column
    TRY.
    CALL METHOD lo_col_tab->set_key
      EXPORTING
        VALUE  = IF_SALV_C_BOOL_SAP=>TRUE
        .

    ENDTRY.

*   fix the Key column
    call method  lo_cols_tab->SET_KEY_FIXATION.

Regards,

Naimesh Patel

0 Kudos

Changing the order between those method calls didn't seem to help.

Thank you though.

Former Member
0 Kudos

Hi,

Please try this class CL_SALV_COLUMN and method

set_column_position.

0 Kudos

set_column_position doesn't seem to be part of cl_salv_column - it seems to be part of cl_salv_columns_table.

And calling it didn't seem to fix the problem.

Thanks though.

0 Kudos

Sorry missed a 's' at the end. Check this class

CL_SALV_COLUMNS for method SET_COLUMN_POSITION.

0 Kudos

Still didn't seem to help.

former_member188685
Active Contributor
0 Kudos

I didn't change your code. I just deleted some parts of it.

it is working fine. just check it once.

*----------------------------------------------------------------------*
*   Data definition
*----------------------------------------------------------------------*
* ALV variables.
DATA: oref_table          TYPE REF TO cl_salv_table,
      oref_functions      TYPE REF TO cl_salv_functions,
      oref_columns        TYPE REF TO cl_salv_columns_table,
      oref_column         TYPE REF TO cl_salv_column_table,
      oref_layout         TYPE REF TO cl_salv_layout,
      oref_display        TYPE REF TO cl_salv_display_settings,
      oref_sorts          TYPE REF TO cl_salv_sorts,
      oref_aggregations   TYPE REF TO cl_salv_aggregations,
      g_repid             TYPE sy-repid,
      g_layout_key        TYPE salv_s_layout_key,
      g_layout            TYPE salv_s_layout,
      gt_columns          TYPE salv_t_column_ref.
DATA: gt_output TYPE sflight_tab1.
*----------------------------------------------------------------------*
*   Initialization
*----------------------------------------------------------------------*
INITIALIZATION.

  g_repid = sy-repid.

* instantiate the table and the layout in the initialization section
* so we can use a method of the layout class for the f4 help.
  TRY .
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table   = oref_table
        CHANGING
          t_table        = gt_output
             ).
    CATCH cx_salv_msg.

  ENDTRY.

  oref_layout = oref_table->get_layout( ).
  g_layout_key-report = g_repid.
  oref_layout->set_key( g_layout_key ).

* only let the user save user-specific layouts - this needs to be set in
* init for the f4 to work right
  oref_layout->set_save_restriction( cl_salv_layout=>restrict_user_dependant ).



*----------------------------------------------------------------------*
*   END-OF-SELECTION
*----------------------------------------------------------------------*
END-OF-SELECTION.
select * from sflight
into table gt_output
up to 20 rows.
  PERFORM display_alv.

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV
*&---------------------------------------------------------------------*
*       Call ALV related functions.
*----------------------------------------------------------------------*
FORM display_alv .


* create the fucntion object and force all to available.
  oref_functions = oref_table->get_functions( ).
  oref_functions->set_all( abap_true ).

* create the display settings object and set the various attributes
  oref_display = oref_table->get_display_settings( ).
  oref_display->set_striped_pattern( cl_salv_display_settings=>true ).
  oref_display->set_fit_column_to_table_size( cl_salv_display_settings=>true ).
  oref_display->set_list_header( 'Complaints Reporting' ).

* create the columns object and apply the ddic structure to the otuput
  oref_columns = oref_table->get_columns( ).
  oref_columns->apply_ddic_structure( name = 'SFLIGHT' ).
  oref_columns->set_key_fixation( ).

* lock the notification number column
  TRY .
      oref_column ?= oref_columns->get_column( 'QMNUM' ).
    CATCH cx_salv_not_found.
  ENDTRY.
  IF oref_column IS BOUND.
    oref_column->set_key( ).
    oref_column->set_key_presence_required( ).
  ENDIF.

* set the data in the table.
  TRY .
      oref_table->set_data( CHANGING t_table = gt_output   ).
    CATCH cx_salv_no_new_data_allowed.

  ENDTRY.

* update sort info
  oref_sorts = oref_table->get_sorts( ).

* display the table.
  oref_table->display( ).

ENDFORM.                    " DISPLAY_ALV

0 Kudos

Removing the sorting didn't help.

I think that's all you took out, right?

0 Kudos

I included the sort also in the code , it is working fine. i just changed the below one.

oref_columns->apply_ddic_structure( name = 'SFLIGHT' ).

and removed the variant option. rest every thing same. did you check the above code.

0 Kudos

I did just write a quick test program using SFLIGHT rather than my custom structure. That worked.

I think the problem is that my custom DDIC structure does not have a key. Since it does not have a key, telling the model to lock the key fields does nothing.

I wonder if there's an easy way around this... I suppose rather than create a ddic structure for my ALV output I could have built it programatically - but with 150+ fields, that would have been unpleasant.

0 Kudos

So I figured it out - if you're using a DDIC structure, you need to make sure you have the key fields specified as key fields in the DDIC if you want to lock. There doesn't appear to be any other way to do it.

I didn't realize you could specify key fields in a structure. I never needed to do it before, and the field appears to be hidden by default. I had to look in the extras menu to display it in the structure editor.

Anyway, thanks for the pointers all.

0 Kudos

Turns out I was completely wrong with my "solution" above. I wanted to post an update to this issue in case some one else comes looking for an answer to a similar situation.

The reason it didn't work is because the code above calls the set_data method of the table object AFTER modifying the layout. Unfortunately, set_data refreshes the layout from the internal table. Therefore, in order to fix the code above, I would need to call set_data FIRST, then do the modifications to the layout.

Like so:

FORM display_alv .
 * set the data in the table.
  TRY .
      oref_table->set_data( CHANGING t_table = gt_output   ).
    CATCH cx_salv_no_new_data_allowed.
 
  ENDTRY.
 
* create the fucntion object and force all to available.
  oref_functions = oref_table->get_functions( ).
  oref_functions->set_all( abap_true ).
 
* create the display settings object and set the various attributes
  oref_display = oref_table->get_display_settings( ).
  oref_display->set_striped_pattern( cl_salv_display_settings=>true ).
  oref_display->set_fit_column_to_table_size( cl_salv_display_settings=>true ).
  oref_display->set_list_header( 'Complaints Reporting' ).
 
* create the columns object and apply the ddic structure to the otuput
  oref_columns = oref_table->get_columns( ).
  oref_columns->apply_ddic_structure( name = c_structure ).
  oref_columns->set_key_fixation( ).
 
* lock the notification number column
  TRY .
      oref_column ?= oref_columns->get_column( 'QMNUM' ).
    CATCH cx_salv_not_found.
  ENDTRY.
  IF oref_column IS BOUND.
    oref_column->set_key( ).
    oref_column->set_key_presence_required( ).
  ENDIF.
 
ENDFORM.

0 Kudos

Hi Brian -

I tried to use your solution, but it seems like SET_KEY method doesn't exist in class CL_SALV_COLUMN.

Thanks,

John

Sorry, I should be using cl_salv_column_table instead of cl_salv_column.

Thanks,

John