Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active January 31, 2017 11:59
Show Gist options
  • Save gabrielmontagne/6291cffce1c783cb94e6f565fb185b3f to your computer and use it in GitHub Desktop.
Save gabrielmontagne/6291cffce1c783cb94e6f565fb185b3f to your computer and use it in GitHub Desktop.
Zambezi Grid -- cell selection (programmatic and through user interaction)
height: 1000
license: mit

cell selection component

The cell selection component allows multiple selection (and block selection) with the usual modifiers. Programmatic selection is also possible by reconfiguring the cell selection component and redrawing the grid.

Selections are defined by arrays of [{row, column}] where row and column are references to the "real" objects. Using {row: indexInRawData, column: columnId} is also supported.

An active cell is also exposed -- it is the last cell selected by user interaction. Block selection is defined from that active cell.

<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment