Skip to content

Instantly share code, notes, and snippets.

@limscoder
Last active November 19, 2017 17:39
Show Gist options
  • Save limscoder/e60dc12ea509cc7aa825d0811d1af78d to your computer and use it in GitHub Desktop.
Save limscoder/e60dc12ea509cc7aa825d0811d1af78d to your computer and use it in GitHub Desktop.
D3 enter selection
// html:
// <ul>
// <li></li>
// <li></li>
// </ul>
const users = [{
id: 1,
name: 'Nancy',
role: 'person'
}, {
id: 2,
name: 'Bart',
role: 'cartoon'
}, {
id: 3,
name: 'Maggie',
role: 'cartoon'
}];
const enterUsers = d3.select('ul.app-user-list')
.selectAll('li.app-user')
.data(users, keyFunc)
.enter();
// prints 1
// Nancy and Bart match existing "li" elements,
// so they are not included in the enter set.
//
// Maggie does not match an existing "li" element,
// so there is one element in the enter set.
console.log(enterUsers.size());
// Add all "new" users to the document.
enterUsers.append('li')
.attr('class', 'app-user')
.attr('text', d => d.name)
.style('color', d => (d.role === 'human' ? 'purple' : 'yellow'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment