Definition
Action is an object which contains the type and payload
{
type: string,
...payload
}
Predefined actions
All the actions below are processed by kaReducer. Every action has an action creator: a function which returns the action. There are two ways to dispatch a predefined action:
// a table with its own state: the action creators are the methods of the instance
const table = useTable();
table.selectRow(1);
table.updatePageIndex(2);
// a controlled table, or dispatch of a child component
import { selectRow } from 'ka-table/actionCreators';
dispatch(selectRow(1)); // the same as dispatch({ type: 'SelectRow', rowKeyValue: 1 })
The column “UI” shows the actions which the table dispatches itself in response to the events of the UI (see UI Actions).
Editing
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'OpenEditor'openEditor( |
{ |
yes | Add cell to editableCells |
'OpenRowEditors'openRowEditors( |
{ |
no | Add row cells to editableCells |
'OpenAllEditors'openAllEditors() |
{ |
no | Adds each data cell to editableCells |
'CloseEditor'closeEditor( |
{ |
yes | Removes cell from editableCells note: when EditingMode.Cell is enabled table calls CloseEditor action each time editor loses focus |
'CloseRowEditors'closeRowEditors( |
{ |
no | Removes row cells from editableCells |
'UpdateEditorValue'updateEditorValue( |
{ |
yes | Update editorCell in editableCells |
'UpdateCellValue'updateCellValue( |
{ |
yes | Update data |
'SaveRowEditors'saveRowEditors( |
{ |
no | Saves editor values to data and removes row cells from editableCells. Settings is optional: validate - validate values before save, do not save and do not close if validation is unsuccessful. |
'SaveAllEditors'saveAllEditors() |
{ |
no | Saves editableCells editorValues to data |
'Validate'validate() |
{ |
no | Adds/removes validationMessage to/from editableCells items |
'ShowNewRow'showNewRow() |
{ |
no | Add new row cells to editableCells |
'HideNewRow'hideNewRow() |
{ |
no | Removes row cells from editableCells |
'SaveNewRow'saveNewRow( |
{ |
no | Saves editor values to the new item of data and removes row cells from editableCells. Settings is optional: validate - validate values before save, do not save if validation is unsuccessful. |
Data and loading
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'UpdateData'updateData( |
{ |
no | Update data |
'UpdateRow'updateRow( |
{ |
no | Replaces the row with the same key in data |
'InsertRow'insertRow( |
{ |
no | Inserts row to specific position in the data |
'DeleteRow'deleteRow( |
{ |
no | Deletes row from data and its key from selectedRows, detailsRows, treeGroupsExpanded, editableCells and focused |
'ReorderRows'reorderRows( |
{ |
yes | Place rowKeyValue to the position of targetRowKeyValue |
'LoadData'loadData() |
{ |
no | Notifies grid about time to reload its data |
'ShowLoading'showLoading( |
{ |
no | Set loading = true and updates the text if text is specified |
'HideLoading'hideLoading() |
{ |
no | Set loading = false |
Columns
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'ShowColumn'showColumn( |
{ |
no | Sets visible=true for specific column |
'HideColumn'hideColumn( |
{ |
no | Sets visible=false for specific column |
'InsertColumn'insertColumn( |
{ |
no | Inserts column to specific position in the columns |
'MoveColumnBefore'moveColumnBefore( |
{ |
no | Move columnKey to position before targetColumnKey in columns |
'MoveColumnToIndex'moveColumnToIndex( |
{ |
no | Move columnKey to index position in columns |
'ReorderColumns'reorderColumns( |
{ |
yes | Place columnKey to the position of targetColumnKey |
'ResizeColumn'resizeColumn( |
{ |
yes | Change column width |
Sorting, filtering and search
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'UpdateSortDirection'updateSortDirection( |
{ |
yes | Update sortDirection to the opposite for specific column |
'UpdateFilterRowValue'updateFilterRowValue( |
{ |
yes | Update filterRowValue for specific column |
'UpdateFilterRowOperator'updateFilterRowOperator( |
{ |
no | Update filterRowOperator for specific column |
'UpdateHeaderFilterValues'updateHeaderFilterValues( |
{ |
yes | Fires when user selects/deselects value in the header filter |
'UpdateHeaderFilterPopupState'updateHeaderFilterPopupState( |
{ |
yes | Shows/hides the header filter popup |
'UpdateHeaderFilterSearchValue'updateHeaderFilterSearchValue( |
{ |
yes | Updates the search text of the header filter popup |
'UpdatePopupPosition'updatePopupPosition( |
{ |
yes | Fires when header filter popup is shown |
'ClearAllFilters'clearAllFilters() |
{ |
no | Clears all table filters |
'Search'search( |
{ |
no | Change searchText option |
Grouping, tree and details rows
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'GroupColumn'groupColumn( |
{ |
yes | Groups columnKey |
'UngroupColumn'ungroupColumn( |
{ |
yes | Ungroups columnKey |
'UpdateGroupsExpanded'updateGroupsExpanded( |
{ |
yes | Add/remove group id to/from groupsExpanded |
'UpdateTreeGroupsExpanded'updateTreeGroupsExpanded( |
{ |
yes | Adds/removes rowKeyValue to/from treeGroupsExpanded |
'ShowDetailsRow'showDetailsRow( |
{ |
no | Adds the row key to detailsRows |
'HideDetailsRow'hideDetailsRow( |
{ |
no | Removes the row key from detailsRows |
Paging and scrolling
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'UpdatePageIndex'updatePageIndex( |
{ |
yes | Update paging.pageIndex option and show new page data |
'UpdatePageSize'updatePageSize( |
{ |
yes | Update paging.pageSize, the first row of the page stays shown |
'UpdatePagesCount'updatePagesCount( |
{ |
no | Update paging.pagesCount (remote paging) |
Selection
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'SelectRow'selectRow( |
{ |
no | Add row id to selectedRows |
'SelectRows'selectRows( |
{ |
no | Add rows ids to selectedRows |
'SelectRowsRange'selectRowsRange( |
{ |
no | rowKeyValueFrom, rowKeyValueTo describe start and end of visible elements range, all ids added to selectedRows |
'SelectSingleRow'selectSingleRow( |
{ |
no | Set selectedRows as an array with one row id |
'SelectAllRows'selectAllRows() |
{ |
no | Add all items ids of data to selectedRows |
'SelectAllFilteredRows'selectAllFilteredRows() |
{ |
no | Add all filtered items ids of data to selectedRows |
'SelectAllVisibleRows'selectAllVisibleRows() |
{ |
no | Add all visible items ids of data to selectedRows |
'DeselectRow'deselectRow( |
{ |
no | Removes row id from selectedRows |
'DeselectRows'deselectRows( |
{ |
no | Removes rows ids from selectedRows |
'DeselectAllRows'deselectAllRows() |
{ |
no | Clears selectedRows |
'DeselectAllFilteredRows'deselectAllFilteredRows() |
{ |
no | Remove all filtered items ids of data to selectedRows |
'DeselectAllVisibleRows'deselectAllVisibleRows() |
{ |
no | Remove all visible items ids of data to selectedRows |
Focus
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'SetFocused'setFocused( |
{ |
yes | Set focused property: the cell or the group row focused by a click, Tab or the keyboard navigation |
'ClearFocused'clearFocused() |
{ |
no | Sets focused as undefined |
'MoveFocusedUp'moveFocusedUp( |
{ |
no | Move focused one cell up |
'MoveFocusedDown'moveFocusedDown( |
{ |
no | Move focused one cell down |
'MoveFocusedLeft'moveFocusedLeft( |
{ |
no | Move focused one cell left |
'MoveFocusedRight'moveFocusedRight( |
{ |
no | Move focused one cell right |
Single action
| Type and action creator | Action | UI | kaReducer |
|---|---|---|---|
'SetSingleAction'setSingleAction( |
{ |
no | Set singleAction property |
'ClearSingleAction'clearSingleAction() |
{ |
no | Sets singleAction as undefined |
TypeScript
In TypeScript the actions of the table are TableAction (ka-table/types): a union which is narrowed by action.type, so onDispatch, customReducer and dispatch of a controlled table get typed actions:
import { ActionType } from 'ka-table/enums';
const table = useTable({
onDispatch: (action) => {
if (action.type === ActionType.UpdatePageIndex) {
loadPage(action.pageIndex); // number
}
},
});
Custom actions
Custom actions also can be dispatched and processed by dispatch function or custom reducer:
// declare const and action creator (optional)
export const CUSTOM_ACTION_TYPE = "CUSTOM_ACTION_TYPE";
export const customAction = (myData) => ({
type: CUSTOM_ACTION_TYPE, // required
myData // optional payload
});
// dispatch custom action
dispatch(customAction({ value: 5 }));
const [tableProps, changeTableProps] = useState(tablePropsInit);
// process action in the dispatch or pass it to custom reducer
const dispatch: DispatchFunc = (action) => {
switch (action.type) {
case CUSTOM_ACTION_TYPE:
// action.myData.value == 5
// do action things or pass it to the custom reducer
break;
default:
changeTableProps((prevState) => kaReducer(prevState, action));
}
};
return (
<Table
{...tableProps}
dispatch={dispatch} // pass dispatch to be able to obtain actions from Table
/>
);
In TypeScript dispatch accepts the actions of the table and the actions of your own code (CustomAction: a type and any other fields). onDispatch and customReducer of useTable get TableAction: add the type of your actions to the parameter.
interface LoadMoreAction { type: 'LOAD_MORE' }
const table = useTable({
onDispatch: (action: TableAction | LoadMoreAction) => {
if (action.type === 'LOAD_MORE') { /* ... */ }
},
});
table.dispatch({ type: 'LOAD_MORE' });