使用REUSE_ALV_GRID_DISPLAY时,如何实现仅单列不启用列宽优化?
Answers to REUSE_ALV_GRID_DISPLAY Column Width Optimization Questions
Great questions—these are super common tweaks when working with ALV grids, and I’ve tackled both scenarios in ABAP projects before. Let’s break them down with practical steps:
1. Optimize Column Width for a Single Column Only
The global layout-colwidth_optimize parameter controls all columns at once, but you can override this for individual fields using the field catalog’s col_opt attribute. Here’s how to make it work:
- First, set the global layout option to not optimize all columns (
layout-colwidth_optimize = ' '). - Then, find the specific column in your field catalog and set
col_opt = 'X'to enable optimization just for that field.
Example code snippet:
DATA: gs_layout TYPE slis_layout_alv, gt_fieldcat TYPE slis_t_fieldcat_alv, gs_fieldcat TYPE slis_fieldcat_alv. " First populate your field catalog (via merge or manual setup) CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING i_program_name = sy-repid i_internal_tabname = 'GT_DATA' i_inclname = sy-repid CHANGING ct_fieldcat = gt_fieldcat. " Enable optimization ONLY for your target column (e.g., 'CUSTOMER_NAME') LOOP AT gt_fieldcat INTO gs_fieldcat WHERE fieldname = 'CUSTOMER_NAME'. gs_fieldcat-col_opt = 'X'. MODIFY gt_fieldcat FROM gs_fieldcat. ENDLOOP. " Disable global optimization gs_layout-colwidth_optimize = ' '. " Call the ALV grid CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = gs_layout it_fieldcat = gt_fieldcat TABLES t_outtab = gt_data.
2. Disable Optimization for a Single Column (Optimize All Others)
This is the reverse scenario: you want global optimization turned on, but exclude one specific column. The same col_opt parameter comes to the rescue—just flip the values:
- Set
layout-colwidth_optimize = 'X'to enable optimization for all columns by default. - For the column you want to exclude, set
col_opt = ' 'in the field catalog. This overrides the global setting for that field.
Example code for your old report:
DATA: gs_layout TYPE slis_layout_alv, gt_fieldcat TYPE slis_t_fieldcat_alv, gs_fieldcat TYPE slis_fieldcat_alv. " Assume your existing code already populates gt_fieldcat " ... (your existing field catalog setup logic) " Turn on global column width optimization gs_layout-colwidth_optimize = 'X'. " Disable optimization for the excluded column (e.g., 'LONG_DESCRIPTION') LOOP AT gt_fieldcat INTO gs_fieldcat WHERE fieldname = 'LONG_DESCRIPTION'. gs_fieldcat-col_opt = ' '. MODIFY gt_fieldcat FROM gs_fieldcat. ENDLOOP. " Call the ALV grid with updated settings CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING i_callback_program = sy-repid is_layout = gs_layout it_fieldcat = gt_fieldcat TABLES t_outtab = gt_data.
Quick Tips
- The
col_optfield in the field catalog always takes priority over the globallayout-colwidth_optimizesetting—this is SAP’s official way to fine-tune per-column behavior. - If your old report uses a dynamic field catalog, make sure the loop to modify
col_optruns after the catalog is fully populated (like right afterREUSE_ALV_FIELDCATALOG_MERGE).
内容的提问来源于stack exchange,提问作者Kevin Holtkamp
相关产品推荐
相关产品推荐

