ABAP-CL_GUI_ALV_TREE 选择框

https://answers.sap.com/questions/2237965/catch-checkbox-in-heirachycal-alv.html

 1 METHOD handle_checkbox_change .
 2 * define local data
 3   DATA:
 4     lw_child            TYPE lvc_nkey,
 5     lt_children         TYPE lvc_t_nkey,
 6 *
 7     l_wa_outtab_line    TYPE zfr_calc_marge_detail,
 8     l_wa_item_layout    TYPE lvc_s_layi,
 9     lt_item_layout      TYPE lvc_t_layi,
10     l_wa_item_layout_c  TYPE lvc_s_laci,
11     lt_item_layout_c    TYPE lvc_t_laci.
12 
13 
14 
15 " The user marks or unmarks a checkbox on the ALV tree. The status
16 " (marked / unmarked) is "inherited" to the child nodes.
17 node_key = cl_alv_tree_base=>c_virtual_root_node.
18 
19 " Get sub-tree of selected node (i.e. the node where the checkbox was changed)
20   CALL METHOD sender->get_subtree
21     EXPORTING
22       i_node_key       = node_key
23     IMPORTING
24       et_subtree_nodes = lt_children.
25 " NOTE: the sender is the ALV tree instance
26 
27 
28   LOOP AT lt_children INTO lw_child.
29     REFRESH: lt_item_layout.
30     CLEAR:   l_wa_outtab_line.
31 
32 *   Knoten und Item-Layout lesen
33     CALL METHOD sender->get_outtab_line
34       EXPORTING
35         i_node_key     = lw_child
36       IMPORTING
37         e_outtab_line  = l_wa_outtab_line
38 *        E_NODE_TEXT    =
39         et_item_layout = lt_item_layout
40 *        ES_NODE_LAYOUT =
41       EXCEPTIONS
42         node_not_found = 1
43         OTHERS         = 2.
44     IF sy-subrc <> 0.
45 *     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
46 *                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
47     ENDIF.
48 
49 
50     READ TABLE lt_item_layout INTO l_wa_item_layout
51          WITH KEY fieldname = cl_gui_alv_tree=>c_hierarchy_column_name
52                   class     = cl_gui_column_tree=>item_class_checkbox.
53     IF ( syst-subrc = 0 ).
54       l_wa_item_layout-chosen = checked.  " 'status' of changed checkbox
55       MODIFY lt_item_layout FROM l_wa_item_layout INDEX syst-tabix.
56 
57 
58       REFRESH: lt_item_layout_c.
59       LOOP AT lt_item_layout INTO l_wa_item_layout.
60         MOVE-CORRESPONDING l_wa_item_layout TO l_wa_item_layout_c.
61         l_wa_item_layout_c-u_chosen = 'X'.  " update CHOSEN field
62         APPEND l_wa_item_layout_c TO lt_item_layout_c.
63       ENDLOOP.
64 
65 
66       CALL METHOD sender->change_node
67         EXPORTING
68           i_node_key     = lw_child
69           i_outtab_line  = l_wa_outtab_line
70 *          IS_NODE_LAYOUT =
71           it_item_layout = lt_item_layout_c
72 *          I_NODE_TEXT    =
73 *          I_U_NODE_TEXT  =
74         EXCEPTIONS
75           node_not_found = 1
76           OTHERS         = 2.
77       IF sy-subrc <> 0.
78 *       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
79 *                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
80       ENDIF.
81 
82     ENDIF.
83 
84   ENDLOOP.
85 
86 " Sending changes to the frontend
87   CALL METHOD sender->frontend_update.
88 
89 
90 ENDMETHOD.
  1 DATA: gr_tree type ref to CL_SALV_TREE,
      nodes TYPE REF TO cl_salv_nodes, 2 3 node TYPE REF TO cl_salv_node, 4 5 item TYPE REF TO cl_salv_item, 6 7 text TYPE lvc_value. 8 9 node = nodes->add_node( related_node = ip_node 10 11 data_row = gs_tree 12 13 row_style = lv_style 14 15 text = text 16 17 relationship = cl_gui_column_tree=>relat_last_child ). 18 19 item = node->get_hierarchy_item( ). 20 21 item->set_type( if_salv_c_item_type=>checkbox ). 22 23 IF NOT gv_flag IS INITIAL. 24 25 item->set_checked( abap_true ). 26 27 ENDIF. 28 29 item->set_editable( abap_true ). 30 31 ENDIF. 32 33 &---- 34 35 *& Form CHECKBOX_CHANGE 36 37 &---- 38 39 text 40 41 ---- 42 43 -->IV_COLUMNNAME Columnname 44 45 -->IV_NODE_KEY Node key 46 47 -->IV_CHECKED Falg for check box 48 49 ---- 50 51 FORM checkbox_change USING iv_columnname TYPE lvc_fname 52 53 iv_node_key TYPE salv_de_node_key 54 55 iv_checked TYPE sap_bool. 56 57 DATA: lv_tree TYPE REF TO data, 58 59 ls_tree TYPE rpuben42_alv1. 60 61 FIELD-SYMBOLS: <fs> TYPE ANY. 62 63 DATA: nodes TYPE REF TO cl_salv_nodes, 64 65 item1 TYPE REF TO cl_salv_item, 66 67 item TYPE REF TO cl_salv_item, 68 69 tree TYPE salv_t_nodes, 70 71 tree1 TYPE salv_t_nodes, 72 73 leaf TYPE salv_s_nodes, 74 75 leaf1 TYPE salv_s_nodes. 76 77 get all existing nodes 78 79 TRY. 80 81 nodes = gr_tree->get_nodes( ). 82 83 tree = nodes->get_all_nodes( ). 84 85 CATCH cx_salv_msg. 86 87 EXIT. 88 89 ENDTRY. 90 91 READ TABLE tree INTO leaf WITH KEY key = iv_node_key. 92 93 CREATE DATA lv_tree TYPE rpuben42_alv1. 94 95 IF iv_checked = abap_true. 96 97 TRY. 98 99 tree1 = leaf-node->get_subtree( ). 100 101 item1 = leaf-node->get_hierarchy_item( ). 102 103 item1->set_checked( abap_true ). 104 105 lv_tree = leaf-node->get_data_row( ). 106 107 ASSIGN lv_tree->* TO <fs>. 108 109 ls_tree = <fs> . 110 111 MOVE ls_tree TO gs_tree. 112 113 PERFORM update_table USING iv_checked. 114 115 CLEAR gs_tree. 116 117 CATCH cx_salv_msg. 118 119 ENDTRY. 120 121 LOOP AT tree1 INTO leaf1. 122 123 item = leaf1-node->get_hierarchy_item( ). 124 125 IF item->is_checked( ) NE abap_true. 126 127 item->set_checked( abap_true ). 128 129 lv_tree = leaf1-node->get_data_row( ). 130 131 ASSIGN lv_tree->* TO <fs>. 132 133 ls_tree = <fs> . 134 135 MOVE ls_tree TO gs_tree. 136 137 PERFORM update_table USING iv_checked. 138 139 CLEAR gs_tree. 140 141 ENDIF. 142 143 ENDLOOP. 144 145 ELSE. 146 147 TRY. 148 149 tree1 = leaf-node->get_subtree( ). 150 151 CATCH cx_salv_msg. 152 153 ENDTRY. 154 155 LOOP AT tree1 INTO leaf1. 156 157 item = leaf1-node->get_hierarchy_item( ). 158 159 item->set_checked( abap_false ). 160 161 lv_tree = leaf1-node->get_data_row( ). 162 163 ASSIGN lv_tree->* TO <fs>. 164 165 ls_tree = <fs> . 166 167 MOVE ls_tree TO gs_tree. 168 169 PERFORM update_table USING iv_checked. 170 171 CLEAR gs_tree. 172 173 ENDLOOP. 174 175 ENDIF. 176 177 ENDFORM. " CHECKBOX_CHANGE
原文地址:https://www.cnblogs.com/ricoo/p/15371774.html