# Slicers

> Floating button lists that filter a pivot table or a table by the items of one field, with mouse and keyboard selection.

A slicer is a floating list of buttons, one per item of a field. Selecting items filters the [pivot table](https://docs.birchui.com/spreadsheet/pivot-tables.md) or [table](https://docs.birchui.com/spreadsheet/tables.md) the slicer is connected to.

```ts
const id = sheet.addSlicer({
  target: { kind: 'pivot', name: 'PivotTable1' },
  field: 'Channel',
  anchor: { cell: 'L2', width: 180, height: 200 },
  caption: 'Sales channel',
});

sheet.addSlicer({
  target: { kind: 'table', name: 'Orders' },
  field: 'Region',
  anchor: { cell: 'H2', width: 160, height: 220 },
  selected: ['North', 'West'],
});
```

**`SlicerJSON`** (interface from `@birchui/spreadsheet`, full reference: https://docs.birchui.com/api/spreadsheet.md)

```ts
interface SlicerJSON
```

| Property | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `target` | `{ kind: 'pivot' \| 'table'; name: string }` |  |
| `field` | `string` |  |
| `anchor` | `DrawingAnchor` |  |
| `caption?` | `string` |  |
| `selected?` | `string[]` |  |
| `columns?` | `number` |  |
| `alsoFilters?` | `string[]` |  |

| Property | Meaning |
| --- | --- |
| `target` | `{ kind: 'pivot', name }` or `{ kind: 'table', name }`. |
| `field` | A source field of the pivot, or a column of the table. |
| `anchor` | The cell the slicer is attached to, optional pixel offsets, and its size. It moves with inserted rows and columns like a [picture](https://docs.birchui.com/spreadsheet/images.md). |
| `caption` | The title. The default is the field name. |
| `selected` | The items that are shown. Leave it out for no filter. |

In the toolbar, **Insert › Slicer** lists the columns of the table or pivot table under the active cell.

## Using a slicer

| Action | Mouse | Keyboard |
| --- | --- | --- |
| Show one item | Click it | `Enter` or `Space` on it |
| Add or remove an item | `Ctrl` + click | `Ctrl` + `Enter` or `Ctrl` + `Space` |
| Show everything again | Click the only selected item, or the clear button | Same, or activate the clear button |
| Move between items | | `↑` `↓`, `Home`, `End` |
| Move the slicer | Drag its header | Arrow keys on the header, 10 px a step, 1 px with `Shift` |
| Remove the slicer | | `Delete` on the header |
| Back to the grid | | `Escape` |

The list is a multi-select listbox: each item reports `aria-selected`, and the result is announced, for example "Channel: 1 of 2 selected".

## What it filters

- **Pivot table.** Rows whose item is not selected are left out of the pivot before it aggregates. The slicer does not add a page filter row to the pivot, and it combines with the pivot's own `filters` and with other slicers. A field does not have to be used in the pivot to slice by it.
- **Table.** The slicer sets the table's column filter and hides the rows, exactly like the filter button in the header. The two stay in sync.

Selections are undoable. Slicers are locked in `readOnly` mode and on a protected sheet unless **Edit objects** is allowed.

A slicer scales with the [zoom](https://docs.birchui.com/spreadsheet/toolbar.md#status-bar) of the sheet, like shapes and pictures do: at 200% it is twice as wide and tall and its text is twice as large.

## Columns and several pivot tables

`columns` lays the items out in that many columns, from 2 to 20. Inside such a slicer the up and down arrows move by a row and the left and right arrows by one item.

`alsoFilters` names more pivot tables that the slicer filters together with its `target`. Each must have the slicer's field. The items listed are those of the target.

```ts
sheet.updateSlicer('regions', { columns: 2, alsoFilters: ['ByChannel'] });
```

## API

```ts
sheet.updateSlicer(id, { selected: ['Retail'] });
sheet.updateSlicer(id, { selected: undefined });
sheet.updateSlicer(id, { caption: 'Channel', anchor: { cell: 'N4', width: 200, height: 180 } });
sheet.getSlicers();
sheet.removeSlicer(id);
```

Slicers are saved in the sheet's `slicers` array in the [workbook JSON](https://docs.birchui.com/spreadsheet/sheets.md#workbookjson). 

## Excel files

Slicers on pivot tables and on tables are written to [xlsx files](https://docs.birchui.com/spreadsheet/xlsx.md#slicers-in-files) as slicer and slicer cache parts, with a frame in the drawing, and read back with their target, field, caption, position and selection.

- A slicer whose table does not exist, or whose pivot table is written as cells only, is left out of the file.
- `columns` is written as `columnCount` and read back. `alsoFilters` is not written as a connection, because a file ties a slicer to one pivot cache and this exporter gives every pivot table its own: the slicer is written for its target, the other pivot tables are written with the filtered-out items [hidden](https://docs.birchui.com/spreadsheet/pivot-tables.md#hidden-items), so the file shows the same numbers, and the link is gone when the file is opened again.
- A slicer in a file that hides its caption, has its own sort order or is connected to several pivot tables is kept in the file unchanged and is not shown. Timelines are kept the same way.
- The filtered state is written in three places, the way Excel writes it: the items the slicer hides are marked hidden in the pivot table definition, the slicer cache lists the selected items, and the cells of the pivot table hold the filtered values and totals. This is also true when a workbook JSON with a slicer selection is converted with `jsonToXlsx()` without the component.
- LibreOffice has no slicers. It opens the file, rebuilds every pivot table from the source data and ignores the hidden items of a field that is not on a row, column or page of the pivot, so it shows unfiltered totals (and calls the last row "Total Result"). The file itself is filtered: openpyxl reads the filtered values from the cells. The test suite checks both.
- openpyxl opens the file and drops the slicers. Nothing is tested in Microsoft Excel, and for slicers no other program confirms the parts either, so open an exported file in Excel before you rely on it.
