Skip to Content
avatar image
Former Member

Alv grid (oops):hiding empty column

Hi ,

I want to hide the column when the entire column is empty .

In the above screen shot  column Id of a switch ,switch position etc  is empty ,i need  to hide column when it is empty  .

scn.PNG (25.6 kB)
scn1.PNG (23.5 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Sep 12, 2013 at 09:57 AM

    You can hide a column by using the set_technical property, in the column settings.

    Below are code examples from a program I wrote before. Just an example, not ready to use.

    *   Get the columns from ALV Table    lr_columns_table = <table>->get_columns( ).    if lr_columns_table is not initial.      refresh : lt_t_column_ref.      lt_t_column_ref = lr_columns_table->get( ).............*     Individual Column Properties.      me->column_settings( exporting im_column_ref         = lt_t_column_ref                                     im_rf_columns_table   = lr_columns_table                                     im_table              = <table> ).    endif.

    In the Column_settings method, you loop over the columns and check if the column is completely empty.

    If that is the case, you can use the method SET_TECHNICAL to remove the column from the table.

        loop at im_column_ref into ls_s_column_ref.      try.          lr_column_table ?=            im_rf_columns_table->get_column( ls_s_column_ref-columnname ).        catch cx_salv_not_found into rf_root.          me->handle_error( exporting rf_oref = rf_root ).      endtry....


    *       Remove columns that are not required when empty        if lr_column_table->get_columnname( ) in lra_hide_when_empty.          lp_return = me->is_empty_column( im_column = ls_s_column_ref-columnname                                                im_table  = main_list ).          if lp_return = abap_true.            lr_column_table->set_technical( if_salv_c_bool_sap=>true ).          endif.        endif.
    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 12, 2013 at 10:04 AM

    HI,

       we can update field catalog as

     

        loop at lt_table into ls_tab where column is not initial.

           endloop.

           if sy-subrc <> 0.

              ls_fcat-no_out = c_true.   " ls_fcat-tech = c_true

            endif.

    Regards,

    Praveen

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 12, 2013 at 10:27 AM

    Hi Juneed,

    Before passing  final internal table to display , check the internal table fields whether all fields having values in each row or not .If any one column field not having any data,just leave that field and pass remaining fields to another internal table and display it.so that no need of hiding the columns.

    But if you want to hide the column means ,check the final internal table empty fields and hide using class CL_SALV_COLUMNS_TABLE and method REMOVE_COLUMN.If helpful please reward the points.

    Regards
    Ashok P

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Sep 12, 2013 at 11:10 AM

    This solution works perfectly for me.

    While creating the field catalog, add the following statement to each field.

    data : flag.

       lv_pos = lv_pos + 1.
       CLEAR gw_alv_fieldcat.
       gw_alv_fieldcat-fieldname = 'ORT02'.
       gw_alv_fieldcat-tabname   = 'GT_OUT'.
       gw_alv_fieldcat-seltext_l = text-h11.
       gw_alv_fieldcat-col_pos   = lv_pos.
       gw_alv_fieldcat-outputlen = 10.

      clear flag.
        loop at gt_out into gw_out where ort02 ne '  '.
           flag = 'X'.
           exit.
        endloop .

    * With the first non initial value, it will exit the loop. Hence there will not be more than one loop for each field in the field catalog.

        if flag is INITIAL.
          gw_alv_fieldcat-no_out = 'X'.
        endif. 

    APPEND gw_alv_fieldcat TO gt_alv_fieldcat.

    You will be happy with the output.  (Just make sure that data is retrieved before creation of field catalog )

    Add comment
    10|10000 characters needed characters exceeded