DataTables and MySQL
DataTables and MySQL
Zwall33
Posts: 4Questions: 1Answers: 0
Hi everybody !
I have a bit of a problem, I'm trying to get my fields fill with datas from a mysql database. But it seems like I'm doing it wrong because I have nothing in my spreadsheet and no error at all. I hope you can see what I missed, it's been 2 days of trying but nothing works for me...
Here is the code;
1st the node api:
static getDefautLEA(){
let sql = "SELECT * FROM Defaut ORDER BY Frequence DESC LIMIT 1";
return sql;
}
router.get("/defautLEA", (req, res, next) => {
db.query(Request.getDefautLEA(), (err, row)=> {
if(!err) {
res.send(row);
}
});
});
I'm routing the result of my query to my JS spreadsheet.
The JS spreadsheet:
setInterval(function(){
$(document).ready(function(){
var data;
$(function(){
$.get('/dbIndex/defautLEA',{},function(row){
data = [
{
"Défaut": row[0].Defaut,
"Présence": row[0].Présence,
"Dernière présence": row[0].LastTime,
"Nombre par jour": row[0].Frequence
}
]
});
if ( $.fn.dataTable.isDataTable( '#defaut_table' ) ) {
table = $('#defaut_table').DataTable();
}
else {
table = $('#defaut_table').DataTable({
paging: false,
retrieve: true,
data: data,
columns: [
{ data: 'Défaut' },
{ data: 'Présence' },
{ data: 'Dernière présence' },
{ data: 'Nombre par jour' }
]
});
}
});
});
},1000);
And the HTML:
<table id="defaut_table" class="display" style="width:100%">
<thead>
<tr>
<th>Défaut</th>
<th>Présence</th>
<th>Dernière présence</th>
<th>Nombre par jour</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Défaut</th>
<th>Présence</th>
<th>Dernière présence</th>
<th>Nombre par jour</th>
</tr>
</tfoot>
</table>
This discussion has been closed.
Answers
Found solution, bad scope... All need to be in $.get('/dbIndex/defautLEA',{},function(row){});
Okay new problem, can't it be dynamic ? Automaticly update every 1 second in my case. Is there something I missed ?
It can be - you need to use the
row().data()
method to update the data for a row as the new data comes in. Or if you are getting the whole data set again, then useclear()
and thenrows.add()
.Allan
Thx Allan ! I'm going to try that !