Documentation
Row Selection
Select one or many rows with checkboxes, click-to-select, keyboard, or the TableAPI.
- 1
Enable checkbox selection
SetenableRowSelectionfor a pinned checkbox column (multiple mode by default, with select-all in the header). ProvidegetRowIdso selection survives sort, filter, and pagination.React TSXCopy<SimpleTableenableRowSelectioncolumns={columns}rows={rows}getRowId={({ row }) => String(row.id)}/>AngularCopy<simple-table[columns]="columns"[rows]="rows"[enableRowSelection]="true"[getRowId]="getRowId"></simple-table>Vue SFCCopy<SimpleTable:columns="columns":rows="rows":enable-row-selection="true":get-row-id="(ctx) => String(ctx.row.id)"/>SvelteCopy<SimpleTable{columns}{rows}enableRowSelection={true}getRowId={({ row }) => String(row.id)}/>Solid TSXCopy<SimpleTableenableRowSelectioncolumns={columns}rows={rows()}getRowId={({ row }) => String(row.id)}/>TypeScriptCopynew SimpleTableVanilla(container, {columns,rows,enableRowSelection: true,getRowId: ({ row }) => String(row.id),}); - 2
Handle selection changes
UseonRowSelectionChangefor updates, or read selected rows from the table ref withgetSelectedRowsData().React TSXCopy<SimpleTableenableRowSelectioncolumns={columns}rows={rows}getRowId={({ row }) => String(row.id)}onRowSelectionChange={({ row, isSelected, selectedRows }) => {// ...}}/>AngularCopy<simple-table[columns]="columns"[rows]="rows"[enableRowSelection]="true"[getRowId]="getRowId"(rowSelectionChange)="handleRowSelectionChange($event)"></simple-table>Vue SFCCopy<SimpleTable:columns="columns":rows="rows":enable-row-selection="true":get-row-id="(ctx) => String(ctx.row.id)":on-row-selection-change="handleRowSelectionChange"/>SvelteCopy<SimpleTable{columns}{rows}enableRowSelection={true}getRowId={({ row }) => String(row.id)}onRowSelectionChange={handleRowSelectionChange}/>Solid TSXCopy<SimpleTableenableRowSelectioncolumns={columns}rows={rows()}getRowId={({ row }) => String(row.id)}onRowSelectionChange={({ row, isSelected, selectedRows }) => {// ...}}/>TypeScriptCopynew SimpleTableVanilla(container, {columns,rows,enableRowSelection: true,getRowId: ({ row }) => String(row.id),onRowSelectionChange: ({ row, isSelected, selectedRows }) => {// ...},});
Patterns
Single selection
Set rowSelectionMode="single" when only one row should be selected. Selecting another row replaces the previous selection; the header select-all control is hidden.
React TSX
Copy
<SimpleTableenableRowSelectioncolumns={columns}rows={rows}rowSelectionMode="single"getRowId={({ row }) => String(row.id)}/>
Angular
Copy
<simple-table[columns]="columns"[rows]="rows"[enableRowSelection]="true"rowSelectionMode="single"[getRowId]="getRowId"></simple-table>
Vue SFC
Copy
<SimpleTable:columns="columns":rows="rows":enable-row-selection="true"row-selection-mode="single":get-row-id="(ctx) => String(ctx.row.id)"/>
Svelte
Copy
<SimpleTable{columns}{rows}enableRowSelection={true}rowSelectionMode="single"getRowId={({ row }) => String(row.id)}/>
Solid TSX
Copy
<SimpleTableenableRowSelectioncolumns={columns}rows={rows()}rowSelectionMode="single"getRowId={({ row }) => String(row.id)}/>
TypeScript
Copy
new SimpleTableVanilla(container, {columns,rows,enableRowSelection: true,rowSelectionMode: "single",getRowId: ({ row }) => String(row.id),});
Click to select
Use selectRowOnClick, hide the checkbox column with showRowSelectionColumn={false}, and set selectableCells={false} so clicks (and keyboard Space / arrows) drive row selection.
React TSX
Copy
<SimpleTableenableRowSelectioncolumns={columns}rows={rows}selectRowOnClickshowRowSelectionColumn={false}selectableCells={false}getRowId={({ row }) => String(row.id)}/>
Angular
Copy
<simple-table[columns]="columns"[rows]="rows"[enableRowSelection]="true"[selectRowOnClick]="true"[showRowSelectionColumn]="false"[selectableCells]="false"[getRowId]="getRowId"></simple-table>
Vue SFC
Copy
<SimpleTable:columns="columns":rows="rows":enable-row-selection="true":select-row-on-click="true":show-row-selection-column="false":selectable-cells="false":get-row-id="(ctx) => String(ctx.row.id)"/>
Svelte
Copy
<SimpleTable{columns}{rows}enableRowSelection={true}selectRowOnClick={true}showRowSelectionColumn={false}selectableCells={false}getRowId={({ row }) => String(row.id)}/>
Solid TSX
Copy
<SimpleTableenableRowSelectioncolumns={columns}rows={rows()}selectRowOnClickshowRowSelectionColumn={false}selectableCells={false}getRowId={({ row }) => String(row.id)}/>
TypeScript
Copy
new SimpleTableVanilla(container, {columns,rows,enableRowSelection: true,selectRowOnClick: true,showRowSelectionColumn: false,selectableCells: false,getRowId: ({ row }) => String(row.id),});
Programmatic selection
Call selectRow, toggleRowSelection, getSelectedRowsData, and clearRowSelection on the table API. See also Programmatic Control.
React TSX
Copy
tableRef.current?.selectRow("1", true);tableRef.current?.toggleRowSelection("2");const selected = tableRef.current?.getSelectedRowsData();tableRef.current?.clearRowSelection();
Angular
Copy
this.tableRef.getAPI()?.selectRow("1", true);this.tableRef.getAPI()?.toggleRowSelection("2");const selected = this.tableRef.getAPI()?.getSelectedRowsData();this.tableRef.getAPI()?.clearRowSelection();
Vue SFC
Copy
tableRef.value?.selectRow("1", true);tableRef.value?.toggleRowSelection("2");const selected = tableRef.value?.getSelectedRowsData();tableRef.value?.clearRowSelection();
Svelte
Copy
tableRef.getAPI()?.selectRow("1", true);tableRef.getAPI()?.toggleRowSelection("2");const selected = tableRef.getAPI()?.getSelectedRowsData();tableRef.getAPI()?.clearRowSelection();
Solid TSX
Copy
tableRef.selectRow("1", true);tableRef.toggleRowSelection("2");const selected = tableRef.getSelectedRowsData();tableRef.clearRowSelection();
TypeScript
Copy
table.selectRow("1", true);table.toggleRowSelection("2");const selected = table.getSelectedRowsData();table.clearRowSelection();
Checkbox selection example
Library Management Demo
• Click the header checkbox to select/deselect all books
• Click individual row checkboxes to select specific books
Selected Books:None
React TSX
Copy
1import { useState, useMemo } from "react";2import { SimpleTable } from "@simple-table/react";3import type {4 Theme,5 ReactColumnDef,6 CellRendererProps,7 RowSelectionChangeProps8} from "@simple-table/react";9import { rowSelectionConfig, rowSelectionData, type LibraryBook } from "./row-selection.demo-data";10import "@simple-table/react/styles.css";1112const RowSelectionDemo = ({13 height = "348px",14 theme15}: {16 height?: string | number;17 theme?: Theme;18}) => {19 const [selectedBooks, setSelectedBooks] = useState<LibraryBook[]>([]);2021 const headers: ReactColumnDef<LibraryBook>[] = useMemo(22 () =>23 rowSelectionConfig.headers.map((h): ReactColumnDef<LibraryBook> => {24 if (h.accessor === "status") {25 return {26 ...h,27 cellRenderer: ({ row }: CellRendererProps<LibraryBook>) => {28 const s = String(row.status);29 const color =30 s === "Available" ? "#16a34a" : s === "Checked Out" ? "#ea580c" : "#dc2626";31 return <span style={{ color, fontWeight: "bold" }}>{s}</span>;32 }33 };34 }35 return h;36 }),37 [],38 );3940 const handleRowSelectionChange = (props: RowSelectionChangeProps<LibraryBook>) => {41 const selected = rowSelectionData.filter((book) => props.selectedRows.has(String(book.id)));42 setSelectedBooks(selected);43 };4445 return (46 <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>47 <div48 style={{49 padding: 12,50 backgroundColor: "#f0f9ff",51 borderRadius: 8,52 border: "1px solid #bae6fd"53 }}54 >55 <div style={{ fontWeight: "bold", marginBottom: 4, color: "#0c4a6e" }}>56 Library Management Demo57 </div>58 <div style={{ fontSize: 13, color: "#475569", marginBottom: 6 }}>59 Use the checkbox column to select books for checkout or bulk actions.60 </div>61 <div style={{ fontSize: 13, color: "#334155" }}>62 <strong>Selected Books: </strong>63 {selectedBooks.length > 0 ? selectedBooks.map((b) => b.title).join(", ") : "None"}64 </div>65 </div>6667 <SimpleTable68 columns={headers}69 rows={rowSelectionConfig.rows}70 enableRowSelection71 columnResizing72 columnReordering73 selectableCells74 onRowSelectionChange={handleRowSelectionChange}75 height={height}76 theme={theme}77 getRowId={({ row }) => row.id}78 />79 </div>80 );81};8283export default RowSelectionDemo;
Angularrow-selection-demo.component.ts
Copy
1import { Component, Input } from "@angular/core";2import { SimpleTableImports } from "@simple-table/angular";3import type { AngularColumnDef, GetRowIdParams, RowSelectionChangeProps, Theme } from "@simple-table/angular";4import type { LibraryBook } from "./row-selection.demo-data";5import { rowSelectionConfig, rowSelectionData } from "./row-selection.demo-data";6import "@simple-table/angular/styles.css";78const STATUS_COLOR: Record<string, string> = {9 Available: "#16a34a",10 "Checked Out": "#ea580c",11 Reserved: "#dc2626",12};1314@Component({15 selector: "row-selection-demo",16 standalone: true,17 imports: [SimpleTableImports],18 template: `19 <div style="display: flex; flex-direction: column; gap: 12px">20 <div style="padding: 12px; background-color: #f0f9ff; border-radius: 8px; border: 1px solid #bae6fd">21 <div style="font-weight: bold; margin-bottom: 4px; color: #0c4a6e">22 Library Management Demo23 </div>24 <div style="font-size: 13px; color: #475569; margin-bottom: 6px">25 Click rows to select books. Use the checkbox column to select multiple.26 </div>27 <div style="font-size: 13px; color: #334155">28 <strong>Selected Books: </strong>{{ selectedTitles }}29 </div>30 </div>3132 <simple-table33 [getRowId]="getRowId"34 [rows]="rows"35 [columns]="headers"36 [height]="height"37 [theme]="theme"38 [enableRowSelection]="true"39 [columnResizing]="true"40 [columnReordering]="true"41 [selectableCells]="true"42 (rowSelectionChange)="handleSelectionChange($event)"43 >44 <ng-template stCell="status" let-value="value">45 <span [style.color]="statusColor(value)" style="font-weight:bold">{{ value }}</span>46 </ng-template>47 </simple-table>48 </div>49 `,50})51export class RowSelectionDemoComponent {52 @Input() height: string | number = "348px";53 @Input() theme?: Theme;5455 readonly rows: LibraryBook[] = rowSelectionConfig.rows;56 readonly headers: AngularColumnDef<LibraryBook>[] = rowSelectionConfig.headers;5758 selectedBooks: LibraryBook[] = [];59 private readonly selectedIds = new Set<number>();6061 get selectedTitles(): string {62 return this.selectedBooks.length > 063 ? this.selectedBooks.map((b) => b.title).join(", ")64 : "None";65 }6667 statusColor(value: unknown): string {68 return STATUS_COLOR[String(value)] ?? "#dc2626";69 }7071 handleSelectionChange(event: RowSelectionChangeProps<LibraryBook>): void {72 if (event.isSelected) {73 this.selectedIds.add(event.row.id);74 } else {75 this.selectedIds.delete(event.row.id);76 }77 this.selectedBooks = rowSelectionData.filter((book) => this.selectedIds.has(book.id));78 }7980 getRowId = ({ row }: GetRowIdParams<LibraryBook>) => row.id;81}828384// row-selection.demo-data.ts85// Self-contained demo table setup for this example.86import type { AngularColumnDef } from "@simple-table/angular";878889export type LibraryBook = {90 id: number;91 isbn: string;92 title: string;93 author: string;94 genre: string;95 yearPublished: number;96 pages: number;97 rating: number;98 status: string;99 librarySection: string;100 borrowedBy?: string;101};102103export const rowSelectionData: LibraryBook[] = [104 { id: 1001, isbn: "978-0553418026", title: "The Quantum Chronicles", author: "Dr. Elena Vasquez", genre: "Science Fiction", yearPublished: 2019, pages: 324, rating: 4.7, status: "Available", librarySection: "Fiction A-L" },105 { id: 1002, isbn: "978-0316769488", title: "Digital Renaissance", author: "Marcus Chen", genre: "Technology", yearPublished: 2021, pages: 287, rating: 4.2, status: "Checked Out", librarySection: "Technology", borrowedBy: "Sarah Williams" },106 { id: 1003, isbn: "978-1400079179", title: "Echoes of Ancient Wisdom", author: "Prof. Amara Okafor", genre: "Philosophy", yearPublished: 2018, pages: 456, rating: 4.9, status: "Available", librarySection: "Philosophy" },107 { id: 1004, isbn: "978-0062315007", title: "The Midnight Observatory", author: "Luna Rodriguez", genre: "Mystery", yearPublished: 2020, pages: 298, rating: 4.4, status: "Reserved", librarySection: "Fiction M-Z" },108 { id: 1005, isbn: "978-0544003415", title: "Sustainable Architecture Now", author: "Kai Nakamura", genre: "Architecture", yearPublished: 2022, pages: 368, rating: 4.6, status: "Available", librarySection: "Architecture" },109 { id: 1006, isbn: "978-0147516466", title: "Neural Networks Simplified", author: "Dr. Priya Sharma", genre: "Computer Science", yearPublished: 2021, pages: 412, rating: 4.8, status: "Checked Out", librarySection: "Computer Science", borrowedBy: "Alex Thompson" },110 { id: 1007, isbn: "978-0547928227", title: "Culinary Traditions of the World", author: "Isabella Fontana", genre: "Cooking", yearPublished: 2019, pages: 276, rating: 4.3, status: "Available", librarySection: "Lifestyle" },111 { id: 1008, isbn: "978-0525509288", title: "The Biomimicry Revolution", author: "Dr. James Whitfield", genre: "Biology", yearPublished: 2020, pages: 345, rating: 4.5, status: "Available", librarySection: "Science" },112 { id: 1009, isbn: "978-0345391803", title: "Symphonies in Code", author: "Aria Blackwood", genre: "Programming", yearPublished: 2022, pages: 423, rating: 4.7, status: "Checked Out", librarySection: "Computer Science", borrowedBy: "Emma Davis" },113 { id: 1010, isbn: "978-0812988407", title: "Urban Gardens & Green Spaces", author: "Miguel Santos", genre: "Gardening", yearPublished: 2021, pages: 189, rating: 4.1, status: "Available", librarySection: "Lifestyle" },114 { id: 1011, isbn: "978-0374533557", title: "The Psychology of Innovation", author: "Dr. Rachel Kim", genre: "Psychology", yearPublished: 2019, pages: 312, rating: 4.6, status: "Reserved", librarySection: "Psychology" },115 { id: 1012, isbn: "978-0593229439", title: "Climate Solutions for Tomorrow", author: "Dr. Hassan Al-Rashid", genre: "Environmental Science", yearPublished: 2022, pages: 398, rating: 4.8, status: "Available", librarySection: "Science" },116];117118export const rowSelectionHeaders: AngularColumnDef<LibraryBook>[] = [119 { accessor: "id", label: "Book ID", width: 80, sortable: true, type: "number" },120 { accessor: "isbn", label: "ISBN", width: 120, sortable: true, type: "string" },121 { accessor: "title", label: "Title", minWidth: 150, width: "1fr", sortable: true, type: "string" },122 { accessor: "author", label: "Author", width: 140, sortable: true, type: "string" },123 { accessor: "genre", label: "Genre", width: 120, sortable: true, type: "string" },124 { accessor: "yearPublished", label: "Year", width: 80, sortable: true, type: "number" },125 { accessor: "pages", label: "Pages", width: 80, sortable: true, type: "number" },126 { accessor: "rating", label: "Rating", width: 80, sortable: true, type: "number" },127 { accessor: "status", label: "Status", width: 100, sortable: true, type: "string" },128 { accessor: "librarySection", label: "Section", width: 120, sortable: true, type: "string" },129];130131export const rowSelectionConfig = {132 headers: rowSelectionHeaders,133 rows: rowSelectionData,134 tableProps: {135 enableRowSelection: true,136 columnResizing: true,137 columnReordering: true,138 selectableCells: true,139 },140};141
Vue SFC
Copy
1<script setup lang="ts">2import { ref, computed } from "vue";3import { SimpleTable } from "@simple-table/vue";4import type { Theme, RowSelectionChangeProps, VueColumnDef, CellRendererProps, GetRowIdParams } from "@simple-table/vue";5import { rowSelectionConfig, rowSelectionData } from "./row-selection.demo-data";6import type { LibraryBook } from "./row-selection.demo-data";7import "@simple-table/vue/styles.css";89withDefaults(defineProps<{ height?: string | number; theme?: Theme }>(), {10 height: "348px",11});1213const selectedBooks = ref<LibraryBook[]>([]);14const getRowId = ({ row }: GetRowIdParams<LibraryBook>) => row.id;1516const selectedTitles = computed(() =>17 selectedBooks.value.length > 018 ? selectedBooks.value.map((b) => b.title).join(", ")19 : "None",20);2122const headers: VueColumnDef<LibraryBook>[] = rowSelectionConfig.headers.map((col) => {23 if (col.accessor === "status") {24 return {25 ...col,26 cellRenderer: ({ row }: CellRendererProps<LibraryBook>) => {27 const s = row.status;28 const color = s === "Available" ? "#16a34a" : s === "Checked Out" ? "#ea580c" : "#dc2626";29 return `<span style="color:${color};font-weight:bold">${s}</span>`;30 },31 };32 }33 return { ...col };34});3536function handleRowSelectionChange(props: RowSelectionChangeProps<LibraryBook>) {37 selectedBooks.value = rowSelectionData.filter((book) =>38 props.selectedRows.has(String(book.id)),39 );40}41</script>4243<template>44 <div style="display: flex; flex-direction: column; gap: 12px">45 <div46 style="padding: 12px; background-color: #f0f9ff; border-radius: 8px; border: 1px solid #bae6fd"47 >48 <div style="font-weight: bold; margin-bottom: 4px; color: #0c4a6e">49 Library Management Demo50 </div>51 <div style="font-size: 13px; color: #475569; margin-bottom: 6px">52 Click rows to select books. Use the checkbox column to select multiple.53 </div>54 <div style="font-size: 13px; color: #334155">55 <strong>Selected Books: </strong>{{ selectedTitles }}56 </div>57 </div>5859 <SimpleTable60 :columns="headers"61 :get-row-id="getRowId"62 :rows="rowSelectionConfig.rows"63 :enable-row-selection="true"64 :column-resizing="true"65 :column-reordering="true"66 :selectable-cells="true"67 :height="height"68 :theme="theme"69 @row-selection-change="handleRowSelectionChange"70 />71 </div>72</template>
Svelte
Copy
1<script lang="ts">2 import { SimpleTable } from "@simple-table/svelte";3 import type {4 Theme,5 RowSelectionChangeProps,6 SvelteColumnDef,7 CellRendererProps,8 GetRowIdParams,9 } from "@simple-table/svelte";10 import { rowSelectionConfig, rowSelectionData } from "./row-selection.demo-data";11 import type { LibraryBook } from "./row-selection.demo-data";12 import "@simple-table/svelte/styles.css";1314 let { height = "348px", theme }: { height?: string | number; theme?: Theme } = $props();1516 let selectedBooks: LibraryBook[] = $state([]);1718 let selectedTitles = $derived(19 selectedBooks.length > 020 ? selectedBooks.map((b) => b.title).join(", ")21 : "None",22 );2324 const getRowId = ({ row }: GetRowIdParams<LibraryBook>) => row.id;2526 const headers: SvelteColumnDef<LibraryBook>[] = rowSelectionConfig.headers.map((h) => {27 if (h.accessor === "status") {28 return {29 ...h,30 cellRenderer: ({ row }: CellRendererProps<LibraryBook>) => {31 const color = row.status === "Available" ? "#16a34a" : row.status === "Checked Out" ? "#ea580c" : "#dc2626";32 return `<span style="color:${color};font-weight:bold">${row.status}</span>`;33 },34 };35 }36 return { ...h };37 });3839 function handleRowSelectionChange({ selectedRows }: RowSelectionChangeProps<LibraryBook>) {40 selectedBooks = rowSelectionData.filter((book) =>41 selectedRows.has(String(book.id)),42 );43 }44</script>4546<div style="display: flex; flex-direction: column; gap: 12px;">47 <div style="padding: 12px; background-color: #f0f9ff; border-radius: 8px; border: 1px solid #bae6fd;">48 <div style="font-weight: bold; margin-bottom: 4px; color: #0c4a6e;">49 Library Management Demo50 </div>51 <div style="font-size: 13px; color: #475569; margin-bottom: 6px;">52 Click rows to select books. Use the checkbox column to select multiple.53 </div>54 <div style="font-size: 13px; color: #334155;">55 <strong>Selected Books: </strong>{selectedTitles}56 </div>57 </div>5859 <SimpleTable60 columns={headers}61 rows={rowSelectionConfig.rows}62 {getRowId}63 enableRowSelection={true}64 columnResizing={true}65 columnReordering={true}66 selectableCells={true}67 onRowSelectionChange={handleRowSelectionChange}68 {height}69 {theme}70 />71</div>
Solid TSX
Copy
1import { createSignal, createMemo } from "solid-js";2import { SimpleTable } from "@simple-table/solid";3import type {4 Theme,5 SolidColumnDef,6 CellRendererProps,7 RowSelectionChangeProps,8} from "@simple-table/solid";9import { rowSelectionConfig, rowSelectionData } from "./row-selection.demo-data";10import type { LibraryBook } from "./row-selection.demo-data";11import "@simple-table/solid/styles.css";1213export default function RowSelectionDemo(props: {14 height?: string | number;15 theme?: Theme;16}) {17 const [selectedBooks, setSelectedBooks] = createSignal<LibraryBook[]>([]);1819 const selectedTitles = createMemo(() => {20 const books = selectedBooks();21 return books.length > 0 ? books.map((b) => b.title).join(", ") : "None";22 });2324 const headers: SolidColumnDef<LibraryBook>[] = rowSelectionConfig.headers.map((h) => {25 if (h.accessor === "status") {26 return {27 ...h,28 cellRenderer: (cr: CellRendererProps<LibraryBook>) => {29 const s = String(cr.row.status);30 const color = s === "Available" ? "#16a34a" : s === "Checked Out" ? "#ea580c" : "#dc2626";31 return <span style={{ color, "font-weight": "bold" }}>{s}</span>;32 },33 };34 }35 return h;36 });3738 const handleSelectionChange = (selection: RowSelectionChangeProps<LibraryBook>) => {39 const selected = rowSelectionData.filter((book) =>40 selection.selectedRows.has(String(book.id)),41 );42 setSelectedBooks(selected);43 };4445 return (46 <div style={{ display: "flex", "flex-direction": "column", gap: "12px" }}>47 <div48 style={{49 padding: "12px",50 "background-color": "#f0f9ff",51 "border-radius": "8px",52 border: "1px solid #bae6fd",53 }}54 >55 <div style={{ "font-weight": "bold", "margin-bottom": "4px", color: "#0c4a6e" }}>56 Library Management Demo57 </div>58 <div style={{ "font-size": "13px", color: "#475569", "margin-bottom": "6px" }}>59 Click rows to select books. Use the checkbox column to select multiple.60 </div>61 <div style={{ "font-size": "13px", color: "#334155" }}>62 <strong>Selected Books: </strong>63 {selectedTitles()}64 </div>65 </div>6667 <SimpleTable68 columns={headers}69 getRowId={({ row }) => row.id}70 rows={rowSelectionConfig.rows}71 height={props.height ?? "348px"}72 theme={props.theme}73 enableRowSelection={true}74 columnResizing={true}75 columnReordering={true}76 selectableCells={true}77 onRowSelectionChange={handleSelectionChange}78 />79 </div>80 );81}
TypeScriptRowSelectionDemo.ts
Copy
1import { SimpleTableVanilla } from "simple-table-core";2import type { Theme, ColumnDef, CellRendererProps, GetRowIdParams } from "simple-table-core";3import { rowSelectionConfig, rowSelectionData } from "./row-selection.demo-data";4import type { LibraryBook } from "./row-selection.demo-data";5import "simple-table-core/styles.css";678const getRowId = ({ row }: GetRowIdParams<LibraryBook>) => row.id;9export function renderRowSelectionDemo(10 container: HTMLElement,11 options?: { height?: string | number; theme?: Theme },12): SimpleTableVanilla<LibraryBook> {13 const wrapper = document.createElement("div");14 wrapper.style.cssText = "display:flex;flex-direction:column;gap:12px";1516 const infoPanel = document.createElement("div");17 infoPanel.style.cssText =18 "padding:12px;background-color:#f0f9ff;border-radius:8px;border:1px solid #bae6fd";19 infoPanel.innerHTML = `20 <div style="font-weight:bold;margin-bottom:4px;color:#0c4a6e">Library Management Demo</div>21 <div style="font-size:13px;color:#475569;margin-bottom:6px">Click rows to select books. Use the checkbox column to select multiple.</div>22 <div style="font-size:13px;color:#334155"><strong>Selected Books: </strong><span id="selected-titles">None</span></div>23 `;2425 const tableContainer = document.createElement("div");26 wrapper.appendChild(infoPanel);27 wrapper.appendChild(tableContainer);28 container.appendChild(wrapper);2930 const titlesSpan = infoPanel.querySelector("#selected-titles")!;3132 const headers: ColumnDef<LibraryBook>[] = rowSelectionConfig.headers.map((h) => {33 if (h.accessor === "status") {34 return {35 ...h,36 cellRenderer: ({ row }: CellRendererProps<LibraryBook>) => {37 const s = String(row.status);38 const color = s === "Available" ? "#16a34a" : s === "Checked Out" ? "#ea580c" : "#dc2626";39 return `<span style="color:${color};font-weight:bold">${s}</span>`;40 },41 };42 }43 return { ...h };44 });4546 const table = new SimpleTableVanilla(tableContainer, {47 getRowId,48 columns: headers,49 rows: rowSelectionConfig.rows,50 height: options?.height ?? "348px",51 theme: options?.theme,52 enableRowSelection: true,53 columnResizing: true,54 columnReordering: true,55 selectableCells: true,56 onRowSelectionChange: (selection) => {57 const selected: LibraryBook[] = rowSelectionData.filter((book) =>58 selection.selectedRows.has(String(book.id)),59 );60 titlesSpan.textContent =61 selected.length > 0 ? selected.map((b) => b.title).join(", ") : "None";62 },63 });6465 return table;66}676869// row-selection.demo-data.ts70// Self-contained demo table setup for this example.71import type { ColumnDef } from "simple-table-core";727374export type LibraryBook = {75 id: number;76 isbn: string;77 title: string;78 author: string;79 genre: string;80 yearPublished: number;81 pages: number;82 rating: number;83 status: string;84 librarySection: string;85 borrowedBy?: string;86};8788export const rowSelectionData: LibraryBook[] = [89 { id: 1001, isbn: "978-0553418026", title: "The Quantum Chronicles", author: "Dr. Elena Vasquez", genre: "Science Fiction", yearPublished: 2019, pages: 324, rating: 4.7, status: "Available", librarySection: "Fiction A-L" },90 { id: 1002, isbn: "978-0316769488", title: "Digital Renaissance", author: "Marcus Chen", genre: "Technology", yearPublished: 2021, pages: 287, rating: 4.2, status: "Checked Out", librarySection: "Technology", borrowedBy: "Sarah Williams" },91 { id: 1003, isbn: "978-1400079179", title: "Echoes of Ancient Wisdom", author: "Prof. Amara Okafor", genre: "Philosophy", yearPublished: 2018, pages: 456, rating: 4.9, status: "Available", librarySection: "Philosophy" },92 { id: 1004, isbn: "978-0062315007", title: "The Midnight Observatory", author: "Luna Rodriguez", genre: "Mystery", yearPublished: 2020, pages: 298, rating: 4.4, status: "Reserved", librarySection: "Fiction M-Z" },93 { id: 1005, isbn: "978-0544003415", title: "Sustainable Architecture Now", author: "Kai Nakamura", genre: "Architecture", yearPublished: 2022, pages: 368, rating: 4.6, status: "Available", librarySection: "Architecture" },94 { id: 1006, isbn: "978-0147516466", title: "Neural Networks Simplified", author: "Dr. Priya Sharma", genre: "Computer Science", yearPublished: 2021, pages: 412, rating: 4.8, status: "Checked Out", librarySection: "Computer Science", borrowedBy: "Alex Thompson" },95 { id: 1007, isbn: "978-0547928227", title: "Culinary Traditions of the World", author: "Isabella Fontana", genre: "Cooking", yearPublished: 2019, pages: 276, rating: 4.3, status: "Available", librarySection: "Lifestyle" },96 { id: 1008, isbn: "978-0525509288", title: "The Biomimicry Revolution", author: "Dr. James Whitfield", genre: "Biology", yearPublished: 2020, pages: 345, rating: 4.5, status: "Available", librarySection: "Science" },97 { id: 1009, isbn: "978-0345391803", title: "Symphonies in Code", author: "Aria Blackwood", genre: "Programming", yearPublished: 2022, pages: 423, rating: 4.7, status: "Checked Out", librarySection: "Computer Science", borrowedBy: "Emma Davis" },98 { id: 1010, isbn: "978-0812988407", title: "Urban Gardens & Green Spaces", author: "Miguel Santos", genre: "Gardening", yearPublished: 2021, pages: 189, rating: 4.1, status: "Available", librarySection: "Lifestyle" },99 { id: 1011, isbn: "978-0374533557", title: "The Psychology of Innovation", author: "Dr. Rachel Kim", genre: "Psychology", yearPublished: 2019, pages: 312, rating: 4.6, status: "Reserved", librarySection: "Psychology" },100 { id: 1012, isbn: "978-0593229439", title: "Climate Solutions for Tomorrow", author: "Dr. Hassan Al-Rashid", genre: "Environmental Science", yearPublished: 2022, pages: 398, rating: 4.8, status: "Available", librarySection: "Science" },101];102103export const rowSelectionHeaders: ColumnDef<LibraryBook>[] = [104 { accessor: "id", label: "Book ID", width: 80, sortable: true, type: "number" },105 { accessor: "isbn", label: "ISBN", width: 120, sortable: true, type: "string" },106 { accessor: "title", label: "Title", minWidth: 150, width: "1fr", sortable: true, type: "string" },107 { accessor: "author", label: "Author", width: 140, sortable: true, type: "string" },108 { accessor: "genre", label: "Genre", width: 120, sortable: true, type: "string" },109 { accessor: "yearPublished", label: "Year", width: 80, sortable: true, type: "number" },110 { accessor: "pages", label: "Pages", width: 80, sortable: true, type: "number" },111 { accessor: "rating", label: "Rating", width: 80, sortable: true, type: "number" },112 { accessor: "status", label: "Status", width: 100, sortable: true, type: "string" },113 { accessor: "librarySection", label: "Section", width: 120, sortable: true, type: "string" },114];115116export const rowSelectionConfig = {117 headers: rowSelectionHeaders,118 rows: rowSelectionData,119 tableProps: {120 enableRowSelection: true,121 columnResizing: true,122 columnReordering: true,123 selectableCells: true,124 },125};126
Single selection example
Selected: None
React TSX
Copy
1
Click to select example
Selected: None
React TSX
Copy
1
Programmatic selection example
Ready · Selected: None
React TSX
Copy
1
Props
Row Selection Configuration
| Property | Required | Description | Example |
|---|---|---|---|
Property | Required | Description | Example |
enableRowSelectionboolean | Optional | Enables row selection via checkboxes, click, keyboard, or TableAPI. | |
rowSelectionModeenum | Optional | Multiple (default) allows many selected rows; single replaces the previous selection and hides select-all. Options: single multiple | |
selectRowOnClickboolean | Optional | Selecting a data cell selects the row. Prefer selectableCells={false} for a pure click-to-select UX. | |
showRowSelectionColumnboolean | Optional | When false, hides the checkbox column. Selection still works via click, keyboard, or API. Default true. | |
getRowId(props: { row: Row }) => string | null | undefined | Optional | Stable row id so selection survives sort, filter, and pagination. | |
onRowSelectionChange(props: RowSelectionChangeProps) => void | Optional | Fires when selection changes. |
RowSelectionChangeProps
| Property | Required | Description | Example |
|---|---|---|---|
Property | Required | Description | Example |
row | Required | The complete row object that was selected or deselected | |
isSelectedboolean | Required | Boolean indicating whether the row was selected (true) or deselected (false) | |
selectedRowsSet<string> | Required | Set containing the IDs of all currently selected rows |