왼쪽·오른쪽 컬럼 붙여 두기Pin columns to the left and right
컬럼이 많아 가로로 스크롤해도 거래처 이름은 왼쪽에, 합계는 오른쪽에 그대로 남아요. 몇 개를 붙일지 직접 바꿔 볼 수 있어요.Scroll sideways through many columns while the customer stays on the left and the total on the right. Change how many are pinned.
코드Code
탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.
이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)
버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<div id="app">
<div class="toolbar">
<select aria-label="왼쪽 고정" id="left">
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
<option value="3">3개</option>
</select>
<select aria-label="오른쪽 고정" id="right">
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));const customers = [
'한빛물류',
'푸른식품',
'북촌상회',
'단풍리테일',
'정상테크',
'바다수산',
'새봄약품',
'은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const won = (value) => `${Number(value).toLocaleString('ko-KR')}`;
export const options = {
columns: [
{ id: 'customer', title: '거래처', width: 100, pinned: 'start' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'grade', title: '등급', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 130,
align: 'end',
formatValue: won,
})),
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: won,
},
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api) {
const ids = api.getColumns().map((c) => c.id);
// 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api, value) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api, value) => {
right = Number(value ?? 0);
applyPins(api);
},
};<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>왼쪽·오른쪽 컬럼 붙여 두기</title>
</head>
<body>
<div class="toolbar">
<select aria-label="왼쪽 고정" id="left">
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
<option value="3">3개</option>
</select>
<select aria-label="오른쪽 고정" id="right">
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const customers = [
'한빛물류',
'푸른식품',
'북촌상회',
'단풍리테일',
'정상테크',
'바다수산',
'새봄약품',
'은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const won = (value) => `${Number(value).toLocaleString('ko-KR')}`;
const options = {
columns: [
{ id: 'customer', title: '거래처', width: 100, pinned: 'start' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'grade', title: '등급', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number',
width: 130,
align: 'end',
formatValue: won,
})),
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: won,
},
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api) {
const ids = api.getColumns().map((c) => c.id);
// 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
const actions = {
left: (api, value) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api, value) => {
right = Number(value ?? 0);
applyPins(api);
},
};
options.ariaLabel = "왼쪽·오른쪽 컬럼 붙여 두기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));
</script>
</body>
</html>버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="왼쪽 고정"
disabled={!api}
onChange={(event) => api && actions.left(api, event.target.value)}
>
<option value="0">없음</option>
<option value="1">1개</option>
<option value="2">2개</option>
<option value="3">3개</option>
</select>
<select defaultValue="1"
aria-label="오른쪽 고정"
disabled={!api}
onChange={(event) => api && actions.right(api, event.target.value)}
>
<option value="0">없음</option>
<option value="1">1개</option>
<option value="2">2개</option>
</select>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'한빛물류',
'푸른식품',
'북촌상회',
'단풍리테일',
'정상테크',
'바다수산',
'새봄약품',
'은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const won = (value: unknown) => `${Number(value).toLocaleString('ko-KR')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: '거래처', width: 100, pinned: 'start' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'grade', title: '등급', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: won,
})),
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: won,
},
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="왼쪽 고정"
:disabled="!api"
@change="api && actions.left(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
<option value="3">3개</option>
</select>
<select
aria-label="오른쪽 고정"
:disabled="!api"
@change="api && actions.right(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
</select>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'한빛물류',
'푸른식품',
'북촌상회',
'단풍리테일',
'정상테크',
'바다수산',
'새봄약품',
'은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const won = (value: unknown) => `${Number(value).toLocaleString('ko-KR')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: '거래처', width: 100, pinned: 'start' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'grade', title: '등급', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: won,
})),
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: won,
},
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="왼쪽 고정"
[disabled]="!api"
(change)="api && actions.left(api, $any($event.target).value)"
>
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
<option value="3">3개</option>
</select>
<select
aria-label="오른쪽 고정"
[disabled]="!api"
(change)="api && actions.right(api, $any($event.target).value)"
>
<option value="0">없음</option>
<option value="1" selected>1개</option>
<option value="2">2개</option>
</select>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'한빛물류',
'푸른식품',
'북촌상회',
'단풍리테일',
'정상테크',
'바다수산',
'새봄약품',
'은하전자',
];
const managers = ['김서연', '이준호', '박민지', '최도윤'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
// 가운데 컬럼이 많아서 가로로 스크롤돼요. 맨 왼쪽(고객)과 맨 오른쪽(합계)은 스크롤해도 그대로 있어요.
// 컬럼에 pinned: 'start'(왼쪽) 또는 'end'(오른쪽)를 주면 돼요.
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const won = (value: unknown) => `${Number(value).toLocaleString('ko-KR')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: '거래처', width: 100, pinned: 'start' },
{ id: 'manager', title: '담당자', width: 110 },
{ id: 'grade', title: '등급', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: `${k + 1}월`,
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: won,
})),
{
id: 'total',
title: '합계',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: won,
},
],
rows,
rowKey: 'id',
// 화면이 480px보다 좁으면 고정이 풀려서 모두 같이 스크롤되고, 넓어지면 다시 붙어요.
pinMinWidth: 480,
};
// 왼쪽·오른쪽에서 몇 개를 붙일지 기억해 두고, 바뀔 때마다 모든 컬럼에 다시 적용해요.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// 먼저 전부 풀고(뒤에서부터 풀어야 컬럼 순서가 그대로예요), 왼쪽은 앞에서부터, 오른쪽은 끝에서부터 다시 붙여요.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<select aria-label="Pinned left" id="left">
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
<option value="3">3 columns</option>
</select>
<select aria-label="Pinned right" id="right">
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
</div>
<script type="module" src="./main.js"></script>import { createGrid } from 'maxgrid';
import { actions, options } from './example';
const api = createGrid(document.getElementById('grid'), options);
document.getElementById('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));const customers = [
'Northwind Logistics',
'Greenfield Foods',
'Maple Trading',
'Autumn Retail',
'Summit Tech',
'Ocean Seafood',
'Springtime Pharma',
'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const money = (value) => `${Number(value).toLocaleString('en-US')}`;
export const options = {
columns: [
{ id: 'customer', title: 'Account', width: 160, pinned: 'start' },
{ id: 'manager', title: 'Manager', width: 120 },
{ id: 'grade', title: 'Grade', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: monthNames[k],
kind: 'number',
width: 130,
align: 'end',
formatValue: money,
})),
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: money,
},
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api) {
const ids = api.getColumns().map((c) => c.id);
// Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api, value) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api, value) => {
right = Number(value ?? 0);
applyPins(api);
},
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pin columns to the left and right</title>
</head>
<body>
<div class="toolbar">
<select aria-label="Pinned left" id="left">
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
<option value="3">3 columns</option>
</select>
<select aria-label="Pinned right" id="right">
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
</select>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const customers = [
'Northwind Logistics',
'Greenfield Foods',
'Maple Trading',
'Autumn Retail',
'Summit Tech',
'Ocean Seafood',
'Springtime Pharma',
'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'];
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const money = (value) => `${Number(value).toLocaleString('en-US')}`;
const options = {
columns: [
{ id: 'customer', title: 'Account', width: 160, pinned: 'start' },
{ id: 'manager', title: 'Manager', width: 120 },
{ id: 'grade', title: 'Grade', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: monthNames[k],
kind: 'number',
width: 130,
align: 'end',
formatValue: money,
})),
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: money,
},
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api) {
const ids = api.getColumns().map((c) => c.id);
// Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
const actions = {
left: (api, value) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api, value) => {
right = Number(value ?? 0);
applyPins(api);
},
};
options.ariaLabel = "Pin columns to the left and right";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('left').addEventListener('change', (event) => actions.left(api, event.target.value));
document.getElementById('right').addEventListener('change', (event) => actions.right(api, event.target.value));
</script>
</body>
</html>When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Grid, useGridApi } from '@maxgrid/react';
import { actions, options, type Row } from './example';
export function ExampleGrid() {
const [api, gridRef] = useGridApi<Row>();
return (
<>
<div className="toolbar">
<select
aria-label="Pinned left"
disabled={!api}
onChange={(event) => api && actions.left(api, event.target.value)}
>
<option value="0">None</option>
<option value="1">1 column</option>
<option value="2">2 columns</option>
<option value="3">3 columns</option>
</select>
<select defaultValue="1"
aria-label="Pinned right"
disabled={!api}
onChange={(event) => api && actions.right(api, event.target.value)}
>
<option value="0">None</option>
<option value="1">1 column</option>
<option value="2">2 columns</option>
</select>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'Northwind Logistics',
'Greenfield Foods',
'Maple Trading',
'Autumn Retail',
'Summit Tech',
'Ocean Seafood',
'Springtime Pharma',
'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const money = (value: unknown) => `${Number(value).toLocaleString('en-US')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: 'Account', width: 160, pinned: 'start' },
{ id: 'manager', title: 'Manager', width: 120 },
{ id: 'grade', title: 'Grade', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: monthNames[k],
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: money,
})),
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: money,
},
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<script setup lang="ts">
import { shallowRef } from 'vue';
import { Grid, type GridApi } from '@maxgrid/vue';
import { actions, options, type Row } from './example';
const api = shallowRef<GridApi<Row> | null>(null);
function onReady(event: { api: GridApi<Row> }) {
api.value = event.api;
}
</script>
<template>
<div class="toolbar">
<select
aria-label="Pinned left"
:disabled="!api"
@change="api && actions.left(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
<option value="3">3 columns</option>
</select>
<select
aria-label="Pinned right"
:disabled="!api"
@change="api && actions.right(api, ($event.target as HTMLSelectElement).value)"
>
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
</select>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'Northwind Logistics',
'Greenfield Foods',
'Maple Trading',
'Autumn Retail',
'Summit Tech',
'Ocean Seafood',
'Springtime Pharma',
'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const money = (value: unknown) => `${Number(value).toLocaleString('en-US')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: 'Account', width: 160, pinned: 'start' },
{ id: 'manager', title: 'Manager', width: 120 },
{ id: 'grade', title: 'Grade', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: monthNames[k],
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: money,
})),
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: money,
},
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
import { Component } from '@angular/core';
import { GridComponent, isGridEvent, type GridApi, type GridEventMessage } from '@maxgrid/angular';
import { actions, options, type Row } from './example';
@Component({
selector: 'app-example-grid',
standalone: true,
imports: [GridComponent],
template: `
<div class="toolbar">
<select
aria-label="Pinned left"
[disabled]="!api"
(change)="api && actions.left(api, $any($event.target).value)"
>
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
<option value="3">3 columns</option>
</select>
<select
aria-label="Pinned right"
[disabled]="!api"
(change)="api && actions.right(api, $any($event.target).value)"
>
<option value="0">None</option>
<option value="1" selected>1 column</option>
<option value="2">2 columns</option>
</select>
</div>
<mx-grid style="height: 300px" [options]="options" [events]="relayed" (gridEvent)="onEvent($event)" />
`,
})
export class ExampleGridComponent {
readonly options = options;
readonly actions = actions;
readonly relayed = ['ready'] as const;
api: GridApi<Row> | null = null;
onEvent(event: GridEventMessage<Row>): void {
if (isGridEvent(event, 'ready')) {
this.api = event.payload.api;
}
}
}import type { ColumnDef, GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
customer: string;
manager: string;
m1: number;
m2: number;
m3: number;
m4: number;
m5: number;
m6: number;
m7: number;
m8: number;
total: number;
grade: string;
}
const customers = [
'Northwind Logistics',
'Greenfield Foods',
'Maple Trading',
'Autumn Retail',
'Summit Tech',
'Ocean Seafood',
'Springtime Pharma',
'Galaxy Electronics',
];
const managers = ['Liam Carter', 'Emma Stone', 'Noah Brooks', 'Olivia Reed'];
const months = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'] as const;
const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];
// With many middle columns, the grid scrolls horizontally. The leftmost (customer) and rightmost (total) columns stay in place while scrolling.
// Give a column pinned: 'start' (left) or 'end' (right).
const rows: Row[] = customers.map((customer, i) => {
const sales = months.map((_, k) => (3 + ((i * 5 + k * 7) % 9)) * 1_000_000 + k * 250_000);
const [m1, m2, m3, m4, m5, m6, m7, m8] = sales;
return {
id: i + 1,
customer,
manager: managers[i % managers.length],
m1,
m2,
m3,
m4,
m5,
m6,
m7,
m8,
total: sales.reduce((sum, v) => sum + v, 0),
grade: ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'B'][i],
};
});
const money = (value: unknown) => `${Number(value).toLocaleString('en-US')}`;
export const options: GridOptions<Row> = {
columns: [
{ id: 'customer', title: 'Account', width: 160, pinned: 'start' },
{ id: 'manager', title: 'Manager', width: 120 },
{ id: 'grade', title: 'Grade', width: 80, align: 'center' },
...months.map((id, k) => ({
id,
title: monthNames[k],
kind: 'number' as const,
width: 130,
align: 'end' as const,
formatValue: money,
})),
{
id: 'total',
title: 'Total',
kind: 'number',
width: 110,
align: 'end',
pinned: 'end',
formatValue: money,
},
],
rows,
rowKey: 'id',
// On a screen narrower than 480px the pins are released and everything scrolls together; they reattach when it gets wider.
pinMinWidth: 480,
};
// Remember how many columns to pin on the left and right, and reapply to all columns whenever they change.
let left = 1;
let right = 1;
function applyPins(api: GridApi<Row>) {
const ids = api.getColumns().map((c: ColumnDef<Row>) => c.id);
// Release all first (release from the back so the column order stays the same), then pin the left from the front and the right from the end.
[...ids].reverse().forEach((id) => api.pinColumn(id, null));
ids.slice(0, left).forEach((id) => api.pinColumn(id, 'start'));
ids
.slice(ids.length - right)
.reverse()
.forEach((id) => api.pinColumn(id, 'end'));
}
export const actions = {
left: (api: GridApi<Row>, value?: string) => {
left = Number(value ?? 0);
applyPins(api);
},
right: (api: GridApi<Row>, value?: string) => {
right = Number(value ?? 0);
applyPins(api);
},
};