tigrang / cakephp-datatable

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

Column for Actions is empty in rows #65

Closed fr0z3nfyr closed 9 years ago

fr0z3nfyr commented 9 years ago

I have followed the documentation(and few solutions in issues) instruction by instruction and am able to get the data correctly (of course, except the issue mentioned in #64). Now when I tried to add options to the last column of table, the column is created by config in components but the corresponding td in dataTable rows are always blank: Sample from Controller:

'DataTable.DataTable' => array(
            'MyModelAlias' => array(
                'columns' => array(
                    'field1',
                    'AssociatedTable.field2' => array(
                        'label' => 'Field#2'
                    ),
                    .
                    .
                    .
                    'Actions' => null,      // This is to add a column for actions
                ),
            ),

datatable/myModel.ctp:

$actions = $this->Html->link('<span class="glyphicon glyphicon-search"></span>', array('action' => 'view', 'some_id_here'), array('escape' => false));

foreach ($dtResults as $result) {
    $this->dtResponse['aaData'][] = array(
        $result['MyModelAlias']['field1'],
        .
        .
        .
        $actions,    // Tried 'View' instead of $actions for testing but same results- no output for this column
    );
}

Is it possible that the issue is due to 'Actions' => null, in config(should it be something else other than null)? How can this be fixed?

tigrang commented 9 years ago

So you tried the string 'View' for that column and still nothing? Setting it to null is correct, it tells the plugin that column isn't tied to the database. You would need to generate the content you need it.

tigrang commented 9 years ago

Didn't see that you had $actions defined outside the loop. You probably need it in the loop, but the code as is should output the link.

tigrang commented 9 years ago

autoRender and autoData are enabled by default. autoData will extract the columns for you, and you can set autoRender to false for further processing (like Actions column).

You can set autoData and autoRender both to false and use what you have as it is now, or just set autoRender to false and use the view file like this:

<?php
foreach ($dtResults as $i => $result) {
    $this->dtResponse['aaData'][$i][INDEX_OF_ACTION_COLUMN] = 'Action yo ';
}
fr0z3nfyr commented 9 years ago

Excellent!! I tried the second solution and it worked perfectly in first attempt... I understand why you $actions should have been inside foreach(), corrected that. Thanks for help..

tigrang commented 9 years ago

No problem.