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(rowKeyValue, columnKey)
{
type: 'OpenEditor',
rowKeyValue: unknown,
columnKey: string
}
yes Add cell to editableCells
'OpenRowEditors'
openRowEditors(rowKeyValue)
{
type: 'OpenRowEditors',
rowKeyValue: unknown
}
no Add row cells to editableCells
'OpenAllEditors'
openAllEditors()
{
type: 'OpenAllEditors'
}
no Adds each data cell to editableCells
'CloseEditor'
closeEditor(rowKeyValue, columnKey)
{
type: 'CloseEditor',
rowKeyValue: unknown,
columnKey: string
}
yes Removes cell from editableCells
note: when EditingMode.Cell is enabled table calls CloseEditor action each time editor loses focus
'CloseRowEditors'
closeRowEditors(rowKeyValue)
{
type: 'CloseRowEditors',
rowKeyValue: unknown
}
no Removes row cells from editableCells
'UpdateEditorValue'
updateEditorValue(rowKeyValue, columnKey, value)
{
type: 'UpdateEditorValue',
rowKeyValue: unknown,
columnKey: string,
value: unknown
}
yes Update editorCell in editableCells
'UpdateCellValue'
updateCellValue(rowKeyValue, columnKey, value)
{
type: 'UpdateCellValue',
rowKeyValue: unknown,
columnKey: string,
value: unknown
}
yes Update data
'SaveRowEditors'
saveRowEditors(rowKeyValue, settings?)
{
type: 'SaveRowEditors',
rowKeyValue: unknown,
validate?: boolean
}
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()
{
type: 'SaveAllEditors'
}
no Saves editableCells editorValues to data
'Validate'
validate()
{
type: 'Validate'
}
no Adds/removes validationMessage to/from editableCells items
'ShowNewRow'
showNewRow()
{
type: 'ShowNewRow'
}
no Add new row cells to editableCells
'HideNewRow'
hideNewRow()
{
type: 'HideNewRow'
}
no Removes row cells from editableCells
'SaveNewRow'
saveNewRow(rowKeyValue, settings?)
{
type: 'SaveNewRow',
rowKeyValue: unknown,
validate?: boolean
}
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(data)
{
type: 'UpdateData',
data: unknown[]
}
no Update data
'UpdateRow'
updateRow(rowData)
{
type: 'UpdateRow',
rowData: unknown
}
no Replaces the row with the same key in data
'InsertRow'
insertRow(rowData, options?)
{
type: 'InsertRow',
rowData: unknown,
options?: { rowKeyValue?: unknown, insertRowPosition?: ('before' | 'after') }
}
no Inserts row to specific position in the data
'DeleteRow'
deleteRow(rowKeyValue)
{
type: 'DeleteRow',
rowKeyValue: unknown
}
no Deletes row from data and its key from selectedRows, detailsRows, treeGroupsExpanded, editableCells and focused
'ReorderRows'
reorderRows(rowKeyValue, targetRowKeyValue)
{
type: 'ReorderRows',
rowKeyValue: unknown,
targetRowKeyValue: unknown
}
yes Place rowKeyValue to the position of targetRowKeyValue
'LoadData'
loadData()
{
type: 'LoadData'
}
no Notifies grid about time to reload its data
'ShowLoading'
showLoading(text?)
{
type: 'ShowLoading',
text?: string
}
no Set loading = true and updates the text if text is specified
'HideLoading'
hideLoading()
{
type: 'HideLoading'
}
no Set loading = false

Columns

