Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active March 15, 2017 16:30
Show Gist options
  • Save gabrielmontagne/61bf45ef534990696005d88bfcabb164 to your computer and use it in GitHub Desktop.
Save gabrielmontagne/61bf45ef534990696005d88bfcabb164 to your computer and use it in GitHub Desktop.
Zambezi Grid basic configuration
license: mit

Basic zambezi grid render with,

  • nested column configuration
  • virtualised rows and columns
  • locked columns and rows
  • column resize
  • column drag (on modern browsers!)
<!doctype html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/[email protected]">
<style>
.zambezi-grid-cell .icons {
font-family: courier-new, monospace;
background-color: #BBB;
color: white;
}
.zambezi-grid-cell.fancy-cell {
background-color: #F5F6F6;
}
.zambezi-grid-cell.fancy-plus {
color: #A00;
}
</style>
</head>
<div class="grid-target"></div>
<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-components@0"></script>
<script src="https://npmcdn.com/@zambezi/grid@0"></script>
<script>
var table = grid.createGrid()
.columns(
[
{ key: 'name', locked: 'left', width: 200 }
, { key: 'email', sortDescending: true }
, { key: 'phone' }
, { key: 'username', className: 'fancy-cell fancy-plus' }
, {
components: [ translate, grid.updateTextIfChanged ]
, label: 'custom'
, format: r => r.username
, sort: customSort
, width: 150
, template:
'<span>'
+ '<strong class="icons"></strong> — <i class="formatted-text"></i>'
+ '</span>'
}
, {
label: 'address'
, children: [
{ label: 'city', key: 'address.city', width: 300 }
, { label: 'country', key: 'address.ukCountry' }
, { label: 'county', key: 'address.ukCounty' }
]
}
]
)
, rows = _.range(1, 2000).map(faker.Helpers.createCard)
, icon = d3.scaleOrdinal().range('▀▄█▌▐░▒▓■')
rows[10].locked = 'top'
rows[11].locked = 'top'
rows[101].locked = 'bottom'
rows[102].locked = 'bottom'
d3.select('.grid-target')
.style('height', '500px')
.datum(rows)
.call(table)
function translate(d) {
d3.select(this)
.select('.icons')
.text(d.value.username.split('').slice(0, 5).map(icon).join(''))
}
function customSort(a, b) {
return d3.ascending(a.email.split(`@`)[0], b.email.split(`@`)[0]) || d3.ascending(a.name, b.name)
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment