jQuery로 그리드 만들기Create a grid with jQuery
스크립트 태그 세 줄(jQuery, 맥스그리드, 어댑터)로 $('#grid').maxgrid(옵션)을 쓰고, 그리드 이벤트를 jQuery 이벤트(maxgrid:이름)로 받아요.Three script tags (jQuery, MaxGrid, the adapter) give you $('#grid').maxgrid(options), and grid events arrive as jQuery events (maxgrid:name).
코드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 class="toolbar">
<button type="button" id="update">옵션만 바꾸기</button>
<button type="button" id="getApi">API 꺼내기</button>
</div>
<div id="grid" style="height: 300px"></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 options = {
columns: [
{ id: 'name', title: '이름', width: 150 },
{ id: 'dept', title: '부서', width: 130 },
{ id: 'salary', title: '연봉', kind: 'number', width: 130, align: 'end' },
],
rows: [
{ id: 1, name: '김서연', dept: '플랫폼', salary: 5400000 },
{ id: 2, name: '박지훈', dept: '모바일', salary: 4800000 },
{ id: 3, name: '이도윤', dept: '플랫폼', salary: 6100000 },
{ id: 4, name: '최하은', dept: '디자인', salary: 5200000 },
{ id: 5, name: '정우진', dept: '모바일', salary: 4500000 },
{ id: 6, name: '한지민', dept: '디자인', salary: 4950000 },
],
rowKey: 'id',
};
let say = () => { };
function setup(api, log) {
say = log;
const $grid = $('#grid');
// 그리드 이벤트는 jQuery 이벤트 maxgrid:<이벤트 이름>으로 와요. 둘째 인자가 이벤트 내용이에요.
$grid.on('maxgrid:rowClicked', (_event, p) => log({ ko: `maxgrid:rowClicked ${p.rowKey}번 행`, en: `maxgrid:rowClicked row ${p.rowKey}` }));
$grid.on('maxgrid:sortChanged', (_event, p) => log(`maxgrid:sortChanged ${p.sortModel.map((s) => s.columnId).join(', ') || '(none)'}`));
void api;
return () => $grid.off('maxgrid:rowClicked maxgrid:sortChanged');
}
const actions = {
// 같은 요소에 .maxgrid()를 다시 부르면 setOptions예요(그리드가 하나 더 생기지 않아요).
update: () => {
const striped = $('#grid').maxgrid('api').getOptions().striped !== false;
$('#grid').maxgrid({ striped: !striped });
say(`$('#grid').maxgrid({ striped: ${!striped} }) (setOptions)`);
},
// .maxgrid('api')는 첫 요소의 그리드 API를 돌려줘요.
getApi: () => {
const api = $('#grid').maxgrid('api');
say({
ko: `$('#grid').maxgrid('api').getRows() -> ${api.getRows().length}행`,
en: `$('#grid').maxgrid('api').getRows() -> ${api.getRows().length} rows`,
});
},
};
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);
});
document.getElementById('update').addEventListener('click', () => actions.update(api));
document.getElementById('getApi').addEventListener('click', () => actions.getApi(api));
</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>Create a grid with jQuery</title>
</head>
<body>
<div class="toolbar">
<button type="button" id="update">Change options only</button>
<button type="button" id="getApi">Get the API</button>
</div>
<div id="grid" style="height: 300px"></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 options = {
columns: [
{ id: 'name', title: 'Name', width: 150 },
{ id: 'dept', title: 'Department', width: 130 },
{ id: 'salary', title: 'Salary', kind: 'number', width: 130, align: 'end' },
],
rows: [
{ id: 1, name: 'Emma Stone', dept: 'Platform', salary: 5400000 },
{ id: 2, name: 'Noah Reed', dept: 'Mobile', salary: 4800000 },
{ id: 3, name: 'Liam Carter', dept: 'Platform', salary: 6100000 },
{ id: 4, name: 'Olivia Hart', dept: 'Design', salary: 5200000 },
{ id: 5, name: 'Sophia Lane', dept: 'Mobile', salary: 4500000 },
{ id: 6, name: 'Mason Clark', dept: 'Design', salary: 4950000 },
],
rowKey: 'id',
};
let say = () => { };
function setup(api, log) {
say = log;
const $grid = $('#grid');
// Grid events arrive as jQuery events named maxgrid:<event name>; the payload is the second argument.
$grid.on('maxgrid:rowClicked', (_event, p) => log(`maxgrid:rowClicked row ${p.rowKey}`));
$grid.on('maxgrid:sortChanged', (_event, p) => log(`maxgrid:sortChanged ${p.sortModel.map((s) => s.columnId).join(', ') || '(none)'}`));
void api;
return () => $grid.off('maxgrid:rowClicked maxgrid:sortChanged');
}
const actions = {
// Calling .maxgrid() again on the same element runs setOptions (no second grid is created).
update: () => {
const striped = $('#grid').maxgrid('api').getOptions().striped !== false;
$('#grid').maxgrid({ striped: !striped });
say(`$('#grid').maxgrid({ striped: ${!striped} }) (setOptions)`);
},
// .maxgrid('api') returns the grid API of the first element.
getApi: () => {
const api = $('#grid').maxgrid('api');
say(`$('#grid').maxgrid('api').getRows() -> ${api.getRows().length} rows`);
},
};
options.ariaLabel = "Create a grid with 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);
});
document.getElementById('update').addEventListener('click', () => actions.update(api));
document.getElementById('getApi').addEventListener('click', () => actions.getApi(api));
</script>
</body>
</html>