How can I convert this array
[
"sid",
"name",
"Last Name",
"email"
]
to this:
$('#example').DataTable( {
data: data,
columns: [
{ data: 'sid' },
{ data: 'name' },
{ data: 'Last Name' },
{ data: 'email' }
]
} );
<?PHP
//snippet from PHP query
$json = '[{"sid":"38","name":"Alex","Last Name":"hori","email":"noz@gmail.com"},{"sid":"59","name":"Andy","Last Name":"numa","email":"tommy@gmail.com"},{"sid":"40","name":"Ann","Last Name":"hama","email":"ohama@gmail.com"}]';
?>
<script type='text/javascript'>
var myList = <?= $json ?>;
var MyCols = {};
function addAllColumnHeaders(myList, selector) {
var columnSet = [];
var headerTr$ = $('<thead/>');
for (var i = 0;
i < myList.length;
i++) { var rowHash = myList[i];
for (var key in rowHash) {
if ($.inArray(key, columnSet) == -1) { columnSet.push(key);
headerTr$.append($('<th/>').html(key));
}
}
}
$(selector).append(headerTr$);
console.log("columnSet:");
console.log(columnSet);
return columnSet;
}
function buildHtmlTable(selector) {
var columns = addAllColumnHeaders(myList, selector);
MCols = JSON.stringify(columns);
console.log(MCols);
for (var i = 0; i < myList.length; i++) { var row$ = $('<tr/>');
for (var colIndex = 0;
colIndex < columns.length;
colIndex++) { var cellValue = myList[i][columns[colIndex]];
if (cellValue == null) cellValue = "";
row$.append($('<td/>').html(cellValue));
}
$(selector).append(row$);
}
}
</script>
This all works. But, I now want to use a more functional table setup, using datatables.net . However, they don't have the automatic headers, so I'm trying to export the ones in the format they expect; which is what started this question.const columns = columnSet.map(item => ({data:item}))
console.log(columns);
//the next line, the problem is that the actual page hasn't been loaded yet.
$('#example').DataTable( {
data: data,
columns: columns
} );
After the functions, I include the actual table-containing file:<?PHP
include_once "../apex12/dt-data-sources.html"
?>
which includes, among other things,<body onLoad="buildHtmlTable('#excelDataTable')">
<table id="excelDataTable" class="table table-striped table-bordered">
</table>
<table id="example" class="table table-striped table-bordered">
</table>
~~~~~~
<script src="app-assets/vendors/js/datatable/jquery.dataTables.min.js"></script>
<script src="app-assets/vendors/js/datatable/dataTables.bootstrap4.min.js"></script>
<script src="app-assets/js/data-tables/dt-data-sources.js"></script>
</body>
$('#example').DataTable( {
data: data,
columns: [
{ data: 'sid' },
{ data: 'name' },
{ data: 'Last Name' },
{ data: 'email' }
]
} );
So, I was thinking of replacing that with MyCols so that I could put it in place. So far I've tried:MyCols = ( {
data: data,
columns: columns
} );
but that generates an error data is not defined.const columns = columnSet.map(item => ({data:item}))
$('#example').DataTable( {
data: data,
columns: columns
} );
function buildHtmlTable(selector) {
....
// here
}
$(".javascript-sourced").DataTable({
data: myList,
columns: [{
title: "sid"
},
{
title: "name"
},
{
title: "Last Name"
},
{
title: "email"
}
]
});
const arr = [
"sid",
"name",
"Last Name",
"email"
]
const myList = [{"sid":"38","name":"Alex","Last Name":"hori","email":"noz@gmail.com"},{"sid":"59","name":"Andy","Last Name":"numa","email":"tommy@gmail.com"},{"sid":"40","name":"Ann","Last Name":"hama","email":"ohama@gmail.com"}]
const columns = arr.map(item => ({
data: item
}))
console.log(columns)
$('#example').DataTable({
data: myList,
columns: columns
});
$(".javascript-sourced").DataTable({
data: myList,
columns: [{
title: "sid"
},
{
title: "name"
},
{
title: "Last Name"
},
{
title: "email"
}
]
});
$('#example').DataTable( {
data: myList,
columns: [{title: 'sid'},{data:'sid'},{data:'name'},{title: 'name'},{data:'Last Name'},{title: 'Last Name'},{data:'email'},{title: 'email'}]
} );
at this point I'm asking their forum. The main reason for using datatables.net is that it has sorting and export features.$("#example").html(
`<thead><tr>
${Object.keys(myList[0]).map(key => `<th>${key}</th>`)}
</tr></thead>`);
#example thead th { text-transform: capitalize; }
however, I think I have to do the $.document thing later, as the page, at this point, isn't generated.
I created a global variable
Open in new window
can I put the results into that and use the $.document later.sorry for being so dense. I'm currently learning python and never improved my javascript skills.