Add, remove and patch columns
setColumns replaces the whole column list. To change one column, the methods below are simpler. They all work by changing the columns and headerGroups options, so the width, order and pin a user left on a column with the same id stay. A bad value fails with invalid-option before any state changes. They need the columns feature.
| Method | What it does |
|---|---|
addColumn(def, opts?) | Adds one column. Options index, groupId, fill |
removeColumn(id) | Removes the column, its width/order state and sort/filter entries |
updateColumn(id, patch) | Shallow-merges a patch. id and key cannot change |
pinColumns({ start, end }) | Sets the whole pinned set at once |
showAllColumns() | Shows every hidden column |
moveColumnToGroup(id, groupId, at?) | Moves a column to another header group; null takes it out |
getParentGroup(id) | The header group the column is directly in, or null |
getAddedColumns() getRemovedColumns() | Column ids added or removed since creation |
resetColumnChanges() | Clears those lists |
Adding a column
api.addColumn(
{ id: 'bonus', title: 'Bonus', kind: 'number', defaultValue: (row) => row.salary / 10 },
{
index: 1, // position in the visible order; default: the end
groupId: 'pay', // an existing header group id; default: no group
fill: true, // write defaultValue into existing rows that have no value
},
);Values written by fill are not recorded as changes or undo steps, because adding a column is a structure change, not a data edit. defaultValue is a value or a function of the row, and it also fills missing cells in new rows added with addRows. If no column has a defaultValue, addRows behaves as before.
A duplicate id, an out-of-range index or an unknown groupId throws invalid-option. hooks.beforeColumnChange is called with type: 'add', and returning false does nothing.
Removing and patching
removeColumn also takes the column out of its header groups and drops a group that loses its last column. Adding the same id again later does not bring back an old width or hidden state. When the patch of updateColumn has width, hidden or pinned, the patch wins over the value the user left.
A changed title (1.5.0, updateColumn(id, { title }) or titleEditable header rename) is carried in the column state as title: getColumnState() returns it and setColumnState() applies it again. resetColumnState() returns to the title in the definition. See Column layout.
Event
columnsChanged carries { columns } plus added, removed, updated (id arrays) and reason ('add', 'remove', 'update', 'group'). setColumns and option changes still carry only columns.
Pin and show in one call
pinColumns({ start: ['id', 'name'], end: ['salary'] }) sets the order inside each zone as given and unpins every column not named. Naming a column on both sides or an unknown id throws invalid-option.
Gotchas
Keys that block hiding, moving and pinning or show a column by header group are in Column locks, show-when and roles.
Columns made by
autoColumnsare not in thecolumnsoption, so these methods cannot change them. List the columns yourself.The
indexofmoveColumnToGroupis the position among that group's children.