# CWM Table concept

Table widget and manupulation with data in this widget need same behavior across whole YaST.

The CWM Table widget exists in order to make development of modules using this UI concept consistent. It contains few most used buttons and few automatic manipulation features.

The CWM Table widget allows Add/delete buttons, Edit button, Up/down reorder buttons and custom button, which developer set to what they need. Automatic edit action after double-click on table collumn. After reorder is still selected same item, even if is moved. Reordering handlers is called only if cell really can move in choosed direction. Whole table Enable/Disable and automatic enable/disable reordering button on collums which doesn't allow move in that direction.

## Simple example for CWM Table

This example show basic features of CWM table and also basic how to use.

Usage of the CWM Table widget - basic case

    {
          import "CWM";
          import "CWMTable";
          import "Popup";
          import "Wizard";

          list<string> items = [];
          integer counter =0;

          void redraw_table(list<string> values){
            list<term> table_items = maplist(string s, values, {
              return `item(`id(s),s);
            });
            UI::ChangeWidget(`id(`_tw_table),`Items, table_items);
          }

          symbol add_handle(string key, map event){
            counter = counter +1;
            items = add(items,sformat("item %1",counter));
            redraw_table(items);
            return nil;
          }

          symbol edit_handle(string key, map event){
              Popup::Warning("edit");
            return nil;
          }

          symbol delete_handle(string key, map event){
              Popup::Warning("delete");
            return nil;
          }

          symbol updown_handle(string key, map event, boolean up, integer index){
            integer second = up ? (index-1):(index+1);
            y2milestone("updown with up %1 and index %2 and second %3",up,index,second);
            string value = items[second]:"";
            items[second] = items[index]:"";
            items[index] = value;
            redraw_table(items);
            return nil;
          }

          symbol custom_handle(string key, map event){
              Popup::Warning("Custom button");
            return nil;
          }

          map<string,any> table = CWMTable::CreateTableDescr(
            $[
              "add_delete_buttons" : true,
              "edit_button" : true,
              "up_down_buttons" : true,
              "custom_button" : true,
              "custom_button_name" : "Additional button",
              "custom_handle" : custom_handle,
              "header" : `header("id"),
              "edit":edit_handle,
              "delete":delete_handle,
              "add" : add_handle,
              "updown" : updown_handle,
            ],
            $[
              "help" : "",
            ]
          );

          Wizard::CreateDialog();
          symbol ret = CWM::ShowAndRun($[
            "widget_names" : ["table"],
            "widget_descr" : $[ "table" : table],
            "contents" : `VBox("table"),
            "caption" : "test"
          ]);
    }

### Creating the table container

For getting the initial map the function map<string,any> CWMTable::CreateTableDescr () can be used. It takes as a parameters a map specifying the attributes of the table and a map specifying additional settings that will be merged to the description map (see below). Currently supported attributes are:

- "add_delete_buttons" : boolean - if the Add and Delete buttons are wanted, set to true. If false, they will not be shown. Default (if key not present) is true.

- "edit_button" : boolean - if the Edit button is wanted, set to true. If false, it will not be shown. Default (if key not present) is true.

- "up_down_buttons" : boolean - if the Up and Down (reorder) buttons are wanted, set to true. Otherwise, they will not be shown. Default (if not present) is false.

- "custom_button" : boolean - if the Custom button are wanted, set to true. Otherwise, they will not be shown. Default (if not present) is false.

- "custom_button_name" : string - label for the Custom button. Default (if not present) is "Custom button".

- "custom_handle" : symbol(string,map) - handler for the Custom button. Default (if not present) is empty handler (do nothing).

- "add" : symbol(string,map) - handler for the Add button. Default (if not present) is empty handler (do nothing).

- "delete" : symbol(string,map) - handler for the Delete button. Default (if not present) is empty handler (do nothing).

- "edit" : symbol(string,map) - handler for the Edit button. Default (if not present) is empty handler (do nothing).

- "updown" : symbol(string,map, boolean, integer) - handler for the Updown button. Extra parameters is boolean if button is Up button and index of selected table collumn. Default (if not present) is empty handler (do nothing).

- "header" : term - header term for table. It is mandatory key for CWM Table. Default (if not present) is nil and fail of widget.

For id of the widgets related to the table see .

### Setting the widget map

This is the same as for normal widgets. Set the keys according to your needs. Only doesn't overwrite handle function, as it make all handling stuff

## Reserved UI events

Some UI events (return values of UI::UserInput ()) are used internally by the handling mechanism, and can't be used for other widgets.

The table widget contains following ids:

- `_tw_add - Add button - don't use although it is not present

- `_tw_edit - Edit button

- `_tw_delete - Delete button - don't use although it is not present

- `_tw_table - The table

- `_tw_up - The Up button

- `_tw_down - The Down button

- `_tw_custom - The Custom button