Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Support for DT_RowId #27

Open
AshHimself opened this issue Dec 2, 2014 · 12 comments
Open

Support for DT_RowId #27

AshHimself opened this issue Dec 2, 2014 · 12 comments

Comments

@AshHimself
Copy link

Is it possible to implement the datatables option DT_RowId with cakephp-datatable?

The only way I was able to get it to work was to add the following code to the component. Though the side effect for this was that linkable then no longer worked.

Added to the getResponse function;
$removeName = Configure::read('current_controller');
$removeName = rtrim($removeName, "s");
$response = Set::extract('/' . $removeName . '/.', $response);

Datatables documentation as follows; http://datatables.net/release-datatables/examples/server_side/ids.html

@cnizzardini
Copy link
Owner

Can you link to the dataTables documentation on DT_RowId?

@AshHimself
Copy link
Author

@AshHimself
Copy link
Author

Any idea or direction you could point me to fix this myself?

@cnizzardini
Copy link
Owner

Sorry man I don't really use this anymore, because I don't program on Cake2 applications anymore :-( If you have code you want to commit into this project I can make you a collaborator.

@AshHimself
Copy link
Author

Not a problem at all.. I will look into fixing it and commit. Thanks Chris.

On Thu, Jan 22, 2015 at 12:26 AM, Chris Nizzardini <notifications@github.com

wrote:

Sorry man I don't really use this anymore, because I don't program on
Cake2 applications anymore :-( If you have code you want to commit into
this project I can make you a collaborator.

Reply to this email directly or view it on GitHub
#27 (comment)
.

@jammy-git
Copy link

@AshHimself Did you manage to make any progress with this?

@AshHimself
Copy link
Author

Hey @jammy-git Looks like dataTables itself only lets you use DT_RowID with a flat array. You can define what field you would like DT_RowID to lookup but only in with the Editor license. My solution was to apply the id to the row using "fnRowCallback". This issue can be close

"fnRowCallback": function(nRow, aData, iDisplayIndex) {

nRow.setAttribute('id',aData.DT_RowId);
},

@jammy-git
Copy link

Thanks for the rapid response!

I came up with a slightly different solution in the end. In getResponse, from line 163:

if($this->mDataProp == true){
    if( isset($i[$this->controller->modelClass]) && array_key_exists('id', $i[$this->controller->modelClass]) ) {
        $i['DT_RowId'] = $i[$this->controller->modelClass]['id'];
    } elseif( array_key_exists('id', $i) ) {
        $i['DT_RowId'] = $i['id'];
    }
    $response['aaData'][] = $i;
}

@AshHimself
Copy link
Author

Fantastic, I'll defiantly give your code a test when I get home as this will save me from having to define the callback in each DataTable!

@jammy-git
Copy link

No problem.

Obviously it's not very flexible as it forces the use of the 'id' field for the Model to be the DT_RowId, but I figure in 99% of use cases that's what you'd want anyway.

I'm using DataTables 1.9.4 and Editor 1.2.4 and it seems to work OK - the editor passes the correct id through via the Create and Edit AJAX calls at least.

I think the best way to do this would be to specify a "dtRowIdField" as a property of the DataTableComponent, then have the component alter the conditions of the query and have the DB return DT_RowId as part of the query results. Think that would be slightly less overhead than altering $response in a loop - especially for larger datasets!

@jammy-git
Copy link

And after all that, I go back and look at the documentation this morning and find the idSrc property!

@AshHimself
Copy link
Author

I was just about to tell you that :) Sadly, its only available in the editor plugin.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants