Wednesday, 10 October 2018

SAP - ABAP : Dynamic ALV Cell Style

report zalv_edit.

class lcl_report definition.
*
  public section.
*
*   Final output table
    types: begin of ty_vbak,
             vbeln      type vbak-vbeln,
             erdat      type erdat,
             auart      type auart,
             kunnr      type kunnr,
             i_celltype type salv_t_int4_column,
           end   of ty_vbak.
    data: t_vbak type standard table of ty_vbak.

*   ALV reference
    data: o_alv type ref to cl_salv_table.

    methods:
      get_data,           "  data selection
      generate_output.    "  Generating output
*
  private section.
    methods:
      set_columns.        "  Set columns
endclass.                    "lcl_report DEFINITION
*
*
start-of-selection.
  data: lo_report type ref to lcl_report.
*
  create object lo_report.
  lo_report->get_data( ).
  lo_report->generate_output( ).


*
class lcl_report implementation.
*
  method get_data.
*   data selection

    select vbeln erdat auart kunnr
           into  corresponding fields of table t_vbak
           from  vbak
           up to 20 rows.

    field-symbols: <lfs_vbak> like line of t_vbak.
    data: lt_celltype type salv_t_int4_column.
    data: ls_celltype like line of lt_celltype.
    loop at t_vbak assigning <lfs_vbak>.
      clear: lt_celltype.
* Only VBELN for 2nd record
      if sy-tabix = 2.
        ls_celltype-columnname = 'VBELN'.
        ls_celltype-value      = if_salv_c_cell_type=>hotspot.
        append ls_celltype to lt_celltype.
* Only ERDAT for 3rd record
      elseif sy-tabix = 3.
        ls_celltype-columnname = 'ERDAT'.
        ls_celltype-value      = if_salv_c_cell_type=>button.
        append ls_celltype to lt_celltype.
* Entire 5th record
      elseif sy-tabix = 5.
        ls_celltype-columnname = ".
        ls_celltype-value      = if_salv_c_cell_type=>hotspot.
        append ls_celltype to lt_celltype.
      endif.
      <lfs_vbak>-i_celltype = lt_celltype.
    endloop.

  endmethod.                    "get_data
*
  method generate_output.
* New ALV instance
    data: lx_msg type ref to cx_salv_msg.
    try.
        cl_salv_table=>factory(
          importing
            r_salv_table = o_alv
          changing
            t_table      = t_vbak ).
      catch cx_salv_msg into lx_msg.
    endtry.
*
* Setting up the Columns
    me->set_columns( ).

* Displaying the ALV
    o_alv->display( ).

  endmethod.                    "generate_output
*
  method set_columns.
*
*...Get all the Columns
    data: lo_cols type ref to cl_salv_columns_table.
    lo_cols = o_alv->get_columns( ).
*
*   set the Column optimization
    lo_cols->set_optimize( 'X' ).

*   Set the Cell Type
    try.
        lo_cols->set_cell_type_column( 'I_CELLTYPE' ).
      catch cx_salv_data_error.                         "#EC NO_HANDLER
    endtry.

  endmethod.                    "SET_COLUMNS

*
*
endclass.                    "lcl_report IMPLEMENTATION

No comments:

Post a Comment

Turn off default MS Word Editor in Smartforms SAP S/4HANA

Fix SMARTFORMS and SE71 Access Issue in S/4HANA - Enhancement Solution Fix SMARTFORMS and SE71 Access Issue in S/...