Code to go with Web Dynpro ALV – Part 1

Web Dynpro ALV is created using component SALV_WD_TABLE. After creating ALV you might need to tweak its appearance little bit before it’s presented to user. Below are code snippets for some common tweaking which you might need to do.

All these tweaks are done using API call referred collectively as ALV configuration model. To get reference of ALV configuration model you can use Web Dynpro code wizard to call method GET_MODEL of used ALV component.

Web Dynpro Code Wizard for ALV Configuration Model

ALV Configuration Model implements interfaces which in turn provide methods to tweak display setting, toolbar setting, column settings etc.

ALV Configuration Model
ALV Configuration Model

Read about ALV Configuration model in detail at this help.sap.com link.


Making Display Alternating

By default, ALV rows are not displayed with alternating shades. You can set alternating shades using following code.

METHOD set_alternating .

*--------------------------------------------------------------------*
* Call method GET_MODEL of used component
* Code using web dynpro code wizard
*--------------------------------------------------------------------*

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

* Method call to set alternating design (zebra)
  lo_config_table->if_salv_wd_table_settings~set_design(
                         cl_wd_table=>e_design-alternating ) .

ENDMETHOD.

There are other styles available apart from alternating which you can use.

cl_wd_table=>e_design-standard
cl_wd_table=>e_design-transparent
cl_wd_table=>e_design-transparent_with_grid


Set Empty Table Text

With this setting active ALV with no rows will display a message text instead of empty lines.

  lo_config_table->if_salv_wd_table_settings~set_empty_table_text(
      value = 'No Order selected' ) .


Set Header Text

With this piece of code you can set header text on ALV.

METHOD set_header .

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  DATA : lo_alv_header TYPE REF TO cl_salv_wd_header .

  lo_alv_header = lo_config_table->if_salv_wd_table_settings~create_header( ) .

  lo_alv_header->set_text( 'List of Sales Orders').

ENDMETHOD.


Fix Number of Columns in ALV Display

By default ALV grows horizontally, until it run out of space based on layout, to displays all columns. In case of flow layout you may end up with horizontal scroll bar with ‘Filter’ and ‘Settings’ inaccessible to user unless they scroll right. You can solve this problem by setting number of columns ALV will display.

You can get appropriate number by playing with different number of columns from ‘Settings’ menu

image004

Once you find right value for you then you can use below code to set it programmatically.

METHOD set_width .

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  lo_config_table->if_salv_wd_table_settings~set_scrollable_col_count( 10 ).
  
ENDMETHOD.

Note that with right number of column you will not have horizontal scroll bar in page. Scroll bar is now in ALV.

image005


Set Visible Row Count

You can also set number of rows ALV will display using below code.

lo_config_table->if_salv_wd_table_settings~set_visible_row_count( 5 ) .

Setting Column Header Text

Column headers are automatically derived from data element of fields. If you want to change column header best way is to change label of data element. But for some reason if you want to directly change column headers in ALV then below are two options.

Method 1: By specifying text directly using SET_TEXT method of class CL_SALV_WD_COLUMN_HEADER
METHOD change_column_header.

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  DATA : lo_column TYPE REF TO cl_salv_wd_column .
  lo_column = lo_config_table->if_salv_wd_column_settings~get_column( 
                       'SD_DOC' ) .

  IF lo_column IS NOT INITIAL.
    DATA : lo_column_header TYPE REF TO cl_salv_wd_column_header .
    lo_column_header = lo_column->create_header( ) .
    lo_column_header->set_text( 'Sales Order' ) .
  ENDIF.

ENDMETHOD.

image006

Method 2: By using another data element for column header

Method SET_PROP_DDIC_BINDING_ELEMENT of class CL_SALV_WD_COLUMN_HEADER can be called with data element as parameter who’s label will then be for column header.

METHOD change_column_header .

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  DATA : lo_column TYPE REF TO cl_salv_wd_column .
  lo_column = lo_config_table->if_salv_wd_column_settings~get_column( 'SD_DOC' ) .

  IF lo_column IS NOT INITIAL.
    DATA : lo_column_header TYPE REF TO cl_salv_wd_column_header .

    lo_column_header = lo_column->get_header( ) .
    lo_column_header->set_prop_ddic_binding_element(
             EXPORTING value = 'EBELN').
  ENDIF.

ENDMETHOD.

image007


Hiding Column

METHOD hide_column .

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  DATA : lo_column TYPE REF TO cl_salv_wd_column .
  lo_column = lo_config_table->if_salv_wd_column_settings~get_column( 'MATERIAL' ) .

  IF lo_column IS NOT INITIAL.
    CALL METHOD lo_column->set_visible
      EXPORTING
        value = cl_wd_uielement=>e_visible-none.
  ENDIF.
ENDMETHOD.                    "change_column_header

Column Fix

Unlike classical ALV Web Dynpro ALV allows you to fix columns at both ends i.e. left and right. To fix a column first you need to call method SET_SCROLLABLE_COL_COUNT to specify scrollable columns. Then call SET_FIX_POSITION method for each column you want to remain fix. In this particular example, I have 10 columns in ALV and I would like to fix one column fix at each side therefore I am calling SET_SCROLLABLE_COL_COUNT with value 8.

METHOD fix_columns .

  DATA lo_interfacecontroller TYPE REF TO iwci_salv_wd_table .
  DATA lo_config_table        TYPE REF TO cl_salv_wd_config_table.

  lo_interfacecontroller =   wd_this->wd_cpifc_order_salv( ).
  lo_config_table = lo_interfacecontroller->get_model( ).

  lo_config_table->if_salv_wd_table_settings~set_scrollable_col_count( 8 ) .

  DATA lo_column TYPE REF TO cl_salv_wd_column .

  lo_column = lo_config_table->if_salv_wd_column_settings~get_column( 'SD_DOC' ) .
  lo_column->set_fixed_position( cl_wd_abstr_table_column=>e_fixed_position-left ).

  lo_column = lo_config_table->if_salv_wd_column_settings~get_column( 'ITM_NUMBER' ) .
  lo_column->set_fixed_position( cl_wd_abstr_table_column=>e_fixed_position-right ) .

ENDMETHOD.

Leave a Reply