Last active
November 19, 2017 17:39
-
-
Save limscoder/e60dc12ea509cc7aa825d0811d1af78d to your computer and use it in GitHub Desktop.
D3 enter selection
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
// 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