|
<!doctype html> |
|
<meta charset="utf-8" /> |
|
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/[email protected]"> |
|
|
|
<div class="grid-target"></div> |
|
|
|
<p> |
|
<button id="clear-selection">clear selection</button> |
|
<button id="programmatic-select">programmtically select some cells</button> |
|
<button id="programmatic-select-by-indices">programmtically select some cells (using row index and column id)</button> |
|
</p> |
|
|
|
<pre id="output"></pre> |
|
|
|
<script src="https://npmcdn.com/[email protected]"></script> |
|
<script src="https://npmcdn.com/[email protected]/faker.js"></script> |
|
<script src="https://d3js.org/d3.v4.js"></script> |
|
<script src="https://npmcdn.com/@zambezi/fun@2"></script> |
|
<script src="https://npmcdn.com/@zambezi/d3-utils@3"></script> |
|
<script src="https://npmcdn.com/@zambezi/grid@0"></script> |
|
<script src="https://npmcdn.com/@zambezi/[email protected]"></script> |
|
<script> |
|
|
|
var rows = _.range(1, 50).map(faker.Helpers.createCard) |
|
, cells = gridComponents.createCellSelection() |
|
.on( |
|
'cell-selected-change.log' |
|
, (selected, active) => console.log(horribleBanner(selected, active)) |
|
) |
|
|
|
, table = grid.createGrid() |
|
.columns( |
|
[ |
|
{ key: 'name', locked: 'left', width: 200 } |
|
, { key: 'username' } |
|
, { key: 'email' } |
|
, { |
|
label: 'some synthetic column' |
|
, format: summary |
|
, sort: (a, b) => d3.ascending(a.name, b.name) |
|
} |
|
, { key: 'phone' } |
|
] |
|
) |
|
.usePre(cells) |
|
.on('draw.log', onDraw) |
|
|
|
, output = d3.select('#output') |
|
, target = d3.select('.grid-target') |
|
.style('height', '300px') |
|
.datum(rows) |
|
.call(table) |
|
|
|
d3.select('#clear-selection') |
|
.on('click.reset', () => cells.selected([]).active(null)) |
|
.on('click.redraw', () => target.call(table)) |
|
|
|
d3.select('#programmatic-select') |
|
.on('click.clear', () => console.clear()) |
|
.on('click.reset', () => cells.selected(bunchOfCells())) |
|
.on('click.redraw', () => target.call(table)) |
|
|
|
d3.select('#programmatic-select-by-indices') |
|
.on('click.clear', () => console.clear()) |
|
.on( |
|
'click.reset' |
|
, () => cells.selected([ { row: 1, column: 'username' }, { row: 2, column: 'email'} ]) |
|
) |
|
.on('click.redraw', () => target.call(table)) |
|
|
|
function onDraw() { |
|
output.text(horribleBanner(cells.selected(), cells.active())) |
|
} |
|
|
|
function horribleBanner(selected, active) { |
|
return `ACTIVE |
|
\n${ active ? toRepr(active) : '(no active cell)' } |
|
\nSELECTED |
|
\n${ selected.map(toRepr).join('\n') } |
|
|
|
\n▒▓ ${ Date.now().toString(32).toUpperCase() } |
|
` |
|
} |
|
|
|
function bunchOfCells() { |
|
return d3.range( |
|
_.random(20, 40)) |
|
.map(() => ({ row: _.sample(rows), column: _.sample(table.columns()) }) |
|
) |
|
} |
|
|
|
function toRepr({column , row}) { |
|
return column.id + ' <' + column.format(column.value(row)) + '>' |
|
} |
|
|
|
function summary(row) { |
|
return `${row.username.slice(0, 3)} -- ${row.address.city}` |
|
} |
|
|
|
</script> |