Using JavaScript. So, I have set this up in two ways. One way everything works perfectly and the other will not, despite the fact that I can see no difference between them.
I'm displaying tables after user selection on a map. Each new selection populates a new accordion button that should be opened and closed. The tables are set up after an Ajax call to GeoServer so all of this must be dynamic (like the solution that works but the button's do not close). There is a count attached to it so that new panels/table containers/tables are created each time.
Scenario 1) I hard coded the html in which works fine. Like this:
<div class="container" id="accordionContainer">
<button class="accordion">Selection 1</button>
<div class="panel" id="panel1">
<div class="container" id="tableContainer">
<table id="featureTable" class="display" width="100%">
<thead></thead><tfoot></tfoot>
</table>
</div>
</div>
</div>
I set up 6 or 7 of those in the body section (head section works as well) and hard coded the count number (like panel1 above). Works great. User selects a map area and the features are displayed in an accordion button. I can't have hard coded anything though. There needs to be no limit on how many selections/tables.
Which brings me to:
Scenario 2) Everything is set up dynamically after each Ajax request. This works, but the buttons do not close. They are open and display all the tables.
var btnPart = '<button class="accordion">' + 'Selection ' + tableCount.toString() + ' ' + activeOverlay+ '</button>';
var panelPart = '<div class="panel" id="panel' + panelCount.toString() + '">';
var tabContPart = '<div class="container" id="tableContainer' + tableContainerCount.toString() +'">';
var tablePart = '<table id="featureTable' + tableCount.toString() + '" class="display" width="100%"><thead></thead><tfoot></tfoot></table></div></div></div>';
$('#accordionContainer').append(btnPart, panelPart, tabContPart, tablePart);
var newTable = '#featureTable' + tableCount.toString();
$(newTable).DataTable( {
data: data.features, //get the data under features
columns: columns,
destroy: true
} );
I cannot see a difference between hard coding the DOM ahead of time or setting it up on the fly that would cause the buttons to lose their open/close functionality.
UPDATED CODE AS REQUESTED:
<body>
<div id="map"></div>
<div id="sidebar"></div>
<div class="container" id="accordionContainer"></div>
</body>
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
/* Toggle between adding and removing the "active" class,
to highlight the button that controls the panel */
this.classList.toggle("active");
/* Toggle between hiding and showing the active panel */
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
}
}
var btnPart = '<button class="accordion">' + 'Selection ' + tableCount.toString() + ' ' + activeOverlay+ '</button>';
var panelPart = '<div class="panel" id="panel' + panelCount.toString() + '">';
var tabContPart = '<div class="container" id="tableContainer' + tableContainerCount.toString() +'">';
var tablePart = '<table id="featureTable' + tableCount.toString() + '" class="display" width="100%"><thead></thead> <tfoot></tfoot></table></div></div></div>';
$('#accordionContainer').append(btnPart, panelPart, tabContPart, tablePart);
var newTable = '#featureTable' + tableCount.toString();
$(newTable).DataTable( {
data: data.features, //get the data under features
columns: columns,
destroy: true
} );
</script>