코드로 컬럼을 보이게 가로 스크롤하기Scroll a column into view from code
scrollToColumn(id, { align })는 가로 스크롤만으로 컬럼을 보이게 해요. 기본은 가장 가까운 자리이고 start·center·end로 맞출 수도 있어요. 고정 컬럼은 건드리지 않아요.scrollToColumn(id, { align }) scrolls horizontally only. The default is the nearest position; start, center and end are available. Pinned columns are left alone.
코드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="toNearest">가까운 자리</button>
<button type="button" id="toStart">왼쪽 끝에</button>
<button type="button" id="toCenter">가운데</button>
<button type="button" id="toFirst">컬럼 1</button>
</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('toNearest').addEventListener('click', () => actions.toNearest(api));
document.getElementById('toStart').addEventListener('click', () => actions.toStart(api));
document.getElementById('toCenter').addEventListener('click', () => actions.toCenter(api));
document.getElementById('toFirst').addEventListener('click', () => actions.toFirst(api));const rows = Array.from({ length: 8 }, (_, r) => {
const row = { id: r + 1 };
for (let c = 1; c <= 20; c++)
row[`c${c}`] = r * c;
return row;
});
// scrollToColumn은 세로 위치는 그대로 두고 가로로만 컬럼을 보이게 해요. 첫 컬럼은 고정이라 늘 보여요.
export const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `컬럼 ${i + 1}`,
kind: 'number',
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api) => api.scrollToColumn('c12'),
toStart: (api) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api) => api.scrollToColumn('c1'),
};<!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="toNearest">가까운 자리</button>
<button type="button" id="toStart">왼쪽 끝에</button>
<button type="button" id="toCenter">가운데</button>
<button type="button" id="toFirst">컬럼 1</button>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = Array.from({ length: 8 }, (_, r) => {
const row = { id: r + 1 };
for (let c = 1; c <= 20; c++)
row[`c${c}`] = r * c;
return row;
});
// scrollToColumn은 세로 위치는 그대로 두고 가로로만 컬럼을 보이게 해요. 첫 컬럼은 고정이라 늘 보여요.
const options = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `컬럼 ${i + 1}`,
kind: 'number',
width: 110,
})),
],
rows,
rowKey: 'id',
};
const actions = {
toNearest: (api) => api.scrollToColumn('c12'),
toStart: (api) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api) => api.scrollToColumn('c1'),
};
options.ariaLabel = "코드로 컬럼을 보이게 가로 스크롤하기";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('toNearest').addEventListener('click', () => actions.toNearest(api));
document.getElementById('toStart').addEventListener('click', () => actions.toStart(api));
document.getElementById('toCenter').addEventListener('click', () => actions.toCenter(api));
document.getElementById('toFirst').addEventListener('click', () => actions.toFirst(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.toNearest(api)}
>가까운 자리</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toStart(api)}
>왼쪽 끝에</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toCenter(api)}
>가운데</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toFirst(api)}
>컬럼 1</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn은 세로 위치는 그대로 두고 가로로만 컬럼을 보이게 해요. 첫 컬럼은 고정이라 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `컬럼 ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};버튼을 누르면 이 예제의 코드가 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.toNearest(api)"
>가까운 자리</button>
<button type="button" :disabled="!api" @click="api && actions.toStart(api)">왼쪽 끝에</button>
<button
type="button"
:disabled="!api"
@click="api && actions.toCenter(api)"
>가운데</button>
<button type="button" :disabled="!api" @click="api && actions.toFirst(api)">컬럼 1</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn은 세로 위치는 그대로 두고 가로로만 컬럼을 보이게 해요. 첫 컬럼은 고정이라 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `컬럼 ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};버튼을 누르면 이 예제의 코드가 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.toNearest(api)"
>가까운 자리</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toStart(api)"
>왼쪽 끝에</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toCenter(api)"
>가운데</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toFirst(api)"
>컬럼 1</button>
</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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn은 세로 위치는 그대로 두고 가로로만 컬럼을 보이게 해요. 첫 컬럼은 고정이라 늘 보여요.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: '번호', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `컬럼 ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};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="toNearest">Nearest</button>
<button type="button" id="toStart">Align start</button>
<button type="button" id="toCenter">Center</button>
<button type="button" id="toFirst">Column 1</button>
</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('toNearest').addEventListener('click', () => actions.toNearest(api));
document.getElementById('toStart').addEventListener('click', () => actions.toStart(api));
document.getElementById('toCenter').addEventListener('click', () => actions.toCenter(api));
document.getElementById('toFirst').addEventListener('click', () => actions.toFirst(api));const rows = Array.from({ length: 8 }, (_, r) => {
const row = { id: r + 1 };
for (let c = 1; c <= 20; c++)
row[`c${c}`] = r * c;
return row;
});
// scrollToColumn keeps the vertical position and only scrolls horizontally to reveal the column. The first column is pinned, so it is always visible.
export const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `Column ${i + 1}`,
kind: 'number',
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api) => api.scrollToColumn('c12'),
toStart: (api) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api) => api.scrollToColumn('c1'),
};<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Scroll a column into view from code</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="toNearest">Nearest</button>
<button type="button" id="toStart">Align start</button>
<button type="button" id="toCenter">Center</button>
<button type="button" id="toFirst">Column 1</button>
</div>
<div id="grid" style="height: 300px"></div>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script>
const rows = Array.from({ length: 8 }, (_, r) => {
const row = { id: r + 1 };
for (let c = 1; c <= 20; c++)
row[`c${c}`] = r * c;
return row;
});
// scrollToColumn keeps the vertical position and only scrolls horizontally to reveal the column. The first column is pinned, so it is always visible.
const options = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `Column ${i + 1}`,
kind: 'number',
width: 110,
})),
],
rows,
rowKey: 'id',
};
const actions = {
toNearest: (api) => api.scrollToColumn('c12'),
toStart: (api) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api) => api.scrollToColumn('c1'),
};
options.ariaLabel = "Scroll a column into view from code";
const api = MaxGrid.createGrid(document.getElementById('grid'), options);
document.getElementById('toNearest').addEventListener('click', () => actions.toNearest(api));
document.getElementById('toStart').addEventListener('click', () => actions.toStart(api));
document.getElementById('toCenter').addEventListener('click', () => actions.toCenter(api));
document.getElementById('toFirst').addEventListener('click', () => actions.toFirst(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.toNearest(api)}
>Nearest</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toStart(api)}
>Align start</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toCenter(api)}
>Center</button>
<button
type="button"
disabled={!api}
onClick={() => api && actions.toFirst(api)}
>Column 1</button>
</div>
<Grid style={{ height: 300 }} ref={gridRef} options={options} />
</>
);
}import type { GridApi, GridOptions } from '@maxgrid/react';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn keeps the vertical position and only scrolls horizontally to reveal the column. The first column is pinned, so it is always visible.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `Column ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};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.toNearest(api)"
>Nearest</button>
<button type="button" :disabled="!api" @click="api && actions.toStart(api)">Align start</button>
<button
type="button"
:disabled="!api"
@click="api && actions.toCenter(api)"
>Center</button>
<button type="button" :disabled="!api" @click="api && actions.toFirst(api)">Column 1</button>
</div>
<Grid style="height: 300px" :options="options" @ready="onReady" />
</template>import type { GridApi, GridOptions } from '@maxgrid/vue';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn keeps the vertical position and only scrolls horizontally to reveal the column. The first column is pinned, so it is always visible.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `Column ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};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.toNearest(api)"
>Nearest</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toStart(api)"
>Align start</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toCenter(api)"
>Center</button>
<button
type="button"
[disabled]="!api"
(click)="api && actions.toFirst(api)"
>Column 1</button>
</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 { GridApi, GridOptions } from '@maxgrid/angular';
export type LogText = string | { ko: string; en: string };
export interface Row {
id: number;
[key: string]: number;
}
const rows: Row[] = Array.from({ length: 8 }, (_, r) => {
const row: Row = { id: r + 1 };
for (let c = 1; c <= 20; c++) row[`c${c}`] = r * c;
return row;
});
// scrollToColumn keeps the vertical position and only scrolls horizontally to reveal the column. The first column is pinned, so it is always visible.
export const options: GridOptions<Row> = {
columns: [
{ id: 'id', title: 'No.', kind: 'number', width: 80, pinned: 'start' },
...Array.from({ length: 20 }, (_, i) => ({
id: `c${i + 1}`,
title: `Column ${i + 1}`,
kind: 'number' as const,
width: 110,
})),
],
rows,
rowKey: 'id',
};
export const actions = {
toNearest: (api: GridApi<Row>) => api.scrollToColumn('c12'),
toStart: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'start' }),
toCenter: (api: GridApi<Row>) => api.scrollToColumn('c12', { align: 'center' }),
toFirst: (api: GridApi<Row>) => api.scrollToColumn('c1'),
};