jQuery와 서버 모드(쪽·정렬·필터)jQuery with server mode (paging, sort, filter)
쪽 번호·정렬·필터를 서버에 보내고 { rows, totalRows }를 받는 서버 모드를 jQuery 어댑터로 써요. 예제는 브라우저 안의 가짜 서버로 대신하고, 코드 주석에 fetch로 바꾸는 줄이 있어요.Use server mode (send page, sort and filter, receive { rows, totalRows }) through the jQuery adapter. The example uses a fake server inside the browser; a comment shows the fetch line to use instead.
코드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)
이 HTML은 추가 번들(위 script 태그)도 함께 불러오고 전역 변수로 써요. 별도 설치는 필요 없어요.
<!doctype html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>jQuery와 서버 모드(쪽·정렬·필터)</title>
</head>
<body>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.jquery.min.js"></script>
<script>
const NAMES = [
'김서연',
'박지훈',
'이도윤',
'최하은',
'정우진',
'한지민',
'오세훈',
'강다은',
'윤태양',
'송가영',
];
const DEPTS = ['플랫폼', '모바일', '디자인', '데이터'];
const ALL = Array.from({ length: 85 }, (_, i) => ({
id: i + 1,
name: NAMES[i % NAMES.length] + ' ' + (Math.floor(i / NAMES.length) + 1),
dept: DEPTS[i % DEPTS.length],
salary: 4000000 + ((i * 137000) % 3000000),
}));
// Log lines that arrive before setup runs wait here.
const pending = [];
let say = (m) => void pending.push(m);
// 브라우저 안의 가짜 서버예요. 진짜 서버라면 loadPage에서 아래 한 줄로 바꿔요(서버 약속은 Java·Node 서버와 쓰기 가이드에 있어요).
// return fetch('api/staff?' + new URLSearchParams({ page: String(q.page), pageSize: String(q.pageSize), sort: JSON.stringify(q.sort) })).then((r) => r.json());
function fakeServer(q) {
let rows = [...ALL];
for (const s of [...q.sort].reverse()) {
const key = s.columnId;
const sign = s.direction === 'desc' ? -1 : 1;
rows.sort((a, b) => (a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0) * sign);
}
const total = rows.length;
rows = rows.slice((q.page - 1) * q.pageSize, q.page * q.pageSize);
return { rows, totalRows: total };
}
const options = {
columns: [
{ id: 'name', title: '이름', width: 170, sortable: true },
{ id: 'dept', title: '부서', width: 130, sortable: true },
{ id: 'salary', title: '연봉', kind: 'number', width: 130, align: 'end', sortable: true },
],
rows: [],
rowKey: 'id',
pagination: {
mode: 'server',
pageSize: 10,
loadPage: (q) => {
const sort = q.sort.map((s) => s.columnId + ' ' + s.direction).join(', ') || '(none)';
say({
ko: `서버가 받은 요청 page ${q.page}, pageSize ${q.pageSize}, sort ${sort}`,
en: `server got page ${q.page}, pageSize ${q.pageSize}, sort ${sort}`,
});
const { rows, totalRows: total } = fakeServer(q);
return new Promise((resolve) => setTimeout(() => {
say({
ko: `서버가 보낸 응답 ${rows.length}행 / 전체 ${total}행`,
en: `server sent ${rows.length} rows of ${total}`,
});
resolve({ rows, totalRows: total });
}, 150));
},
},
};
function setup(api, log) {
say = log;
for (const m of pending.splice(0))
log(m);
// Grid events are jQuery events too.
const $grid = $('#grid');
$grid.on('maxgrid:sortChanged', () => log('maxgrid:sortChanged'));
void api;
return () => $grid.off('maxgrid:sortChanged');
}
options.ariaLabel = "jQuery와 서버 모드(쪽·정렬·필터)";
const api = $('#grid').maxgrid(options).maxgrid('api');
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
</script>
</body>
</html>This HTML also loads an extra bundle (the extra script tag above) and uses it as a global. No install is needed.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>jQuery with server mode (paging, sort, filter)</title>
</head>
<body>
<div id="grid" style="height: 440px"></div>
<ul id="log" aria-live="polite"></ul>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/latest/maxgrid.jquery.min.js"></script>
<script>
const NAMES = [
'Emma Stone',
'Noah Reed',
'Liam Carter',
'Olivia Hart',
'Sophia Lane',
'Mason Clark',
'Ava Brooks',
'Lucas Gray',
'Mia Flores',
'Ethan Cole',
];
const DEPTS = ['Platform', 'Mobile', 'Design', 'Data'];
const ALL = Array.from({ length: 85 }, (_, i) => ({
id: i + 1,
name: NAMES[i % NAMES.length] + ' ' + (Math.floor(i / NAMES.length) + 1),
dept: DEPTS[i % DEPTS.length],
salary: 4000000 + ((i * 137000) % 3000000),
}));
// Log lines that arrive before setup runs wait here.
const pending = [];
let say = (m) => void pending.push(m);
// A fake server inside the browser. With a real server replace the body of loadPage with the line below (the contract is in the Java and Node server guide).
// return fetch('api/staff?' + new URLSearchParams({ page: String(q.page), pageSize: String(q.pageSize), sort: JSON.stringify(q.sort) })).then((r) => r.json());
function fakeServer(q) {
let rows = [...ALL];
for (const s of [...q.sort].reverse()) {
const key = s.columnId;
const sign = s.direction === 'desc' ? -1 : 1;
rows.sort((a, b) => (a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0) * sign);
}
const total = rows.length;
rows = rows.slice((q.page - 1) * q.pageSize, q.page * q.pageSize);
return { rows, totalRows: total };
}
const options = {
columns: [
{ id: 'name', title: 'Name', width: 170, sortable: true },
{ id: 'dept', title: 'Department', width: 130, sortable: true },
{ id: 'salary', title: 'Salary', kind: 'number', width: 130, align: 'end', sortable: true },
],
rows: [],
rowKey: 'id',
pagination: {
mode: 'server',
pageSize: 10,
loadPage: (q) => {
const sort = q.sort.map((s) => s.columnId + ' ' + s.direction).join(', ') || '(none)';
say(`server got page ${q.page}, pageSize ${q.pageSize}, sort ${sort}`);
const { rows, totalRows: total } = fakeServer(q);
return new Promise((resolve) => setTimeout(() => {
say(`server sent ${rows.length} rows of ${total}`);
resolve({ rows, totalRows: total });
}, 150));
},
},
};
function setup(api, log) {
say = log;
for (const m of pending.splice(0))
log(m);
// Grid events are jQuery events too.
const $grid = $('#grid');
$grid.on('maxgrid:sortChanged', () => log('maxgrid:sortChanged'));
void api;
return () => $grid.off('maxgrid:sortChanged');
}
options.ariaLabel = "jQuery with server mode (paging, sort, filter)";
const api = $('#grid').maxgrid(options).maxgrid('api');
setup(api, (message) => {
const item = document.createElement('li');
item.textContent =
typeof message === 'string' ? message : navigator.language.startsWith('ko') ? message.ko : message.en;
document.getElementById('log').prepend(item);
});
</script>
</body>
</html>