Type and action creator Action UI kaReducer
'ShowColumn'
showColumn(columnKey)
{
type: 'ShowColumn',
columnKey: string
}
no Sets visible=true for specific column
'HideColumn'
hideColumn(columnKey)
{
type: 'HideColumn',
columnKey: string
}
no Sets visible=false for specific column
'InsertColumn'
insertColumn(column, index)
{
type: 'InsertColumn',
column: Column,
index: number
}
no Inserts column to specific position in the columns
'MoveColumnBefore'
moveColumnBefore(columnKey, targetColumnKey)
{
type: 'MoveColumnBefore',
columnKey: string,
targetColumnKey: string
}
no Move columnKey to position before targetColumnKey in columns
'MoveColumnToIndex'
moveColumnToIndex(columnKey, index)
{
type: 'MoveColumnToIndex',
columnKey: string,
index: number
}
no Move columnKey to index position in columns
'ReorderColumns'
reorderColumns(columnKey, targetColumnKey)
{
type: 'ReorderColumns',
columnKey: string,
targetColumnKey: string
}
yes Place columnKey to the position of targetColumnKey
'ResizeColumn'
resizeColumn(columnKey, width)
{
type: 'ResizeColumn',
columnKey: string,
width: number
}
yes Change column width
Type and action creator Action UI kaReducer
'UpdateSortDirection'
updateSortDirection(columnKey)
{
type: 'UpdateSortDirection',
columnKey: string
}
yes Update sortDirection to the opposite for specific column
'UpdateFilterRowValue'
updateFilterRowValue(columnKey, filterRowValue)
{
type: 'UpdateFilterRowValue',
columnKey: string,
filterRowValue: unknown
}
yes Update filterRowValue for specific column
'UpdateFilterRowOperator'
updateFilterRowOperator(columnKey, filterRowOperator)
{
type: 'UpdateFilterRowOperator',
columnKey: string,
filterRowOperator: string
}
no Update filterRowOperator for specific column
'UpdateHeaderFilterValues'
updateHeaderFilterValues(columnKey, item, checked)
{
type: 'UpdateHeaderFilterValues',
columnKey: string,
item: string,
checked: boolean
}
yes Fires when user selects/deselects value in the header filter
'UpdateHeaderFilterPopupState'
updateHeaderFilterPopupState(columnKey, isHeaderFilterPopupShown?)
{
type: 'UpdateHeaderFilterPopupState',
columnKey: string,
isHeaderFilterPopupShown?: boolean
}
yes Shows/hides the header filter popup
'UpdateHeaderFilterSearchValue'
updateHeaderFilterSearchValue(columnKey, headerFilterSearchValue)
{
type: 'UpdateHeaderFilterSearchValue',
columnKey: string,
headerFilterSearchValue: string
}
yes Updates the search text of the header filter popup
'UpdatePopupPosition'
updatePopupPosition(popupPosition)
{
type: 'UpdatePopupPosition',
popupPosition: { x?: number; y?: number; }
}
yes Fires when header filter popup is shown
'ClearAllFilters'
clearAllFilters()
{
type: 'ClearAllFilters'
}
no Clears all table filters
'Search'
search(searchText)
{
type: 'Search',
searchText: string
}
no Change searchText option

Grouping, tree and details rows

Type and action creator Action UI kaReducer
'GroupColumn'
groupColumn(columnKey)
{
type: 'GroupColumn',
columnKey: string
}
yes Groups columnKey
'UngroupColumn'
ungroupColumn(columnKey)
{
type: 'UngroupColumn',
columnKey: string
}
yes Ungroups columnKey
'UpdateGroupsExpanded'
updateGroupsExpanded(groupKey)
{
type: 'UpdateGroupsExpanded',
groupKey: unknown[]
}
yes Add/remove group id to/from groupsExpanded
'UpdateTreeGroupsExpanded'
updateTreeGroupsExpanded(rowKeyValue)
{
type: 'UpdateTreeGroupsExpanded',
rowKeyValue: unknown
}
yes Adds/removes rowKeyValue to/from treeGroupsExpanded
'ShowDetailsRow'
showDetailsRow(rowKeyValue)
{
type: 'ShowDetailsRow',
rowKeyValue: unknown
}
no Adds the row key to detailsRows
'HideDetailsRow'
hideDetailsRow(rowKeyValue)
{
type: 'HideDetailsRow',
rowKeyValue: unknown
}
no Removes the row key from detailsRows

Paging and scrolling

