Skip to content

Instantly share code, notes, and snippets.

@gabrielmontagne
Last active January 10, 2017 14:21
Show Gist options
  • Save gabrielmontagne/f83bfda1e29501cbcd3a7ed820e18a2a to your computer and use it in GitHub Desktop.
Save gabrielmontagne/f83bfda1e29501cbcd3a7ed820e18a2a to your computer and use it in GitHub Desktop.
Zambezi Grid -- edit cells value component
license: mit

This example showcases the edit-cell component. Cells on the username column are editable. Double-click on them and change their value.

This is a usage example of the edit cell value for the Zambezi Grid

In this example, configuration has been added for

  • Validation
    • Username cannot be empty
    • Username cannot use only one letter
  • Character class for input: Only uppercase and lower case ASCII characters and numbers between 0 and 5 can used to input usernames.
  • Non editable cells: Cells for rows in which the username begins with "Zac" cannot be edited
  • Processing: Client can decide how to consolidate the change; in this case, it is turned into upper case
<!doctype html>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="https://npmcdn.com/[email protected]">
<style>
.external-editor {
display: none;
}
.external-editor.is-editing {
display: block;
}
.external-editor.is-invalid {
background-color: red;
color: white;
}
.external-editor-placeholder {
width: 100%;
height: 100%;
position: absolute;
left: 0;
padding-left: 8px;
padding-right: 8px;
padding-top: 2px;
background-color: magenta;
color: white;
}
</style>
<div class="grid-target"></div>
<section class="external-editor">
<h2 class="title">some custom external editor</h2>
<p>
<input type="text" id="username" placeholder="ok"></input>
</p>
</section>
<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/[email protected]"></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(
[
{ key: 'name', locked: 'left', width: 200 }
, {
key: 'username'
, components: [
gridComponents.createEditCell()
.component(createExternalEditor())
.on('change', onUsernameChange)
.on('validationerror', console.error.bind(console, 'VALIDATION'))
.on('editstart', d => console.debug('edit start', d))
.on('editend', d => console.debug('edit end', d))
.validate(validateUserName)
.editable(isNotZack)
, grid.updateTextIfChanged
]
}
, { key: 'email', sortDescending: true }
, { key: 'phone' }
]
)
, rows = _.range(1, 2000).map(faker.Helpers.createCard)
d3.select('.grid-target')
.style('height', '300px')
.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 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 isNotZack(cell) {
return cell.row.username.indexOf('Zac') !== 0
}
// On some other module somewhere, something like this is defined,
function createExternalEditor() {
const dispatch = d3.dispatch('partialedit', 'commit', 'cancel')
, api = d3Utils.rebind().from(dispatch, 'on')
function externalEditor(s) {
s.each(externalEditorEach)
}
return api(externalEditor)
function externalEditorEach(d, i) {
console.debug('externalEditorEach', d, i)
const target = d3.select(this).text('EDITING EXTERNALLY')
.classed('external-editor-placeholder', true)
, { column, row, isValidInput } = d
, form = d3.select('.external-editor')
.datum(d)
.classed('is-editing', true)
.classed('is-invalid', !isValidInput)
, title = form.select('.title')
.text(`edit ${column.id} currently «${column.value(row)}»`)
, input = form.select('[type=text]')
.property('value', d.tempInput || column.format(column.value(row)))
.on('input', onInput)
.on('change', commit)
.on('blur', commit)
.each(focus)
function onInput(d, i) {
dispatch.call('partialedit', this, d)
}
function focus() {
this.focus()
}
function commit() {
dispatch.call('commit', this, d)
form.classed('is-editing', false)
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment