Skip to content
Advertisement

Add/Remove Table Columns in JavaScript Based On Checkbox Status

In the following DEMO if you paste the list in the search box:

00001, 00002, 00003, 00004, 00005, 00006, 00007, 00008, 00009, 00010, 00011, 00012, 00013

It will pull the respective property features from a JSON file that is located here: https://api.myjson.com/bins/f2nos

JavaScript
JavaScript

The checkboxes control whether or not the table columns are visible.

So if you uncheck checkbox CODE, CODE column will disappear

One small problem.

When I uncheck any checkboxes BEFORE the search, for example CODE, and then search I get this weird table: enter image description here

The reason that is happening is because even though the checkbox is unchecked the APPEND() Statement still appends the CODE column.

So how to connect the checkbox status to the append statement so that columns don’t show up even after the search?

I’m assuming the solution is to turn every Table TD into a variable and somehow connect it to the status of the checkbox?

How to achieve this? Or better solutions?

Advertisement

Answer

“So how to connect the checkbox status to the append statement so that columns don’t show up even after the search?”

“I’m assuming the solution is to turn every Table TD into a variable and somehow connect it to the status of the checkbox?”

“How to achieve this? Or better solutions?”

You are correct, it is the way the rows are appended to the table without checking if any of the checkboxes were unchecked. The following are the changes made:

  • All checkboxes have only one class: .theader

  • All <td> have the #ids removed, it’s invalid HTML to have duplicated #ids plus it served no purpose anyhow.

  • Below is the solution to the issue concerning column generation:

JavaScript

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement