Skip to content

Instantly share code, notes, and snippets.

@rattrayalex
Created July 10, 2016 07:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rattrayalex/8450e2378465e85ed846837a5d0114a6 to your computer and use it in GitHub Desktop.
Save rattrayalex/8450e2378465e85ed846837a5d0114a6 to your computer and use it in GitHub Desktop.
const getIndex = (th) => Array.from(th.parentElement.children).indexOf(th)
const getChildren = (i) => Array.from(document.querySelectorAll(`td:nth-child(${i + 1})`))
const setHiddenStyle = (node) => {
node.style.width = '1px'
node.style.overflow = 'hidden'
node.style.overflowWrap = 'normal'
}
const toggleHidden = (node) => {
if (node.style.length) {
node.style = {}
} else {
setHiddenStyle(node)
}
}
const handleClick = (e) => {
const i = getIndex(e.currentTarget)
getChildren(i).concat([e.currentTarget]).forEach(toggleHidden)
}
document.querySelectorAll('th').forEach(th => {
th.onclick = handleClick;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment