tigrang / cakephp-datatable

JQuery DataTable plugin server-side processing component for CakePHP
47 stars 29 forks source link

JSON formatting error #28

Closed Mehrdad-Dadkhah closed 11 years ago

Mehrdad-Dadkhah commented 11 years ago

hi thansk for plugin ...

but i have this js error: DataTables warning (table id = 'DataTables_Table_0'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.

luisillo26 commented 11 years ago

Check the response of the request that datatables is doing to cake, you can check it with firebug, you must getting html instead of json

joshdcid commented 11 years ago

Hey @luisillo26 ..I have the same error, where do i check that in cake? Thanks.. Screen Shot 2013-04-15 at 11 13 05 AM

luisillo26 commented 11 years ago

You have to check the response of the datatables request.

If you use Firebug or Chrome Inspector, you can view it in the network tab.

If Cake is triggering errors (only if you have your debug config higher than 0), surely cake's response is an HTML with the description of the error so you will know what to do to fix it.

joshdcid commented 11 years ago

Thanks.. i got my error Array to string conversion [CORE/Cake/Model/Datasource/DboSource.php, line 461] Database Error

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'customSearch' at line 1

luisillo26 commented 11 years ago

Sweet!! i'm glad you found it.

Saludos!!

tigrang commented 11 years ago

Thanks for helping out @luisillo26

@joshdcid Were you able to get it all working?

joshdcid commented 11 years ago

Yes! Thanks.. i been looking for a way to have inside a row in datatables edit, view and delete.. how you came across with that problem?

But everything else is working.. thanks..

tigrang commented 11 years ago

In your columns, setup a non-table column (either by 'Actions' => null or 'actions' => array(..some options.., 'useField' => false).

Then in your view:

foreach($dtResults as $result) {
    $this->dtResponse['aaData'][] = array(
       $result['Model']['field_a'],
       $this->Html->link('View', array('action' => 'view', $result['Model']['id'])) . .. the rest of your links for the actions
    );
}
joshdcid commented 11 years ago

Hey.. i have the same problem again.. "DataTables warning (table id = 'DataTables_Table_0'): DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error."

I don't have any problem in the console or in the network area, and the last time it work 'cause i generate the JSON my self, but now i'm trying to use your plugin and i can't make it work.. I simplify everything to try and find my error..

Thanks in advance.. sorry for all the trouble..

tigrang commented 11 years ago

Look at the network tab after the datatable makes a request for data. It may be returning HTML because there is an error happening. If it is, what does the error say?

joshdcid commented 11 years ago

I guess this one has to be JSON.. but i got no error.. Screen Shot 2013-04-24 at 2 39 20 PM Screen Shot 2013-04-24 at 2 38 54 PM

tigrang commented 11 years ago

Yea see how it has text/html, so there is an issue. Look at the contents of that request.

joshdcid commented 11 years ago

Well i don't see any error on my code, do i have to put some echo json_encode( ) to something? or do i need to have a separte function only for paginate?

*In my View/Users/datatable.ctp echo $this->DataTable->render();

In my View/Users/datatable/datatable.ctp foreach($dtResults as $result) { $this->dtResponse['aaData'][] = array( $result['User']['id'], $result['User']['username'], $result['User']['email'], 'actions', ); }

tigrang commented 11 years ago

In the Network tab, click on the request made by datables and then click Response. There you should see what was returned. It's going to be HTML, but most likely there will be an error message somewhere in it.

joshdcid commented 11 years ago

Mmm nop.. here is the response.. from the Network tab.. http://bin.cakephp.org/view/1486975118 And in the image is the preview from the Network tab. Jajajaja sorry to bother you so much.. Screen Shot 2013-04-24 at 5 19 23 PM

tigrang commented 11 years ago

In your controller's action add: $this->DataTable->settings['trigger'] = 'datatable'; By default, datatable component is set to handle requests only when the action is index, so you have to let it know that its the datatable action you want it to automatically handle as a datatable request. It can also be an array of multiple actions, or * for all actions.

joshdcid commented 11 years ago

Hello.. i change everything (Action in the controller, the view and in the datatable carpet) to index and i got an 500 Internal Server Error... and it says:

DateTableComponent: Model not specified for request.

Error: An Internal Error Has Occurred.

Stack Trace

[internal function] → DataTableComponent->beforeRender(UsersController) CORE/Cake/Utility/ObjectCollection.php line 130 → call_user_func_array(array, array) [internal function] → ObjectCollection->trigger(CakeEvent) CORE/Cake/Event/CakeEventManager.php line 246 → call_user_func(array, CakeEvent) CORE/Cake/Controller/Controller.php line 922 → CakeEventManager->dispatch(CakeEvent) CORE/Cake/Routing/Dispatcher.php line 193 → Controller->render() CORE/Cake/Routing/Dispatcher.php line 161 → Dispatcher->_invoke(UsersController, CakeRequest, CakeResponse) APP/webroot/index.php line 100 → Dispatcher->dispatch(CakeRequest, CakeResponse)

tigrang commented 11 years ago

It has to do with something I (think) I fixed in the bug-fixes branch.

So change your view to $this->DataTable->render('User', array(), array('sAjaxSource' => array());

joshdcid commented 11 years ago

That was it!!!... Thanks! it work...

tigrang commented 11 years ago

No problem :)

tigrang commented 11 years ago

I do have it fixed in bug-fixes branch. I can't remember why I haven't merged it (there are other changes along with it).

You can grab the latest master branch. I added the fix just for this right now. So you can just use $this->DataTable->render() without the extra params.

joshdcid commented 11 years ago

Great i will clone it!.. Thanks..

mbialasek commented 10 years ago

I want to add a delete link to 'Category', which will only show up, when there is no 'Items' of this Category. How to add link depend for if statement ?

tigrang commented 10 years ago

Create a variable for your row with the values you want in each column, $row = array($result['Category']['name'], etc...)

Then check your condition: if (...) { $row[column_index_here] = 'your delete link'; }

And append the row to response $this->dtResponse['aaData'][] = $row;

mbialasek commented 10 years ago

It works! Thanks