cnizzardini / cakephp-datatable

CakePHP Component for interoperability between CakePHP 2.x and jQuery DataTables plugin.
62 stars 47 forks source link

Support for DT_RowId #27

Open AshHimself opened 9 years ago

AshHimself commented 9 years ago

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 commented 9 years ago

Can you link to the dataTables documentation on DT_RowId?

AshHimself commented 9 years ago

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

AshHimself commented 9 years ago

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

cnizzardini commented 9 years ago

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 commented 9 years ago

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

jammy-git commented 9 years ago

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

AshHimself commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

AshHimself commented 9 years ago

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