Component from the Zambezi grid components
Last active
January 11, 2017 10:28
-
-
Save gabrielmontagne/a6aaeeef596ad1473532318d153f0836 to your computer and use it in GitHub Desktop.
Zambezi Grid -- edit cell values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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://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> | |
const rows = _.range(1, 10).map(faker.Helpers.createCard) | |
, table = grid.createGrid() | |
.columns( | |
[ | |
{ key: 'name', locked: 'left', width: 200 } | |
, { | |
key: 'username' | |
, components: [ | |
gridComponents.createEditCellValue() | |
.on('change', onUsernameChange) | |
.on('validationerror', console.error.bind(console, 'VALIDATION')) | |
.characterClass('A-Za-z0-5') | |
.validate(validateUserName) | |
.editable(isNotZack) | |
, grid.updateTextIfChanged | |
] | |
} | |
, { key: 'email', sortDescending: true } | |
, { key: 'phone' } | |
] | |
) | |
d3.select('.grid-target') | |
.style('height', '500px') | |
.datum(rows) | |
.call(table) | |
function onUsernameChange(row, value) { | |
row.username = value.toUpperCase() | |
} | |
function validateUserName(row, value) { | |
if (!value) return 'Username cannot be empty' | |
if (!value.split('').some(isDifferentCharacter())) return 'Cannot all be the same character' | |
} | |
function isDifferentCharacter() { | |
let found | |
return function check(ch) { | |
if (found && !~found.indexOf(ch)) return true | |
found = ch | |
return false | |
} | |
} | |
function isNotZack(cell) { | |
return cell.row.username.indexOf('Zac') !== 0 | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment