SAP - ABAP : Dynamic ALV Cell Style

report zalv_edit.

class lcl_report definition.
*
  public section.
*
*   Final output table
    typesbegin 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.
    datat_vbak type standard table of ty_vbak.

*   ALV reference
    datao_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.
  datalo_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.
    datalt_celltype type salv_t_int4_column.
    datals_celltype like line of lt_celltype.
    loop at t_vbak assigning <lfs_vbak>.
      clearlt_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
    datalx_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
    datalo_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

Comments

Popular posts from this blog

IT Asset Management Dengan PHP MySQL

PHP MySql CRUD Dengan Konsep MVC

Cara Sederhana Multi Insert Data Dengan PHP - MySQL