Type and action creator Action UI kaReducer
'UpdatePageIndex'
updatePageIndex(pageIndex)
{
type: 'UpdatePageIndex',
pageIndex: number
}
yes Update paging.pageIndex option and show new page data
'UpdatePageSize'
updatePageSize(pageSize)
{
type: 'UpdatePageSize',
pageSize: number
}
yes Update paging.pageSize, the first row of the page stays shown
'UpdatePagesCount'
updatePagesCount(pagesCount)
{
type: 'UpdatePagesCount',
pagesCount: number
}
no Update paging.pagesCount (remote paging)

Selection

Type and action creator Action UI kaReducer
'SelectRow'
selectRow(rowKeyValue)
{
type: 'SelectRow',
rowKeyValue: unknown
}
no Add row id to selectedRows
'SelectRows'
selectRows(rowsKeyValues)
{
type: 'SelectRows',
rowsKeyValues: unknown[]
}
no Add rows ids to selectedRows
'SelectRowsRange'
selectRowsRange(rowKeyValueFrom, rowKeyValueTo)
{
type: 'SelectRowsRange',
rowKeyValueFrom: unknown,
rowKeyValueTo: unknown
}
no rowKeyValueFrom, rowKeyValueTo describe start and end of visible elements range, all ids added to selectedRows
'SelectSingleRow'
selectSingleRow(rowKeyValue)
{
type: 'SelectSingleRow',
rowKeyValue: unknown
}
no Set selectedRows as an array with one row id
'SelectAllRows'
selectAllRows()
{
type: 'SelectAllRows'
}
no Add all items ids of data to selectedRows
'SelectAllFilteredRows'
selectAllFilteredRows()
{
type: 'SelectAllFilteredRows'
}
no Add all filtered items ids of data to selectedRows
'SelectAllVisibleRows'
selectAllVisibleRows()
{
type: 'SelectAllVisibleRows'
}
no Add all visible items ids of data to selectedRows
'DeselectRow'
deselectRow(rowKeyValue)
{
type: 'DeselectRow',
rowKeyValue: unknown
}
no Removes row id from selectedRows
'DeselectRows'
deselectRows(rowsKeyValues)
{
type: 'DeselectRows',
rowsKeyValues: unknown[]
}
no Removes rows ids from selectedRows
'DeselectAllRows'
deselectAllRows()
{
type: 'DeselectAllRows'
}
no Clears selectedRows
'DeselectAllFilteredRows'
deselectAllFilteredRows()
{
type: 'DeselectAllFilteredRows'
}
no Remove all filtered items ids of data to selectedRows
'DeselectAllVisibleRows'
deselectAllVisibleRows()
{
type: 'DeselectAllVisibleRows'
}
no Remove all visible items ids of data to selectedRows

Focus

Type and action creator Action UI kaReducer
'SetFocused'
setFocused(focused)
{
type: 'SetFocused',
focused: Focused
}
yes Set focused property: the cell or the group row focused by a click, Tab or the keyboard navigation
'ClearFocused'
clearFocused()
{
type: 'ClearFocused'
}
no Sets focused as undefined
'MoveFocusedUp'
moveFocusedUp(settings?)
{
type: 'MoveFocusedUp',
settings?: { end?: boolean; }
}
no Move focused one cell up
'MoveFocusedDown'
moveFocusedDown(settings?)
{
type: 'MoveFocusedDown',
settings?: { end?: boolean; }
}
no Move focused one cell down
'MoveFocusedLeft'
moveFocusedLeft(settings?)
{
type: 'MoveFocusedLeft',
settings?: { end?: boolean; }
}
no Move focused one cell left
'MoveFocusedRight'
moveFocusedRight(settings?)
{
type: 'MoveFocusedRight',
settings?: { end?: boolean; }
}
no Move focused one cell right

Single action

Type and action creator Action UI kaReducer
'SetSingleAction'
setSingleAction(singleAction)
{
type: 'SetSingleAction',
singleAction: CustomAction
}
no Set singleAction property
'ClearSingleAction'
clearSingleAction()
{
type: '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' });