callbackColumn

callbackColumn(string $columnName, callable $callback)

The method callbackColumn is the transformation of the data for a column at the datagrid. You will need to specify the column name and then the callback for that column name. The callback then will have two parameters:

  1. The value of the column
  2. The whole row as an object. In simple words you have access to any of the data at this row in case you need to combine other parameters at the same row.

Important warning: Please notice that this transformation is happenning after the render so you will need to be careful as the search for this column may not run as you are expecting. So avoid full transformation of the columns. In case you need to create a searchable column, you will need to do that with a setModel instead.

You can find a simple example for the usage of the callback column:

$crud->callbackColumn('menu_title', function ($value, $row) {
    return "<a href='" . site_url('menu/' . $row->id) . "'>$value</a>";
});

A full example is also available below:

$crud->setTable('customers');
$crud->setSubject('Customer', 'Customers');
$crud->columns(['customerName','phone','addressLine1','creditLimit']);

$crud->callbackColumn('customerName', function ($value, $row) {
    if (!empty($value)) {
        return "<a href='/demo_view_customer/" . $row->customerNumber."' target='blank'>$value</a>";
    } else {
        // Make sure that you return white space or else the cell may break on print layout
        return '&nbsp;';
    }
});

$output = $crud->render();

As you will notice the data for the column customerName are links to a profile page that opens in new tab. The search is working as expected as the actual value is not changing.