I have my table in html, and through javascript and a button, I can add as many rows as I want, when the row is created, how can I make it in the action column where a button with an "X" is created, when pressing it has a sub row that expands and collapses, like the jquery examples https://datatables.net/examples/server_side/row_details.html , but without using it
function agregarFila() {
document.getElementById("example").insertRow(-1).innerHTML = '<td><button type="button" class="btn btn-success" onclick="expandir()">+</button></td><td contenteditable="true">x</td><td contenteditable="true">x</td><td contenteditable="true">x</td><td contenteditable="true">x</td></td>';
}
function expandir() {
//subfila con textfield, label etc
}
<body>
<div class="row">
<div class="col-md-12 ml-auto">
<button id="enviar" type="button" class="btn btn-success" onclick="agregarFila()" style="float:right">Agregar Direccion</button>
</div>
</div>
<br>
<table id="example" class="table table-striped table-bordered table-responsive">
<thead>
<tr>
<th style="width:10px;">Acciones</th>
<th style="width:10px;">Calle</th>
<th style="width:10px;">Colonia</th>
<th style="width:10px;">Numero Exterior</th>
<th style="width:10px;">Numero Interior</th>
</tr>
</thead>
<tbody id="example">
<tr>
</tr>
</tbody>
</table>
<script src="../Js/table.js"></script>
</body>
What you need is to capture the element to change or expand, in this case the button, I have placed an id to the button to then expand it via CSS. Your code would look like this:
And the Javascript would look like this:
Notice that I used onclick to activate the expand function, and ondblclick, so that when you double click it collapses.
First, some recommendations on good practices:
document.querySelector('#example tbody");
.innerHTML
to create or update content, because on each execution the browser has to re-render the entire object and its children. There are better options, like .insertAdjacentHTML() , but I'll leave that for you to investigate and improve your code.Now yes, let's go with the answer:
active
, which is what makes it show or hide