I have an issues with datatable sorting and filter basically all the JS function not working. I've already include the JS files. some details: I'm connecting to the database to retrieve data in json. The php file:('district.php')
<?php
include_once('db.php');
$db = mysql_select_db('mor_app',$con);
if(!$db){ die('Database selection failed '.mysql_error()); }
$sql = 'SELECT *FROM users';
$result = mysql_query($sql,$con);
$data = array();
while($row = mysql_fetch_array($result)){
$row_data = array(
'id' => $row['id'],
'date' => $row['date'],
'username' => $row['username'],
'Q1' => $row['Q1'] );
array_push($data, $row_data); }
echo json_encode($data);
?>
the html file:
<!DOCTYPE html>
<html>
<head>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables/1.10.0/css/jquery.dataTables.css">
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables/1.10.0/js/jquery.dataTables.js"></script>
<script src="js/jquery-1.9.1.min.js"></script>
</head>
<body>
<table id="table_id" class="display">
<caption>Try</caption>
<thead> <tr>
<th>ID</th>
<th>Date</th>
<th>user name</th>
<th>Q1</th> </tr>
</thead>
<tbody id="tablebody">
</tbody>
</table>
<script>
$(function() {
var url = 'district.php';
$.getJSON(url, function(data) {
$.each(data, function(index, data) {
$('#tablebody').append('<tr>');
$('#tablebody').append('<td>'+data.id+'</td>');
$('#tablebody').append('<td>'+data.date+'</td>');
$('#tablebody').append('<td>'+data.username+'</td>');
$('#tablebody').append('<td>'+data.Q1+'</td>');
$('#tablebody').append('</tr>');
});
});
});
$('#table_id').DataTable();
</script>
</body>
</html>
now I can see the table but I just can't sorting or searching. if I write something in the search box the table is empty. the same happen with sorting. it say: No data available in table. I thought it's happening because I'm retrieving data. any suggestion? Thank a lot, Mor
I have an issues with datatable sorting and filter basically all the JS function not working. I've already include the JS files. some details: I'm connecting to the database to retrieve data in json. The php file:('district.php')
<?php
include_once('db.php');
$db = mysql_select_db('mor_app',$con);
if(!$db){ die('Database selection failed '.mysql_error()); }
$sql = 'SELECT *FROM users';
$result = mysql_query($sql,$con);
$data = array();
while($row = mysql_fetch_array($result)){
$row_data = array(
'id' => $row['id'],
'date' => $row['date'],
'username' => $row['username'],
'Q1' => $row['Q1'] );
array_push($data, $row_data); }
echo json_encode($data);
?>
the html file:
<!DOCTYPE html>
<html>
<head>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="//cdn.datatables/1.10.0/css/jquery.dataTables.css">
<!-- DataTables -->
<script type="text/javascript" charset="utf8" src="//cdn.datatables/1.10.0/js/jquery.dataTables.js"></script>
<script src="js/jquery-1.9.1.min.js"></script>
</head>
<body>
<table id="table_id" class="display">
<caption>Try</caption>
<thead> <tr>
<th>ID</th>
<th>Date</th>
<th>user name</th>
<th>Q1</th> </tr>
</thead>
<tbody id="tablebody">
</tbody>
</table>
<script>
$(function() {
var url = 'district.php';
$.getJSON(url, function(data) {
$.each(data, function(index, data) {
$('#tablebody').append('<tr>');
$('#tablebody').append('<td>'+data.id+'</td>');
$('#tablebody').append('<td>'+data.date+'</td>');
$('#tablebody').append('<td>'+data.username+'</td>');
$('#tablebody').append('<td>'+data.Q1+'</td>');
$('#tablebody').append('</tr>');
});
});
});
$('#table_id').DataTable();
</script>
</body>
</html>
now I can see the table but I just can't sorting or searching. if I write something in the search box the table is empty. the same happen with sorting. it say: No data available in table. I thought it's happening because I'm retrieving data. any suggestion? Thank a lot, Mor
Share Improve this question asked Jul 4, 2014 at 22:13 MorMor 2293 gold badges6 silver badges14 bronze badges3 Answers
Reset to default 1The problem I found is, you have to use DataTable's fnAddData
function to add data in your HTML table. Otherwise, it is not adding your data which is being fetched on the fly. I tested with hard coded data in HTML which works fine, but whenever I tried to get data from other source through AJAX, it shows "no data found" or something.
I found the solution in another stack overflow question .
I used AJAX for your solution, but the main catch is to use fnAddData
.
Here is the solution (took help from above link) which works fine:
<script>
$('#table_id').DataTable();
$(document).ready(function() {
$.ajax({
type: "POST",
url: "district.php",
data: {data : ""},
cache: false,
success: function(result){
data = JSON.parse(result);
$.each(data, function(index, data) {
//!!!--Here is the main catch------>fnAddData
$('#table_id').dataTable().fnAddData( [
data.id,
data.date,
data.username,
data.Q1 ]
);
});
}
});
});
</script>
IMPORTANT: My answer considers that your PHP code is correctly returning data. I made some dummy array as suggested in the ments. My php file returns like this:
$data = array();
$row_data = array( 'id' => 1, 'date' => "2014-01-01", 'username' => "mou", 'Q1' => "muhahaha" );
array_push($data, $row_data);
$row_data = array( 'id' => 9, 'date' => "2013-02-02", 'username' => "sadi", 'Q1' => "hii" );
array_push($data, $row_data);
echo json_encode($data);
You need to load jQuery BEFORE you load the datatables jQuery plugin:
<script src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables/1.10.0/js/jquery.dataTables.js"></script>
add this statement to datatable definition on javascript
$('#table_id').DataTable({"sAjaxDataProp":"",});