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

새 행에 줄 순서 번호 받기Get an order number for a new row

nextOrderValue로 맨 앞·맨 뒤·특정 행 뒤에 넣을 새 행의 번호를 받아요. 받은 번호로 addRows의 index 자리에 넣으면 번호 순서와 화면 순서가 맞아요.nextOrderValue returns the number for a new row at the start, at the end or after a given row. Add the row with addRows at the matching index and the number order matches the screen order.

입문Beginner
직접 써 보기Live preview새 행에 줄 순서 번호 받기Get an order number for a new row

코드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="addStart">맨 앞에 추가</button>
    <button type="button" id="addEnd">맨 뒤에 추가</button>
    <button type="button" id="addAfterSecond">둘째 행 뒤에 추가</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('addStart').addEventListener('click', () => actions.addStart(api));
document.getElementById('addEnd').addEventListener('click', () => actions.addEnd(api));
document.getElementById('addAfterSecond').addEventListener('click', () => actions.addAfterSecond(api));
example.js
const tasks = ['요구사항 정리', '화면 설계', '데이터 모델', 'API 구현', '테스트 작성'];
export const options = {
  columns: [
    { id: 'seq', title: '순서 번호', width: 90, align: 'end' },
    { id: 'task', title: '할 일', width: 200 },
  ],
  rows: tasks.map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task })),
  rowKey: 'id',
  orderIndex: { field: 'seq' },
};
let say = () => { };
let nextId = 6;
export function setup(api, log) {
  say = log;
  nextId = 6;
  log({
    ko: '새 행의 번호는 nextOrderValue로 받아요. 넣을 자리의 앞뒤 번호 사이 값이에요.',
    en: 'The number for a new row comes from nextOrderValue: a value between the neighbors of its spot.',
  });
  return () => { };
}
function add(api, seq, index) {
  const id = nextId++;
  api.addRows([{ id, seq, task: `새 업무 ${id}` }], { index });
  say({ ko: `${id}번 행을 추가했어요: 번호 ${seq}`, en: `Added row ${id} with number ${seq}` });
}
export const actions = {
  addStart: (api) => add(api, api.nextOrderValue('start'), 0),
  addEnd: (api) => add(api, api.nextOrderValue('end'), api.getRowCount()),
  addAfterSecond: (api) => {
    const second = api.getRows()[1];
    add(api, api.nextOrderValue({ after: second.id }), 2);
  },
};

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="addStart">Add at start</button>
    <button type="button" id="addEnd">Add at end</button>
    <button type="button" id="addAfterSecond">Add after second row</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('addStart').addEventListener('click', () => actions.addStart(api));
document.getElementById('addEnd').addEventListener('click', () => actions.addEnd(api));
document.getElementById('addAfterSecond').addEventListener('click', () => actions.addAfterSecond(api));
example.js
const tasks = ['Gather requirements', 'Design screens', 'Data model', 'Build API', 'Write tests'];
export const options = {
  columns: [
    { id: 'seq', title: 'Order', width: 80, align: 'end' },
    { id: 'task', title: 'Task', width: 200 },
  ],
  rows: tasks.map((task, i) => ({ id: i + 1, seq: (i + 1) * 10, task })),
  rowKey: 'id',
  orderIndex: { field: 'seq' },
};
let say = () => { };
let nextId = 6;
export function setup(api, log) {
  say = log;
  nextId = 6;
  log('The number for a new row comes from nextOrderValue: a value between the neighbors of its spot.');
  return () => { };
}
function add(api, seq, index) {
  const id = nextId++;
  api.addRows([{ id, seq, task: `New task ${id}` }], { index });
  say(`Added row ${id} with number ${seq}`);
}
export const actions = {
  addStart: (api) => add(api, api.nextOrderValue('start'), 0),
  addEnd: (api) => add(api, api.nextOrderValue('end'), api.getRowCount()),
  addAfterSecond: (api) => {
    const second = api.getRows()[1];
    add(api, api.nextOrderValue({ after: second.id }), 2);
  },
};