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

두 그리드 사이에서 행 끌어 놓기Drag rows between two grids

rowDrag의 group과 accept로 두 그리드가 서로의 행을 받아요. 받는 쪽의 mode가 move면 원본에서 지워지고 copy면 남아요. 행 키가 겹치면 받지 않아요.With rowDrag group and accept, two grids accept each other's rows. If the receiving mode is move the source row is removed, with copy it stays. Rows whose keys collide are refused.

심화Advanced
직접 써 보기Live preview두 그리드 사이에서 행 끌어 놓기Drag rows between two grids

코드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="collide">키 겹치게 하기</button>
  </div>
  <div id="grid" style="height: 360px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('collide').addEventListener('click', () => actions.collide(api));
example.js
// 두 그리드는 group과 accept로 서로를 알아요. group: 'todo' 그리드는 'done'의 행을, 'done' 그리드는 'todo'의 행을 받아요.
// 받는 쪽의 mode가 'move'(기본)이면 원본에서 지워지고 'copy'면 남아요. 두 그리드의 행 키(id)는 겹치지 않아야 해요.
export const options = {
  columns: [{ id: 'task', title: '할 일', width: 220 }],
  rowDrag: { group: 'todo', accept: ['done'] },
  rows: [
    { id: 1, task: '요구사항 정리' },
    { id: 2, task: '화면 설계' },
    { id: 3, task: 'API 구현' },
  ],
  rowKey: 'id',
};
let second = null;
let host = null;
export function setup(api, log) {
  // 갤러리 틀이 위 그리드를 meta.height 높이로 만들어 두므로 둘이 함께 보이도록 줄여요.
  const first = document.getElementById('grid');
  if (first !== null)
    first.style.height = '170px';
  host = document.createElement('div');
  host.style.cssText = 'height:170px;margin-top:10px';
  if (first !== null)
    first.after(host);
  else
    document.body.append(host);
  second = MaxGrid.createGrid(host, {
    columns: [{ id: 'task', title: '끝낸 일', width: 220 }],
    rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
    rows: [{ id: 101, task: '킥오프' }],
    rowKey: 'id',
    ariaLabel: 'done grid',
  });
  log({
    ko: '위 그리드의 행을 아래 그리드로 끌어 놓아 보세요. 받는 쪽에 더해지고 원본에서 사라져요. 아래 행도 위로 끌 수 있어요.',
    en: 'Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.',
  });
  const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log({
    ko: `${i === 0 ? '위' : '아래'} 그리드 ${e.external ? '받음' : '보냄'}: ${e.keys.join(', ')} (${e.mode})`,
    en: `${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`,
  })));
  return () => {
    offs.forEach((off) => off());
    second?.destroy();
    host?.remove();
    second = null;
    host = null;
  };
}
export const actions = {
  collide: () => {
    // 같은 키(1)가 이미 있는 행을 아래로 보내 키 충돌 취소를 보여 줘요.
    if (second?.getRows().some((r) => r.id === 1))
      return;
    second?.addRows([{ id: 1, task: '키가 같은 행' }]);
  },
};

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="collide">Make a key collision</button>
  </div>
  <div id="grid" style="height: 360px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('collide').addEventListener('click', () => actions.collide(api));
example.js
// The two grids know each other through group and accept. The 'todo' grid accepts rows from 'done', and the 'done' grid accepts rows from 'todo'.
// If the receiving side's mode is 'move' (the default) the row is removed from the source, and with 'copy' it stays. The row keys (id) of the two grids must not overlap.
export const options = {
  columns: [{ id: 'task', title: 'To do', width: 220 }],
  rowDrag: { group: 'todo', accept: ['done'] },
  rows: [
    { id: 1, task: 'Gather requirements' },
    { id: 2, task: 'Design screens' },
    { id: 3, task: 'Build the API' },
  ],
  rowKey: 'id',
};
let second = null;
let host = null;
export function setup(api, log) {
  // The gallery frame makes the top grid with the meta.height height, so shrink it to show both together.
  const first = document.getElementById('grid');
  if (first !== null)
    first.style.height = '170px';
  host = document.createElement('div');
  host.style.cssText = 'height:170px;margin-top:10px';
  if (first !== null)
    first.after(host);
  else
    document.body.append(host);
  second = MaxGrid.createGrid(host, {
    columns: [{ id: 'task', title: 'Done', width: 220 }],
    rowDrag: { group: 'done', accept: ['todo'], mode: 'move' },
    rows: [{ id: 101, task: 'Kickoff' }],
    rowKey: 'id',
    ariaLabel: 'done grid',
  });
  log('Drag a row from the top grid into the bottom grid: it is added there and removed from the source. Rows can go back up too.');
  const offs = [api, second].map((grid, i) => grid.on('rowDropped', (e) => log(`${i === 0 ? 'Top' : 'Bottom'} grid ${e.external ? 'received' : 'sent'}: ${e.keys.join(', ')} (${e.mode})`)));
  return () => {
    offs.forEach((off) => off());
    second?.destroy();
    host?.remove();
    second = null;
    host = null;
  };
}
export const actions = {
  collide: () => {
    // Send a row whose key (1) already exists down, to show the key-collision cancel.
    if (second?.getRows().some((r) => r.id === 1))
      return;
    second?.addRows([{ id: 1, task: 'Row with the same key' }]);
  },
};