Reputation: 23
How do I go about splitting whole columns into sections:
What I want to achieve in the end is to show/hide the column section based on a button using javascript.
The code:
<table>
<tr>
<th>Number</th> <!-- This column is meant to be shown -->
<th>Shown Content</th> <!-- This column is meant to be shown -->
<th>Hidden Content</th> <!-- This column is meant to be hidden -->
<th>Action</th> <!-- This column is meant to be shown -->
</tr>
<tr>
<td>1</td> <!-- This column is meant to be shown -->
<td>Shown</td> <!-- This column is meant to be shown -->
<td>Hidden</td> <!-- This column is meant to be hidden -->
<td><form action='action'><button>VIEW</button></form></td> <!-- This column is meant to be shown -->
</tr>
</table>
Upvotes: 0
Views: 211
Reputation: 42095
Explained:
nth-child
you're able to select those header and data elements of the supplied column numbercol
), iterate over them and either add or remove the class. CSS is responsible for hidingCaveats:
This code is heavily reliant on ES6, it can be converted, but you should be especially careful of:
=>
)`${variable}`
)[...obj]
function toggle() {
let className = 'hidden';
[...arguments].forEach(num => {
var selector = `th:nth-child(${num}), td:nth-child(${num})`,
col = document.querySelectorAll(selector),
add = !col[0].classList.contains(className);
col.forEach(td => {
add ? td.classList.add(className) : td.classList.remove(className);
});
});
}
.hidden {
display: none;
}
<table>
<tr>
<th>Number</th>
<!-- This column is meant to be shown -->
<th>Shown Content</th>
<!-- This column is meant to be shown -->
<th>Hidden Content</th>
<!-- This column is meant to be hidden -->
<th>Action</th>
<!-- This column is meant to be shown -->
</tr>
<tr>
<td>1</td>
<!-- This column is meant to be shown -->
<td>Shown</td>
<!-- This column is meant to be shown -->
<td>Hidden</td>
<!-- This column is meant to be hidden -->
<td>
<form action='action'><button>VIEW</button></form>
</td>
<!-- This column is meant to be shown -->
</tr>
</table>
<button onclick="toggle(1,3)">Toggle</button>
Upvotes: 2
Reputation: 1175
this to show/hide a column in a specific row
function toggle(){
var header = document.getElementById('headerHide');
var parent = document.getElementById('first');
var sub = parent.querySelector('#hide');
var btn = parent.querySelector('#btn');
if(sub.style.display == 'none'){
sub.style.display = 'block';
header.style.display = 'block';
btn.innerHTML = "HIDE";
}
else{
sub.style.display = 'none';
header.style.display = 'none';
btn.innerHTML = "VIEW";
}
}
<table>
<tr>
<th>Number</th> <!-- This column is meant to be shown -->
<th id="headerShow">Shown Content</th> <!-- This column is meant to be shown -->
<th id="headerHide" style="display:none;">Hidden Content</th> <!-- This column is meant to be hidden -->
<th>Action</th> <!-- This column is meant to be shown -->
</tr>
<tr id="first">
<td>1</td> <!-- This column is meant to be shown -->
<td id="show">Shown</td> <!-- This column is meant to be shown -->
<td id="hide" style="display:none;">Hidden</td> <!-- This column is meant to be hidden -->
<td><form action='something' style='display:inline'><button onclick="toggle()" id="btn">VIEW</button></form></td> <!-- This column is meant to be shown -->
</tr>
</table>
Upvotes: 0
Reputation: 2357
One line of jQuery
$("#hide").on("click", function() {
//hide third column with header, you can add more columns if you wish
$('td:nth-child(3),th:nth-child(3)').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Number</th>
<!-- This column is meant to be shown -->
<th>Shown Content</th>
<!-- This column is meant to be shown -->
<th>Hidden Content</th>
<!-- This column is meant to be hidden -->
<th>Action</th>
<!-- This column is meant to be shown -->
</tr>
<tr>
<td>1</td>
<!-- This column is meant to be shown -->
<td>Shown</td>
<!-- This column is meant to be shown -->
<td>Hidden</td>
<!-- This column is meant to be hidden -->
<td>
<form action='action'><button>VIEW</button></form>
</td>
<!-- This column is meant to be shown -->
</tr>
</table>
<button id="hide">Hide Third Column</button>
Upvotes: 0