본문으로 건너뛰기Skip to content
예제 첫화면Examples home

코드로 컬럼을 보이게 가로 스크롤하기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.

활용Intermediate
직접 써 보기Live preview코드로 컬럼을 보이게 가로 스크롤하기Scroll a column into view 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)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<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>
main.js
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));
example.js
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'),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<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>
main.js
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));
example.js
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'),
};