[ABAP] SALV-Table: Spalte als Schlüsselspalte darstellen und fixieren

TRY.
* Spalte 'MATNR' als Schlüsselspalte definieren    
    DATA(o_coltab) = CAST cl_salv_column_table( o_alv->get_columns( )->get_column( 'MATNR' ) ).
    o_coltab->set_key( abap_true ).
* Schlüsselspalten fixieren
    o_alv->get_columns( )->set_key_fixation( abap_true ).
  
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] SALV-Table – Benutzerspezifisches Layout setzen

* https://belajarabap.wordpress.com/2022/01/29/alv-report-using-cl_salv_table/
* http://abapmentor.expertise-team.com/post/display-alv-list-easily-in-abap-using-cl-salv-table-part-ii-enable-layout-settings.aspx
* https://community.sap.com/t5/application-development-blog-posts/defining-and-displaying-user-defined-layouts-using-factory-method/ba-p/13500044

* REPORT: SALV_DEMO_TABLE_LAYOUT

PARAMETERS: p_layout TYPE disvariant-variant.

INITIALIZATION.
  DATA(lv_key) = VALUE salv_s_layout_key( report = sy-repid ).

  p_layout = cl_salv_layout_service=>get_default_layout( s_key = lv_key )-layout.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_layout.
  DATA(lv_key) = VALUE salv_s_layout_key( report = sy-repid ).

  p_layout = cl_salv_layout_service=>f4_layouts( lv_key )-layout.

START-OF-SELECTION.

  SELECT FROM mara
    FIELDS *
    INTO TABLE @DATA(it_mara)
    UP TO 100 ROWS.

  IF sy-subrc = 0.
    TRY.
*   SALV-Table
        DATA: o_salv TYPE REF TO cl_salv_table.

        cl_salv_table=>factory( IMPORTING r_salv_table = o_salv
                                CHANGING  t_table      = it_mara ).

*   Grundeinstellungen
        o_salv->get_functions( )->set_all( abap_true ).
        o_salv->get_columns( )->set_optimize( abap_true ).
        o_salv->get_display_settings( )->set_list_header( 'MARA' ).
        o_salv->get_display_settings( )->set_striped_pattern( abap_true ).
        o_salv->get_selections( )->set_selection_mode( if_salv_c_selection_mode=>row_column ).

*   Spaltenüberschriften: technischer Name und Beschreibungstexte, Short Text und Medium Text leer lassen für Autosize
        LOOP AT o_salv->get_columns( )->get( ) ASSIGNING FIELD-SYMBOL(<c>).
          DATA(o_col) = <c>-r_column.
          o_col->set_short_text( || ).
          o_col->set_medium_text( || ).
          o_col->set_long_text( |{ o_col->get_long_text( ) }| ).
        ENDLOOP.

* SALV-Layout setzen, Layout-Buttons aktivieren
        DATA(o_layout) = o_salv->get_layout( ).
        o_layout->set_default( abap_true ).
        o_layout->set_key( VALUE #( report = sy-repid ) ).
        o_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).

        IF p_layout IS INITIAL.
          o_layout->set_initial_layout( 'DEFAULT' ).
        ELSE.
          o_layout->set_initial_layout( p_layout ).
        ENDIF.

        o_salv->display( ).
      CATCH cx_root INTO DATA(e_txt).
        WRITE: / e_txt->get_text( ).
    ENDTRY.
  ENDIF.