Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active September 20, 2016 09:45
Show Gist options
  • Save gabrielmontagne/1f7f21300e14be360c9ac6081db16c68 to your computer and use it in GitHub Desktop.
Save gabrielmontagne/1f7f21300e14be360c9ac6081db16c68 to your computer and use it in GitHub Desktop.
Zambezi Grid -- grouped rows
license: mit

Zambezi Grid -- grouped rows

<!doctype html>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/[email protected]">
<div class="grid-target"></div>
<script src="https://npmcdn.com/[email protected]"></script>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://npmcdn.com/[email protected]/faker.js"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script src="https://npmcdn.com/@zambezi/[email protected]"></script>
<script>
var table = grid.createGrid()
.columns(
[
{
components: [ grid.createNestedRowExpanders() ]
, format: hierarchyFormat
, sortable: false
, width: 170
, id: 'expanders'
, label: 'Tree'
, locked: 'left'
}
, {
label: 'Name'
, format: grid.formatNonRollup(fun.property('name'))
, sort: fun.compareWith(d3.ascending, fun.property('name'))
, sortAscending: true
}
, { key: 'email', label: 'Email' }
, { key: 'phone', label: 'Phone' }
, { key: 'address.city', label: 'City' }
]
)
.groupings(
[
{ key: d => d.address.ukCountry , rollup: countryRollup }
, { key: r => r.name[0] , rollup: firstLetterRollup }
, { key: d => d.address.ukCounty , rollup: countyRollup }
]
)
, rows = _.range(1, 5000).map(faker.Helpers.createCard)
, target = d3.select('.grid-target')
.style('height', '500px')
.datum(rows)
.call(table)
function hierarchyFormat(row) {
switch (row.nestLevel) {
case 0: return row.address ? row.address.ukCountry : ''
case 1: return row.name ? row.name[0] : ''
case 2: return row.address ? `${row.address.ukCounty} (${row.children.length})` : ''
default: return row.name.split(' ')[0]
}
}
function firstLetterRollup(row) {
row.name = row.children[0].name[0]
return row
}
function countryRollup(row) {
row.address = { ukCountry: row.children[0].address.ukCountry }
return row
}
function countyRollup(row) {
row.address = { ukCounty: row.children[0].address.ukCounty }
return row
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment