2007 Apr 11 11:58 AM
Hi!
I have a CL_GUI_ALV_GRID instance displaying the data of an itab. 2 columns are set to have optimized width via field catalogue. The first time the 2 columns are ok in width but when I add some rows and call refresh_table_display the width is not changed (I don't do a soft refresh). Can you help me out?
2007 Apr 11 12:03 PM
did u try <b>CWIDTH_OPT</b> field of the layout structure (not sure if this works,w orth a try)
2007 Apr 11 12:09 PM
I dont' want all columns to be optimized...only the first 2 ones. I guess this change will take effect for all columns, wont't it?
2007 Apr 11 12:14 PM
In fieldcatalog also u have an option for column optimization for structure LVC_S_FCAT , try this for the first 2 fields
<b>wa_fieldcat-COL_OPT = 'X'.</b>
Message was edited by:
Chandrasekhar Jagarlamudi
2007 Apr 11 12:19 PM
yep...that's the one I set. And like I posted above it works for initial display. But not when I refresh the table.
2007 Apr 11 1:28 PM
Hello Michael
Executing the following sample report I do not see the behaviour you have described. Irrespective of whether method go_grid->REFRESH_TABLE_DISPLAY is called at PBO or PAI the column width remains stable.
*&---------------------------------------------------------------------*
*& Report ZUS_SDN_ALVGRID_EDITABLE_1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zus_sdn_alvgrid_editable_1.
DATA:
gd_okcode TYPE ui_func,
*
gt_fcat TYPE lvc_t_fcat,
go_docking TYPE REF TO cl_gui_docking_container,
go_grid1 TYPE REF TO cl_gui_alv_grid.
DATA:
gt_knb1 TYPE STANDARD TABLE OF knb1.
PARAMETERS:
p_bukrs TYPE bukrs DEFAULT '2000' OBLIGATORY.
START-OF-SELECTION.
SELECT * FROM knb1 INTO TABLE gt_knb1
WHERE bukrs = p_bukrs.
* Create docking container
CREATE OBJECT go_docking
EXPORTING
parent = cl_gui_container=>screen0
ratio = 90
EXCEPTIONS
OTHERS = 6.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Create ALV grid
CREATE OBJECT go_grid1
EXPORTING
i_parent = go_docking
EXCEPTIONS
OTHERS = 5.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Build fieldcatalog and set hotspot for field KUNNR
PERFORM build_fieldcatalog_knb1.
* Display data
CALL METHOD go_grid1->set_table_for_first_display
CHANGING
it_outtab = gt_knb1
it_fieldcatalog = gt_fcat
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Link the docking container to the target dynpro
CALL METHOD go_docking->link
EXPORTING
repid = syst-repid
dynnr = '0100'
* CONTAINER =
EXCEPTIONS
OTHERS = 4.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* ok-code field = GD_OKCODE
CALL SCREEN '0100'.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'STATUS_0100'.
* SET TITLEBAR 'xxx'.
** CALL METHOD go_grid1->refresh_table_display
*** EXPORTING
*** IS_STABLE =
*** I_SOFT_REFRESH =
** EXCEPTIONS
** FINISHED = 1
** others = 2
** .
** IF sy-subrc <> 0.
*** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
** ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.
CASE gd_okcode.
WHEN 'BACK' OR
'END' OR
'CANC'.
SET SCREEN 0. LEAVE SCREEN.
WHEN OTHERS.
CALL METHOD go_grid1->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
EXCEPTIONS
FINISHED = 1
others = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDCASE.
CLEAR: gd_okcode.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
*& Form BUILD_FIELDCATALOG_KNB1
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM build_fieldcatalog_knb1 .
* define local data
DATA:
ls_fcat TYPE lvc_s_fcat.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
* I_BUFFER_ACTIVE =
i_structure_name = 'KNB1'
* I_CLIENT_NEVER_DISPLAY = 'X'
* I_BYPASSING_BUFFER =
* I_INTERNAL_TABNAME =
CHANGING
ct_fieldcat = gt_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT gt_fcat INTO ls_fcat
WHERE ( fieldname = 'ZUAWA' ).
ls_fcat-col_opt = abap_true.
MODIFY gt_fcat FROM ls_fcat.
ENDLOOP.
ENDFORM. " BUILD_FIELDCATALOG_KNB1
Regards
Uwe
2007 Apr 11 2:41 PM
ok here is exactly what I got:
I got a class that implements the event handlers for my customized alv grid. After an insert or deletion (maybe that's an important point, I do not use the standard add and delete row buttons, I added some custom ones because I need to show a selection popup before adding, see code below) I do a refresh on the grid. The instance of the alv grid is created in the pbo module. There I also create the fieldcat, set the col_opt component for the 2 columns and register the event handler methods. Now, when a new row is inserted with content wider than the column the width is not adjusted. It stays at it's wide at which it has been set during initialization.
METHOD handle_user_command.
DATA: ltab_selected_rows TYPE lvc_t_row,
lstr_selected_rows TYPE lvc_s_row,
l_lines TYPE i,
lstr_forecast LIKE LINE OF gtab_forecast,
lstr_picklist LIKE LINE OF gtab_picklist.
CALL METHOD g_alv_forecast->get_selected_rows
IMPORTING
et_index_rows = ltab_selected_rows.
CASE e_ucomm.
WHEN 'INS_ROW'.
CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
EXPORTING
i_title = 'TEST'
i_tabname = 'GTAB_PICKLIST'
it_fieldcat = gtab_picklist_fieldcat
i_callback_user_command = 'PICKLIST_USER_COMMAND'
i_selection = ' '
i_callback_program = sy-repid
TABLES
t_outtab = gtab_picklist
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF g_selectedpsp NE space.
CLEAR lstr_forecast.
lstr_forecast-psp_element = g_selectedpsp.
g_selectedpsp = space.
READ TABLE gtab_picklist INTO lstr_picklist
WITH KEY id = lstr_forecast-psp_element.
lstr_forecast-description = lstr_picklist-desc.
DESCRIBE TABLE ltab_selected_rows LINES l_lines.
IF l_lines NE 0.
READ TABLE ltab_selected_rows INTO lstr_selected_rows INDEX 1.
INSERT lstr_forecast INTO gtab_forecast INDEX lstr_selected_rows-index.
ELSE.
INSERT lstr_forecast INTO gtab_forecast INDEX 1.
ENDIF.
ENDIF.
WHEN 'DEL_ROW'.
SORT ltab_selected_rows BY index DESCENDING.
LOOP AT ltab_selected_rows INTO lstr_selected_rows.
DELETE gtab_forecast INDEX lstr_selected_rows-index.
ENDLOOP.
WHEN 'REFRESH'.
ENDCASE.
DATA stbl TYPE lvc_s_stbl.
CALL METHOD g_alv_forecast->refresh_table_display
EXPORTING
is_stable = stbl
i_soft_refresh = space.
ENDMETHOD. "handle_user_command_forecast
2007 Apr 11 2:49 PM
Hello Michael
Perhaps it helps if set the fieldcatalog again (using method <b>go_grid->set_frontend_fieldcatalog</b>) <b>AFTER </b>calling method go_grid->refresh_table_display.
Regards
Uwe
2007 Apr 11 2:55 PM
I did a GET_FRONTEND_FIELDCATALOG to check if the columns still have the col_opt flag after the refresh and they definitly still have the flag but the size is not adjusted.
I tried now to reset the catalog using set_frontend_fieldcatalog but it doesn't work. The size is not readjusted.
CALL METHOD g_alv_forecast->refresh_table_display
EXPORTING
is_stable = stbl
i_soft_refresh = space.
data testcat type lvc_t_fcat.
call method g_alv_forecast->GET_FRONTEND_FIELDCATALOG
IMPORTING
ET_FIELDCATALOG = testcat .
.
call method g_alv_forecast->set_frontend_fieldcatalog
exporting
it_fieldcatalog = testcat.
there are methods for optimizing columns but all are private or protected - not accessible for my event handler.
when I use "optimize width" of the alv grid control the output looks like it should...but I can't manage it programmatically
2007 Apr 12 7:48 AM
Hi, I just solved it. Not exactly the way it was before but now it works. Here for your information is how I did it: as I said, there are protected methods for column wide optimization. All I did was creating a 'new' alv_grid_class which is inherited from cl_gui_alv_grid. The event handler methods register on its own instance and now I'm able to call the protected methods...and it works.
2012 Oct 29 9:43 AM
Hi.
I came across a similar problem. The scenario is:
1. building the field catalog in gt_fieldcat and setting layout-cwidth_opt = 'X'.
2. set_table_for_first_display
3. modifying the field catalog table and calling set_frontend_fieldcatalog
4. refresh_table_display
After refresh, the optimization was lost.
Solution:
Before modifying the field catalog table, call get_frontend_fieldcatalog. It works fine and is simpler then the solution above.
2021 Sep 24 3:46 PM
FORM FRM_REFRESH_ALV.
DATA:LS_STABLE TYPE LVC_S_STBL.
CALL METHOD G_GRID->SET_FRONTEND_FIELDCATALOG
EXPORTING
IT_FIELDCATALOG = GT_FIELDCAT.
CALL METHOD G_GRID->SET_FRONTEND_LAYOUT
EXPORTING
IS_LAYOUT = GS_LAYOUT.
LS_STABLE-ROW = 'X'. "固定行
LS_STABLE-COL = 'X'. "固定列
CALL METHOD G_GRID->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = LS_STABLE.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " FRM_REFRESH_ALV
2024 Aug 24 12:25 PM
This is my solution. layout-cwidth_opt contained the value 1 and I initially set the value X (abap_true) and here I had to do it again.
METHOD on_data_changed_finished.
IF e_modified = abap_false.
RETURN.
ENDIF.
sender->get_frontend_layout( IMPORTING es_layout = DATA(layout) ).
layout-cwidth_opt = abap_true.
sender->set_frontend_layout( is_layout = layout ).
DATA(stable) = VALUE lvc_s_stbl( row = abap_true
col = abap_true ).
sender->refresh_table_display( is_stable = stable ).
ENDMETHOD.