I have json data that's being imported to a page where I am trying to create a datatable.
the console.log(jsonReturn)
:
{"1":{"dateReceived":"1905-07-04","cost":"2.7200","number":"757991"},"2":{"dateReceived":"1905-07-04","cost":"2.7200","number":"757991"}}
Below is my var jsonReturn
:
Array
(
[1] => Array
(
[dateReceived] => 1905-07-04
[cost] => 2.7200
[number] => 757991
)
[2] => Array
(
[dateReceived] => 1905-07-04
[cost] => 2.7200
[number] => 757991
)
[3] => Array
(
[dateReceived] => 1905-07-04
[cost] => 2.7200
[number] => 757991
)
)
I've read through every example on the DataTables website and I can't find anything on how to utilize this variable to fill my table. Apart from initializing the table:
$(document).ready(function(){
var table = $('#ltc-table').dataTable( {
"aaData" : jsonReturn,
});
Setting aaData
to jsonReturn
loads something into my table, but it's just gibberish. I need to know how to tell DataTables exactly where to find the data I want it to use to populate the table, and which rows I want to display where, etc.
I arrive at this javascript var by doing the following:
The user searches with a text-box, which goes off to an ajax call.
$.ajax({
type: 'POST',
url: 'quoteManagerSearch.php',
data: {searchBy:searchBy, searchValue:searchValue, options:options},
success:function(data){
jsonReturn = data;
$("#quoteReturn").load("return.php");
//$("#quoteReturn").html(data);
}
}); //close ajax call
$searchQuery = "SELECT * FROM alldata where $searchBy like '%$searchValue%'" ;
$searchResult = mysqli_query($con, $searchQuery);
$x = 0;
while ($row = mysqli_fetch_row($searchResult)) {
$x = $x + 1;
$output[$x]["dateReceived"] = convertDate($row[1]);
$output[$x]["cost"] = $row[15];
$output[$x]["number"] = $row[16];
}
echo json_encode($output);
on success, return.php
is loaded into a div, and var jsonReturn
is created.
Then, in return.php
I try to use my invalid object.
Update
In response to @davidkonrad's post:
I'm getting the following error now:
DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.
From what I can see (minus the quotation marks,) my JSON string looks exactly like yours.
{"aaData":[
{"dateReceived":"1905-07-04","cost":"2.7200","number":"757991"},
{"dateReceived":"1905-07-04","cost":"2.9200","number":"337995"}
]
}
I also changed DataTable
to dataTable
since I'm using 1.9.4 for this test.
Update #2
I figured out what's going wrong!
The process, as I saw it, is as follows:
User loads quoteManager.php, and enters options in a textbox, then hits
search
. This value is converted into a javascript var, and sent off in this ajax call:$.ajax({ type: 'POST', url: 'quoteManagerSearch.php', data: {searchBy:searchBy, searchValue:searchValue, options:options}, success:function(data){ jsonReturn = data; $("#quoteReturn").load("quoteManagerReturn.php"); }
}); //close ajax call
quoteManagerSearch.php is the actual query against the database. This outputs a valid JSON string.
in quoteManager.php
, there is a <div>
at the bottom of the page for this response to load into:
<div id="quoteReturn">
</div>
I do something wrong here.
quoteManagerReturn.php
is loaded into the#quoteReturn
div on thequoteManager.php
page - but without any of the data thatquoteManagerSearch.php
returned, becausequoteManagerReturn.php
is running its own ajax toquoteManagerSearch.php
- not using the one I ran in the beginning, and this string,$searchBy = $_POST['searchBy'];
is empty - so I get no results. This is why it's telling me my JSON is invalid, because it's empty!What I need to do (which I'm not sure how to do...)
- Use
jsonReturn
inquoteManagerReturn.php
- as this is the valid JSON response I want, instead of making a new ajax request toquoteManagerSearch.php
, or - Pass these variables over from
quoteManager.php
toquoteManagerReturn.php
and send them in the ajax request I'm making fromquoteManagerReturn.php
toquoteManagerSearch.php
.
- Use
Update 2++
I sorted this out by just adding the variables into my ajax call on the second page:
var table = $('#ltc-table').DataTable( {
ajax : {
url : 'quoteManagerSearch.php' ,
dataSrc : 'aaData' ,
type : 'POST',
data: {searchBy:searchBy, searchValue:searchValue, options:options},
},
I've taken the original ajax call out of my first page (since there's no reason for the duplication...) I believe this is the solution!
{ "aaData": [] }` I'm not sure if this is just an error in the debugger, as in Chrome's debugger I clearly see the proper JSON response being output by `quoteManagerSearch.php` – Brian Powell May 27 '15 at 14:41