Skip to content
Niko Table

Column Pinning Table

Pin columns to the left or right edge so they stay visible while scrolling.

This example pins columns to the left or right edge so they stay in view during horizontal scroll. Use it for identifier columns (Order ID, Name) on the left and action columns (menu, edit) on the right.

The core handles sticky positioning and z-index. You only say which columns pin and where.

column-pinning.tsx
Order ID
Customer
Product
Amount
Status
Date
Region
Actions
ORD-001John DoePremium Widget$299.99delivered1/15/2024North America
ORD-002Jane SmithBasic Kit$149.50shipped1/18/2024Europe
ORD-003Bob JohnsonPro Bundle$599.00pending1/20/2024Asia Pacific
ORD-004Alice WilliamsStarter Pack$79.99delivered1/22/2024North America
ORD-005Charlie BrownEnterprise Suite$1,299.00shipped1/25/2024Europe
ORD-006Diana PrincePremium Widget$299.99cancelled1/28/2024Asia Pacific
ORD-007Ethan HuntBasic Kit$149.50pending2/1/2024North America
ORD-008Fiona GreenPro Bundle$599.00delivered2/5/2024Europe
ORD-009George MillerStarter Pack$79.99shipped2/8/2024Asia Pacific
ORD-010Hannah LeeEnterprise Suite$1,299.00delivered2/12/2024North America
Preview with Controlled State
column-pinning-state.tsx
Order ID
Customer
Product
Amount
Status
Date
Region
Actions
ORD-001John DoePremium Widget$299.99delivered1/15/2024North America
ORD-002Jane SmithBasic Kit$149.50shipped1/18/2024Europe
ORD-003Bob JohnsonPro Bundle$599.00pending1/20/2024Asia Pacific
ORD-004Alice WilliamsStarter Pack$79.99delivered1/22/2024North America
ORD-005Charlie BrownEnterprise Suite$1,299.00shipped1/25/2024Europe
ORD-006Diana PrincePremium Widget$299.99cancelled1/28/2024Asia Pacific
ORD-007Ethan HuntBasic Kit$149.50pending2/1/2024North America
ORD-008Fiona GreenPro Bundle$599.00delivered2/5/2024Europe
ORD-009George MillerStarter Pack$79.99shipped2/8/2024Asia Pacific
ORD-010Hannah LeeEnterprise Suite$1,299.00delivered2/12/2024North America
Current Table State
Live view of the current table state for demonstration purposes
Search Query:None
Total Items:10
Sorting:None
Page:1 (Size: 10)
Hidden Columns:0
Pinned Columns:1 Left, 1 Right
View Full State Object
Sorting:
[]
Column Visibility:
{}
Column Pinning:
{
  "start": [
    "id"
  ],
  "end": [
    "actions"
  ]
}

Pinned columns stay visible while users scroll sideways. Pin identifier columns (like Order ID) to the left and action columns to the right.

Install the DataTable core and add-ons for this example:

pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-column-pin @niko-table/data-table-column-sort @niko-table/data-table-column-hide @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menu

First time using @niko-table? See the Installation Guide to set up the registry.

For other add-ons or manual copy-paste, see the Installation Guide.

You build an orders table. This is the data:

type Order = {
id: string
customer: string
product: string
amount: number
status: "pending" | "shipped" | "delivered" | "cancelled"
date: string
region: string
}
const data: Order[] = [
{
id: "ORD-001",
customer: "John Doe",
product: "Premium Widget",
amount: 299.99,
status: "delivered",
date: "2024-01-15",
region: "North America",
},
// ...
]

Set initialState.columnPinning to pin columns when the table first renders:

column-pinning.tsx
<DataTableRoot
data={data}
columns={columns}
initialState={{
columnPinning: {
start: ["id"], // Pin Order ID to the start (left in LTR)
end: ["actions"], // Pin actions to the end (right in LTR)
},
}}
>
<DataTable>
<DataTableHeader />
<DataTableBody />
</DataTable>
</DataTableRoot>

Set a size on each column so the table can scroll horizontally:

columns.tsx
const columns: DataTableColumnDef<Order>[] = [
{
accessorKey: "id",
size: 110,
header: () => (
<DataTableColumnHeader>
<DataTableColumnTitle title="Order ID" />
<DataTableColumnSortMenu />
</DataTableColumnHeader>
),
meta: { label: "Order ID" },
},
{
accessorKey: "customer",
size: 160,
header: () => (
<DataTableColumnHeader>
<DataTableColumnTitle title="Customer" />
<DataTableColumnSortMenu />
</DataTableColumnHeader>
),
meta: { label: "Customer" },
},
// ... more columns
]

Add pin options to the column actions so users can pin columns:

{
accessorKey: "customer",
size: 160,
header: () => (
<DataTableColumnHeader>
<DataTableColumnTitle title="Customer" />
<DataTableColumnActions>
<DataTableColumnSortOptions />
<DataTableColumnPinOptions />
</DataTableColumnActions>
</DataTableColumnHeader>
),
}

Hold the pinning state yourself and pass it in:

column-pinning-state.tsx
import { useState } from "react"
import { type ColumnPinningState } from "@tanstack/react-table"
export function ControlledPinningTable({ data }: { data: Order[] }) {
const [columnPinning, setColumnPinning] = useState<ColumnPinningState>({
start: ["id"],
end: ["actions"],
})
return (
<DataTableRoot
data={data}
columns={columns}
state={{ columnPinning }}
onColumnPinningChange={setColumnPinning}
>
{/* ... */}
</DataTableRoot>
)
}

✅ Use Column Pinning when:

  • The table has many columns and scrolls sideways
  • Key identifiers (ID, Name) must stay visible
  • The actions column must always be reachable

❌ Consider other options when:

  • All columns fit on screen
  • You design mobile-first (pinning adds complexity)