Crear otra datatable a partir de tabla ya creada
Crear otra datatable a partir de tabla ya creada
Tengo una tabla creada con datatable, quiero que al hacer click en un boton o el nombre del campo de productos me renderize otra tabla nueva con solamente los modelos de ese producto. Lo intenté con html .createlemnt y javascript pero no logro conseguir lo que quiero, al ser datatables se me complica por que no es lo mismo que renderizar tablas con html puro. Mi código es el siguiente:
HTML:
<table id="sellersQuantityTable" class="table table-bordered table-striped table-sm">
<thead class="text-center">
<tr>
<td>MARCA</td>
<td>MODELO</td>
<td>CANTIDAD</td>
</tr>
</thead>
<tbody class="text-center">
</tbody>
</table>
Javascript:
const tableRanking = $("#sellersQuantityTable").DataTable({
dom: 'Pfrtip',
responsive: true, "lengthChange": false, "autoWidth": false, "searching": true, fixedHeader: true, "scrollX": true,
buttons: [
{
extend: "excel",
title: '',
filename: 'Venta por Financiera',
exportOptions: { orthogonal: 'export' },
}],
language: {
"url": "//cdn.datatables.net/plug-ins/1.10.24/i18n/Spanish.json"
},
columns: [
{ "data": "MARCA" },
{ "data": "MODELO" },
{
"data": "Monto", render: function (data, type, row) {
return type === 'export' ?
data.replaceAll(/[$ |.]/g, '').replace(',', '.') :
data;
}
},
],
order: [[1, "desc"]],
rowsGroup: [0]
});
Esta es mi tabla:
Lo que quiero lograr es que al hacer click en el campo de producto me cree una nueva tabla con solamente el modelo de ese producto y la cantidad
Busqué en google y todos los lugares que conozco pero no encontré nada parecido. Se me hace algo complicado a mi parecer- Muchas gracias.
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
This question has an accepted answers - jump to answer
Answers
This example shows how to create click event handlers and get the row data of the clicked row. The data can then be used to create an HTML table or Datatable.
Kevin
Thanks Kevin. It works but i dont know how create other table. It can be used HTML createElement in this DataTable?
Or how would you?
Do you want to create a new HTML table on the same page or a new Datatalbe on the same page?
You can search for tutorials to use Javascript to dynamically create new tables.
This example shows how to create a Datatable with Javascript data, data from the click event for example. Click the HTML tab and you will see that all Datatables needs is a
table
element and the example usescolumns.title
to create the headers.Kevin
Thanks!
With this I can get the data in a json from where I click, but I can't send it to the new table. What am I doing wrong?
My new table:
In HTML:
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
Use the triple back ticks as on new lines as described below the
Post Comment
button to format blocks of code.Looks like you are initializing the Datatable before you want to add the data. So instead of using
data
to load the data you can removedata: dataNew,
and initialize an empty Datatable.In the click event you can use
clear()
followed byrow.add()
to clear then add the rows to the Datatable. Something like this:You will need to define
columns.data
to match your source data (newData) data structure.Kevin