cakephp-datatable icon indicating copy to clipboard operation
cakephp-datatable copied to clipboard

Support for DT_RowId

Open AshHimself opened this issue 10 years ago • 12 comments

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

AshHimself avatar Dec 02 '14 12:12 AshHimself

Can you link to the dataTables documentation on DT_RowId?

cnizzardini avatar Dec 02 '14 15:12 cnizzardini

Sure... http://datatables.net/release-datatables/examples/server_side/ids.html

AshHimself avatar Dec 03 '14 00:12 AshHimself

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

AshHimself avatar Jan 21 '15 11:01 AshHimself

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.

cnizzardini avatar Jan 21 '15 16:01 cnizzardini

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 <[email protected]

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 https://github.com/cnizzdotcom/cakephp-datatable/issues/27#issuecomment-70868737 .

AshHimself avatar Jan 22 '15 00:01 AshHimself

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

jammy-git avatar Jan 27 '15 23:01 jammy-git

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);
},

AshHimself avatar Jan 28 '15 00:01 AshHimself

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;
}

jammy-git avatar Jan 28 '15 00:01 jammy-git

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!

AshHimself avatar Jan 28 '15 00:01 AshHimself

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 avatar Jan 28 '15 09:01 jammy-git

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

jammy-git avatar Jan 28 '15 10:01 jammy-git

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

AshHimself avatar Jan 28 '15 11:01 AshHimself