Datatables, refresh and fancybox
Datatables, refresh and fancybox
Hey,
I have been using datatables for the last few weeks. What I am now trying to do is load the data from a mySQL database at runtime and have all the filtering, pagination done on the client machine rather than having to keep contacting the server. I use a fancybox plugin which opens up when a row in a table is clicked. The user can then edit the row of data within the fancy box using an iframe. When the user closes the iframe I want the table to destroy itself then reload itself by downloading the rows of data from mySQL without reloading the whole page. I've seen several topics on the forum regarding this and they all point to the same api called fnReloadAjax() . I've also found a blog dictating exactly what I want here http://www.mccran.co.uk/index.cfm/2011/5/19/JQuery-datatable-with-added-FancyBox-and-auto-refresh
Im just really unsure in how I can get the data from mySQL and put it into the table using the json object... I have tried to use the script on the datatables website for php but keep getting this error 'DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error'.
I don't really want to use server side processing, but im not sure how else I can refresh the table without having to refresh the page.
Im pretty sure I am getting close, any help would be appreciated.
Cheers,
Sam
I have been using datatables for the last few weeks. What I am now trying to do is load the data from a mySQL database at runtime and have all the filtering, pagination done on the client machine rather than having to keep contacting the server. I use a fancybox plugin which opens up when a row in a table is clicked. The user can then edit the row of data within the fancy box using an iframe. When the user closes the iframe I want the table to destroy itself then reload itself by downloading the rows of data from mySQL without reloading the whole page. I've seen several topics on the forum regarding this and they all point to the same api called fnReloadAjax() . I've also found a blog dictating exactly what I want here http://www.mccran.co.uk/index.cfm/2011/5/19/JQuery-datatable-with-added-FancyBox-and-auto-refresh
Im just really unsure in how I can get the data from mySQL and put it into the table using the json object... I have tried to use the script on the datatables website for php but keep getting this error 'DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error'.
I don't really want to use server side processing, but im not sure how else I can refresh the table without having to refresh the page.
Im pretty sure I am getting close, any help would be appreciated.
Cheers,
Sam
This discussion has been closed.
Replies
Unknown column 'SQL_CALC_FOUND_ROWS' in 'field list'
Bit confused
[code]my $CallsArray = $dbh->selectall_arrayref( "SELECT IDnumber, confirmation, lastChange, TIMEDIFF(NOW(),lastChange) AS 'ago...', currState, sizeSeconds, sizeBytes, CIDnumber, DID FROM Recordings ORDER BY lastChange DESC LIMIT 200" );
return $self->render_json({ aaData => $CallsArray }) if $CallsArray; [/code]
And it is all basically automatic since I specify the fields and names in the SQL. Just giving it a plug since I owe it my sanity.
OK, so where does that error happen? In PHP (an error log file) or in js (ie: in the browser). Sounds like a typo somewhere from the little you have posted. Post MORE details - in particular the part that specifies the SQL query (would be in PHP I guess) and the part that displays the table (in javascript).
[code]
<?php
/*
* Script: DataTables server-side script for PHP and MySQL
* Copyright: 2010 - Allan Jardine
* License: GPL v2 or BSD (3-point)
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Easy set variables
*/
/* Array of database columns which should be read and sent back to DataTables. Use a space where
* you want to insert a non-database field (for example a counter or static image)
*/
$aColumns = array( 'engine', 'browser', 'platform', 'version', 'grade' );
/* Indexed column (used for fast and accurate table cardinality) */
$sIndexColumn = "id";
/* DB table to use */
$sTable = "ajax";
/*REMOVED DATABASE DETAILS*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP server-side, there is
* no need to edit below this line
*/
/*
* MySQL connection
*/
$gaSql['link'] = mysql_pconnect( $gaSql['server'], $gaSql['user'], $gaSql['password'] ) or
die( 'Could not open connection to server' );
mysql_select_db( $gaSql['db'], $gaSql['link'] ) or
die( 'Could not select database '. $gaSql['db'] );
/*
* Paging
*/
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
$sLimit = "LIMIT ".mysql_real_escape_string( $_GET['iDisplayStart'] ).", ".
mysql_real_escape_string( $_GET['iDisplayLength'] );
}
/*
* Ordering
*/
if ( isset( $_GET['iSortCol_0'] ) )
{
$sOrder = "ORDER BY ";
for ( $i=0 ; $i
this only updates the client side. so you want to also send the data to a server side script that also updates the server.
What version of MySQL are you using? SQL_CALC_FOUND_ROWS is a MySQL instruction which will cause the database to calculate the number of rows that would have been returned if there wasn't a "LIMIT" on the SQL - this is an optimisation to try and make the script run faster.
Allan
you will have to take out the SQL_CALC_FOUND_ROWS and query to get that count
replace lines 106-116 with
[code]
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))." // took out SQL_CALC_FOUND_ROWS
FROM $sTable
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
/* Data set length after filtering */
$sQuery = "
SELECT count($sIndexColumn)
FROM $sTable
$sWhere
";
$rFilteredTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$iFilteredTotal= mysql_num_rows($rFilteredTotal) or die(mysql_error());
[/code]
Just trying to figure out why this is, the database table I have in mySQL was created using the demo version from this site and all the fields are there. Perhaps it could be a problem with my ancient version of mysql
also note that I left out " FROM $sTable" in my intial post (line 17 of the snippet of code I posted)
{"sEcho": 0, "iTotalRecords": 57, "iTotalDisplayRecords": , "aaData": [ ["Trident","Internet Explorer 4.0","Win 95+","4","X"],["Trident","Internet Explorer 5.0","Win 95+","5","C"]
Unfortunately the "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error." dialog box error still appears.
I must be getting very close to fixing this as the response is now retrieving the data from the database.
Thank you for your patience by the way, its good to finally see some light at the end of the tunnel with your help after spending hours trying to understand the problem
I left out " FROM $sTable" in my intial post (line 17 of the snippet of code I posted)
for bServerSide sourced data, just call fnDraw http://www.datatables.net/ref#fnDraw or http://www.datatables.net/api#fnDraw , and you might want to call fnDraw(false) to keep it from re-sorting and moving back go page 1.
the fnDraw will trigger the fnServerData, calling your server side script via AJAX.
In my table i now create table with the following fields: ID, Name, Manager, Client.
I would like another column after client which displays an image of a tick if the record is complete or a cross if it is complete. I have the code for this and it reads no problem, to decide which image to use I usually query the mysql table for a column called Complete which displays either the values On or Off based upon whether to display an image of a tick or cross.
I need to query this column in the php server script for my if statement to decide on which picture to use.
The problem is i do not want this column which contains the values on or off to display in my table, I just want to query it and then display an image which i can do.
How can i query the column Clients from my mysql database without having the column values displayed in my JSON table? is there anyway to hide this column.
Sam
Since you need a column in order to display the image, you might not want the column to be invisible, but just might want to render that column as an image based on the value in it. http://www.datatables.net/ref#fnRender
(and you can disable sorting on that column with bSortable http://www.datatables.net/ref#bSortable )
"bStateSave": true,
"bServerSide": true,
'aoColumnDefs': [{'aTargets': [6,7,8,9,10], 'bSortable': false},
{'aTargets': [ 3 ], 'bVisible': false}
],
'sPaginationType': 'full_numbers',
bVisible has no effect?
I'm having the same as problem as you did, I mean I'm getting JSON code when I called the script, but unfortunately the "DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error." dialog box error still appears.
May you tell me how you fixed that problem???? or can anyone help me?
Thanks!
Allan