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

정렬 중에 줄이 뛰지 않게 행 추가·수정하기Add and edit rows without the sorted rows jumping

addRows와 updateRows에 resort: false를 주면 정렬된 상태에서도 새 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요. 정렬이나 필터를 바꾸면 평소 순서로 돌아가요.With resort: false, addRows and updateRows keep a new row at the end and an edited row at its current place even while a sort is active. Changing the sort or filter returns to the normal order.

활용Intermediate
직접 써 보기Live preview정렬 중에 줄이 뛰지 않게 행 추가·수정하기Add and edit rows without the sorted rows jumping

코드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="addSorted">행 추가 (정렬)</button>
    <button type="button" id="addStay">행 추가 (맨 뒤)</button>
    <button type="button" id="bumpSorted">하늘 99점 (정렬)</button>
    <button type="button" id="bumpStay">하늘 99점 (제자리)</button>
    <button type="button" id="resort">정렬 다시 적용</button>
  </div>
  <div id="grid" style="height: 250px"></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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(api));
example.js
const rows = [
  { id: 1, name: '민수', score: 90 },
  { id: 2, name: '서연', score: 70 },
  { id: 3, name: '지호', score: 50 },
  { id: 4, name: '하늘', score: 30 },
];
let next = 5;
// 정렬 중에 행을 더하거나 고치면 줄이 바로 뛰어요. resort: false를 주면 다음 정렬·필터 변경 전까지
// 추가한 행은 맨 뒤에, 고친 행은 지금 자리에 머물러요.
export const options = {
  columns: [
    { id: 'id', title: '번호', kind: 'number', width: 70 },
    { id: 'name', title: '이름', width: 100 },
    { id: 'score', title: '점수', kind: 'number', width: 100 },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  next = 5;
  api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
  const off = api.on('sortChanged', () => log({
    ko: '정렬을 바꿔서 임시 자리가 사라졌어요',
    en: 'Sort changed: the temporary places are gone',
  }));
  return () => off();
}
export const actions = {
  addSorted: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }]),
  addStay: (api) => api.addRows([{ id: next++, name: `새${next}`, score: 80 }], { resort: false }),
  bumpSorted: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }]),
  bumpStay: (api) => api.updateRows([{ id: 4, name: '하늘', score: 99 }], { resort: false }),
  resort: (api) => api.setSortModel([
    { columnId: 'score', direction: 'desc' },
    { columnId: 'id', direction: 'asc' },
  ]),
};

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="addSorted">Add (sorted)</button>
    <button type="button" id="addStay">Add (at the end)</button>
    <button type="button" id="bumpSorted">Bump (sorted)</button>
    <button type="button" id="bumpStay">Bump (stay)</button>
    <button type="button" id="resort">Apply sort again</button>
  </div>
  <div id="grid" style="height: 250px"></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('addSorted').addEventListener('click', () => actions.addSorted(api));
document.getElementById('addStay').addEventListener('click', () => actions.addStay(api));
document.getElementById('bumpSorted').addEventListener('click', () => actions.bumpSorted(api));
document.getElementById('bumpStay').addEventListener('click', () => actions.bumpStay(api));
document.getElementById('resort').addEventListener('click', () => actions.resort(api));
example.js
const rows = [
  { id: 1, name: 'Liam', score: 90 },
  { id: 2, name: 'Emma', score: 70 },
  { id: 3, name: 'Noah', score: 50 },
  { id: 4, name: 'Olivia', score: 30 },
];
let next = 5;
// Adding or editing rows while sorted makes lines jump right away. With resort: false, added rows go to the end
// and edited rows stay where they are until the next sort or filter change.
export const options = {
  columns: [
    { id: 'id', title: 'No.', kind: 'number', width: 70 },
    { id: 'name', title: 'Name', width: 100 },
    { id: 'score', title: 'Score', kind: 'number', width: 100 },
  ],
  rows,
  rowKey: 'id',
};
export function setup(api, log) {
  next = 5;
  api.setSortModel([{ columnId: 'score', direction: 'desc' }]);
  const off = api.on('sortChanged', () => log('Sort changed: the temporary places are gone'));
  return () => off();
}
export const actions = {
  addSorted: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }]),
  addStay: (api) => api.addRows([{ id: next++, name: `New${next}`, score: 80 }], { resort: false }),
  bumpSorted: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }]),
  bumpStay: (api) => api.updateRows([{ id: 4, name: 'Olivia', score: 99 }], { resort: false }),
  resort: (api) => api.setSortModel([
    { columnId: 'score', direction: 'desc' },
    { columnId: 'id', direction: 'asc' },
  ]),
};