Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active February 16, 2020 07:05
Show Gist options
  • Save gabrielmontagne/7902566377f8ede777bb3cf493237c6e to your computer and use it in GitHub Desktop.
Save gabrielmontagne/7902566377f8ede777bb3cf493237c6e to your computer and use it in GitHub Desktop.
Zambezi Grid -- active row component
license: mit
height: 1000

row-selection is a simple component that will automatically highlight the active row; that is, the row you have clicked.

<!doctype html>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/normalize.css@4">
<div class="grid-target"></div>
<button id="set-active-random-row">set active to random row</button>
<pre id="output"></pre>
<script src="https://npmcdn.com/underscore@1"></script>
<script src="https://npmcdn.com/faker@1.0.0/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/grid-components@0.11.0-active-selection.2"></script>
<script>
const rows = _.range(1, 100).map(faker.Helpers.createCard)
, rowSelection = gridComponents.createRowSelection()
.on('row-active-change.log', r => console.log('row active change', r))
, out = d3.select('#output')
, setActiveRandomRow = d3.select('#set-active-random-row')
.on('click.configure', () => rowSelection.active(_.sample(rows)))
.on('click.redraw', draw)
, table = grid.createGrid()
.columns(
[
{ key: 'name', locked: 'left', width: 200 }
, { key: 'username' }
, { key: 'email' }
, {
label: 'synth column'
, format: r => `${r.name} ${r.email}`.split('').sort().reverse().join('').toUpperCase()
, sort: (a, b) => d3.ascending(a.name, b.name)
}
, { key: 'phone' }
]
)
.usePre(rowSelection)
.on('draw.log', () => out.text('active row:\n' + JSON.stringify(rowSelection.active(), null, 2)))
, target = d3.select('.grid-target')
.datum(rows)
.style('height', '600px')
draw()
function draw() {
target.call(table)
}
</script>
@shodhan
Copy link

shodhan commented Feb 16, 2020

Hi , this is very helpful, i know i am late but is there anyway to change the color of the highlight?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment