Use it with jQuery
Since 1.8.0
@maxgrid/jquery ships with 1.8.0. Server mode, saving and xlsx with a Java or Node server are in Use it with a Java or Node server.
A page that uses jQuery needs three script tags and no bundler. The adapter adds one thing, $('#grid').maxgrid(options). The grid itself only runs in the browser.
With script tags
<script src="jquery.min.js"></script>
<script src="https://cdn.maxmine.com/maxgrid/1.8.0/maxgrid.min.js" integrity="sha384-SsqJYyomnDFE9Aq35InPj08+I3SOPlTRGi17p59mmcEp914M1gDnC6n4TVAU+A13" crossorigin="anonymous"></script>
<script
src="https://cdn.maxmine.com/maxgrid/1.8.0/maxgrid.jquery.min.js"
integrity="sha384-VHH87VvG7VQUquPjPXwbkfUL62Al7UBByos/JrPcSOQUHAigYhRXDizdBwSkPiSY"
crossorigin="anonymous"
></script>
<div id="grid" style="height: 400px"></div>
<script>
$('#grid')
.on('maxgrid:rowClicked', function (e, p) {
console.log(p.rowKey);
})
.maxgrid({
ariaLabel: 'Staff',
rowKey: 'id',
columns: [
{ id: 'name', title: 'Name' },
{ id: 'salary', title: 'Salary', kind: 'number' },
],
rows: [{ id: 1, name: 'Kim', salary: 3500000 }],
});
var api = $('#grid').maxgrid('api'); // the grid API (rows, setSortModel, save ...)
// $('#grid').maxgrid('destroy'); // to remove only the grid and keep the element
</script>.maxgrid(options)creates a grid in every matched element and returns the jQuery object, so it chains. Calling it again on the same element runssetOptions..maxgrid('api')returns the grid API of the first element..maxgrid('destroy')removes the grid and its event links; jQuery calls it for you when it removes the element (.remove(),.empty(),.html(...),.replaceWith(...))..detach()keeps the grid so you can put the element back.- Grid events arrive as jQuery events named
maxgrid:<event name>(for examplemaxgrid:sortChanged). The second handler argument is the event payload. A handler on a parent element works too.
Tested jQuery versions
Tested with jQuery 1.12.4, 2.2.4, 3.7.1 and 4.0.0 in Chromium, Firefox and WebKit: creating on several elements at once, no events crossing between instances, after jQuery.noConflict(true), on a page with an AMD loader such as RequireJS, and cleanup after the element is removed (no listeners left behind). jQuery Migrate and two jQuery copies on one page were not tested (the adapter attaches to the window.jQuery that exists when the page loads it).
npm and a bundler
npm install maxgrid @maxgrid/jquery jqueryimport $ from 'jquery';
import * as MaxGrid from 'maxgrid';
import { installMaxGrid } from '@maxgrid/jquery';
installMaxGrid($, MaxGrid); // call it once
$('#grid').maxgrid({
rowKey: 'id',
columns: [{ id: 'name', title: 'Name' }],
rows: [{ id: 1, name: 'Kim' }],
});Unlike the script-tag bundle this needs no globals. maxgrid and jquery are optional peer dependencies, so install them in your own project.
TypeScript
With @types/jquery (3.x) $.fn.maxgrid and maxgrid:<event> are typed. Import @maxgrid/jquery once anywhere to widen the JQuery interface.
import '@maxgrid/jquery';
interface Staff {
id: number;
name: string;
}
const $grid = $('#grid').maxgrid<Staff>({
rowKey: 'id',
columns: [{ id: 'name', title: 'Name' }],
rows: [{ id: 1, name: 'Kim' }],
});
const api = $grid.maxgrid<Staff>('api'); // GridApi<Staff> | undefined
api?.setSortModel([{ columnId: 'name', direction: 'asc' }]);
$grid.on('maxgrid:sortChanged', (e, payload) => {
console.log(e.type, payload); // the payload is typed per event
});A misspelled command (.maxgrid('nope')) is a compile error. The type definitions are in the API reference under @maxgrid/jquery.