Different buttons on each row of a table/grid/CRUD


#1

I want to have various buttons for each row of a table. Like if it is an editable row, an edit button, if row can be deleted, a trash can, etc.

Preferably, a callback which would have a model->load()->delete(), then a fade left animation on the row, or a popup for an edit…etc. like CRUD does.

What is the least effort way of doing this in atk? Removing buttons from CRUD per row maybe? Or using table or grid, and adding buttons on an Actions column?


#2

Well, here is what i’ve done about this, let me know the better way of doing it.

To my table i add an actions column;

$ac = new \UI\ConditionalActions( );
$table->addColumn( ‘Actions’, $actionsCol );

and then add actions to this column, first one being enabled/disabled based on the row:

$ac->addActionWithConditionFunction( [‘icon’ => ‘red trash’], function($j, $id) use($m) { … } ‘Are you sure?’, ‘canUpdate’ )
$ac->addAction( [‘icon’=>‘chevron circle right’], function ( $j, $id ) use ($app) { … } );

Then it works. the conditional delete button, only appears enabled, if the $model->canUpdate() returns true. Otherwise it appears ‘disabled’

So I created a new TableColumn class, ‘ConditionalActions’ extending ‘Actions’.

Added a method called addActionWithConditionFunction, which is very similar to addAction from Actions class, with the added final parameter as a string, which passes a function name. Model of this table is supposed to be implementing this function, which returns false if the action can not be performed.

Class ConditionalActions, keeps conditionFunction name (‘canUpdate’ in this case) in an array for each action. It also adds a class to the action button {$_name_action_n_state} name being the column name, n being the action index.

Class also overrides getHTMLTags($row, $field) which is called per row. It calls the stored method from the model ($row->canUpdate() in this case), and returns ‘disabled’ for the _name_action_n_state tag, causing the button appear disabled.

So this a merged version of Actions and Status in a way. I wonder if there was a better way of doing this, without any subclassing.


#3

You should look into this:


#4

This is exactly what i was looking for. Now i’ll be looking forward for next release.


#5

this was merged some time ago, so it’s there in the recent releases… :wink:


#6

Here is link to documentation https://agile-ui.readthedocs.io/en/latest/tablecolumn.html#multiformat