컬럼을 왼쪽·오른쪽에 고정하기Pin columns to the left or the right
번호와 고객사는 왼쪽에, 합계는 오른쪽에 고정하고 가운데 월별 컬럼만 가로로 스크롤해요. 코드로 컬럼을 더 고정하거나 풀 수도 있어요.Pin No and Customer to the left and H1 total to the right while the monthly columns in the middle scroll. Pin or unpin more columns from code.
코드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">
<button type="button" id="pinJan">1월 왼쪽에 고정</button>
<button type="button" id="pinJun">6월 오른쪽에 고정</button>
<button type="button" id="unpinAll">고정 모두 풀기</button>
</div>
<div id="grid" style="height: 320px"></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('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
export const options = {
columns: [
{ id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: '고객사', width: 160, pinned: 'start' },
...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
{
id: 'total',
title: '상반기 합계',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: customers.map((customer, i) => {
const sales = (k) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api) => api.pinColumn('jan', 'start'),
pinJun: (api) => api.pinColumn('jun', 'end'),
// 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
// 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
unpinAll: (api) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end])
api.pinColumn(c.id, null);
},
};<!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">
<button type="button" id="pinJan">1월 왼쪽에 고정</button>
<button type="button" id="pinJun">6월 오른쪽에 고정</button>
<button type="button" id="unpinAll">고정 모두 풀기</button>
</div>
<div id="grid" style="height: 320px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
const options = {
columns: [
{ id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: '고객사', width: 160, pinned: 'start' },
...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
})),
{
id: 'total',
title: '상반기 합계',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: customers.map((customer, i) => {
const sales = (k) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
const actions = {
pinJan: (api) => api.pinColumn('jan', 'start'),
pinJun: (api) => api.pinColumn('jun', 'end'),
// 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
// 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
unpinAll: (api) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end])
api.pinColumn(c.id, null);
},
};
options.ariaLabel = "컬럼을 왼쪽·오른쪽에 고정하기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));
</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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.pinJan(api)}
>1월 왼쪽에 고정</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pinJun(api)}
>6월 오른쪽에 고정</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.unpinAll(api)}
>고정 모두 풀기</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: '고객사', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
{
id: 'total',
title: '상반기 합계',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
// 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};버튼을 누르면 이 예제의 코드가 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">
<button type="button" :disabled="!api" @click="api && actions.pinJan(api)">1월 왼쪽에 고정</button>
<button type="button" :disabled="!api" @click="api && actions.pinJun(api)">6월 오른쪽에 고정</button>
<button
type="button"
:disabled="!api"
@click="api && actions.unpinAll(api)"
>고정 모두 풀기</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: '고객사', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
{
id: 'total',
title: '상반기 합계',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
// 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};버튼을 누르면 이 예제의 코드가 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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.pinJan(api)"
>1월 왼쪽에 고정</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pinJun(api)"
>6월 오른쪽에 고정</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.unpinAll(api)"
>고정 모두 풀기</button>
</div>
<mx-grid style="height: 320px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = ['한빛물류', '푸른하늘식품', '북극성무역', '다온상사', '새벽농원'];
// pinned: 'start'는 왼쪽, 'end'는 오른쪽 가장자리에 붙고, 가운데 컬럼만 가로로 스크롤돼요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: '고객사', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: `${i + 1}월`,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('ko-KR'),
})),
{
id: 'total',
title: '상반기 합계',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('ko-KR'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// 고정을 풀면 컬럼은 가운데 영역의 가장자리(왼쪽 고정은 왼쪽 끝, 오른쪽 고정은 오른쪽 끝)로 가요.
// 그래서 왼쪽 고정은 뒤에서부터, 오른쪽 고정은 앞에서부터 풀어야 처음 순서가 그대로 돌아와요.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.
<div id="app">
<div class="toolbar">
<button type="button" id="pinJan">Pin Jan left</button>
<button type="button" id="pinJun">Pin Jun right</button>
<button type="button" id="unpinAll">Unpin all</button>
</div>
<div id="grid" style="height: 320px"></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('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));const customers = [
'Northwind Freight',
'Bluesky Foods',
'Polaris Trading',
'Harbor & Co',
'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
export const options = {
columns: [
{ id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
id,
title: MONTHS[i],
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
{
id: 'total',
title: 'H1 total',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: customers.map((customer, i) => {
const sales = (k) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api) => api.pinColumn('jan', 'start'),
pinJun: (api) => api.pinColumn('jun', 'end'),
// Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
// So unpin left pins from the back and right pins from the front to get the original order back.
unpinAll: (api) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end])
api.pinColumn(c.id, null);
},
};<!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 or the right</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="pinJan">Pin Jan left</button>
<button type="button" id="pinJun">Pin Jun right</button>
<button type="button" id="unpinAll">Unpin all</button>
</div>
<div id="grid" style="height: 320px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const customers = [
'Northwind Freight',
'Bluesky Foods',
'Polaris Trading',
'Harbor & Co',
'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
const options = {
columns: [
{ id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
...['jan', 'feb', 'mar', 'apr', 'may', 'jun'].map((id, i) => ({
id,
title: MONTHS[i],
kind: 'number',
width: 150,
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
})),
{
id: 'total',
title: 'H1 total',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: customers.map((customer, i) => {
const sales = (k) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
const actions = {
pinJan: (api) => api.pinColumn('jan', 'start'),
pinJun: (api) => api.pinColumn('jun', 'end'),
// Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
// So unpin left pins from the back and right pins from the front to get the original order back.
unpinAll: (api) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end])
api.pinColumn(c.id, null);
},
};
options.ariaLabel = "Pin columns to the left or the right";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('pinJan').addEventListener('click', () => actions.pinJan(api));
document.getElementById('pinJun').addEventListener('click', () => actions.pinJun(api));
document.getElementById('unpinAll').addEventListener('click', () => actions.unpinAll(api));
</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">
<button
type="button"
disabled={!api}
onClick={() => api && actions.pinJan(api)}
>Pin Jan left</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.pinJun(api)}
>Pin Jun right</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.unpinAll(api)}
>Unpin all</button>
</div>
<Grid style={{ height: 320 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = [
'Northwind Freight',
'Bluesky Foods',
'Polaris Trading',
'Harbor & Co',
'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: MONTHS[i] as string,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
{
id: 'total',
title: 'H1 total',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
// So unpin left pins from the back and right pins from the front to get the original order back.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};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">
<button type="button" :disabled="!api" @click="api && actions.pinJan(api)">Pin Jan left</button>
<button type="button" :disabled="!api" @click="api && actions.pinJun(api)">Pin Jun right</button>
<button
type="button"
:disabled="!api"
@click="api && actions.unpinAll(api)"
>Unpin all</button>
</div>
<Grid style="height: 320px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = [
'Northwind Freight',
'Bluesky Foods',
'Polaris Trading',
'Harbor & Co',
'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: MONTHS[i] as string,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
{
id: 'total',
title: 'H1 total',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
// So unpin left pins from the back and right pins from the front to get the original order back.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};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">
<button
type="button"
[disabled]="!api"
(click)="api && actions.pinJan(api)"
>Pin Jan left</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.pinJun(api)"
>Pin Jun right</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.unpinAll(api)"
>Unpin all</button>
</div>
<mx-grid style="height: 320px" [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 { GridApi, GridOptions } from '@maxgrid/angular';
export interface Row {
id: number;
customer: string;
jan: number;
feb: number;
mar: number;
apr: number;
may: number;
jun: number;
total: number;
}
const customers = [
'Northwind Freight',
'Bluesky Foods',
'Polaris Trading',
'Harbor & Co',
'Sunrise Farms',
];
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
// pinned: 'start' sticks to the left edge and 'end' to the right edge, and only the middle columns scroll sideways.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No', width: 60, pinned: 'start', align: 'end' },
{ id: 'customer', title: 'Customer', width: 160, pinned: 'start' },
...(['jan', 'feb', 'mar', 'apr', 'may', 'jun'] as const).map((id, i) => ({
id,
title: MONTHS[i] as string,
kind: 'number' as const,
width: 150,
align: 'end' as const,
formatValue: (value: unknown) => Number(value).toLocaleString('en-US'),
})),
{
id: 'total',
title: 'H1 total',
kind: 'number',
width: 150,
pinned: 'end',
align: 'end',
formatValue: (value) => Number(value).toLocaleString('en-US'),
},
],
rows: customers.map((customer, i) => {
const sales = (k: number) => (i + 2) * k * 1000;
return {
id: i + 1,
customer,
jan: sales(1),
feb: sales(2),
mar: sales(3),
apr: sales(4),
may: sales(5),
jun: sales(6),
total: sales(21),
};
}),
rowKey: 'id',
};
export const actions = {
pinJan: (api: GridApi<Row>) => api.pinColumn('jan', 'start'),
pinJun: (api: GridApi<Row>) => api.pinColumn('jun', 'end'),
// Unpinning sends a column to the edge of the middle area (a left pin to its left end, a right pin to its right end).
// So unpin left pins from the back and right pins from the front to get the original order back.
unpinAll: (api: GridApi<Row>) => {
const state = api.getColumnState();
const start = state.filter((c) => c.pinned === 'start').reverse();
const end = state.filter((c) => c.pinned === 'end');
for (const c of [...start, ...end]) api.pinColumn(c.id, null);
},
};