A custom column in tableview

Hello
in the communityedition it was easy to get an custom colum by Callback_Column
$crud->set_table(‘testcase’);
$crud->field_type(‘testdaten’, ‘readonly’);
$crud->columns(‘id’,‘name’,‘testdaten’,‘json’, ‘angelegt’);
$crud->callback_column(‘testdaten’, array($this, ‘testdaten’));

testdaten is not a column in testcase table the callback is collecting data from an other Table

in Enterprise this way
$crud->setTable(‘testcase’);|
$crud->setSubject(‘Testcase’);|
$crud->fieldType(‘testdaten’, ‘readonly’);|
$crud->callbackColumn(‘testdaten’,function ($value, $row) {
$html = “

”;

return $html . “

”;
});

produce a server 500 error

QLSTATE[42S22]: Column not found: 1054 Unknown column ‘testcase.testdaten’ in ‘field list’",

“args”: [
“SELECT testcase.id AS id, testcase.name AS name, testcase.testdaten AS testdaten, testcase.json AS json, testcase.angelegt AS angelegt FROM testcase LIMIT 10 OFFSET 0”

Any Idear to get custom column in enterprise

best gregards
rené
ps
the old code is 1.x on ci3
the new 2.x enterprise on ci4

1 Like

I think I get the same problem with you.

Let’s wait for some answers from others.

:grin: Funny enough, without knowing that you just asked this question about a custom extra column some hours ago, I wrote an email to John asking exactly this. Still waiting for response. But looking forward in solving this issue.

Hello all,

First of all welcome @obiwanke and @xyz to our forums :hugs:. Ok, there is actually a frustration and I can explain (and at the end also give a solution).

Grocery CRUD datagrid work primarily with complicated database queries. So any field should unfortunately be in the database or create a custom Model to manipulate that. However as I can understand that it is a bit of non-sense to create a custom Model only for that there is an undocumented (that I will document soon) work-around for that.

There is a function with name “mapColumn” in order to map a field with another one. Actually I will give an example to be more clear. Let’s say that we have a new field name “whatever”. As the “whatever” field doesn’t exist we need to map it with another field, let’s say “email”. This will look like this:

$crud->columns(['customer_name','test_choices', 'country', 'email', 'whatever']);
$crud->fieldTypeColumn('whatever', 'varchar');
$crud->mapColumn('whatever', 'email');
$crud->callbackColumn('whatever', function ($value) {
    // The $value here is the 'email' value
    return "<div style='background:red'>$value</div>";
});

Keep in mind that if you use a field only on column it is better to use fieldTypeColumn instead of fieldType as it is specified only for the initial datagrid.

Last but not least, I am keeping a note to create a new field type with name “fake” to solve this issue without a work-around in the future.

Let me know if that worked for you.

Regards
Johnny

Hello Johnny,

thanks for this work-around. It works as described.
It’s doing it by now, but the problem is, you always have to put all column names in the array of the $crud-columns call to get this extra “whatever” column. At the moment I had no $crud->columns method call, but with this work-araound I have to enumerate all the names of all the columns in the table.
Anyway, thanks
Regards
Obiwanke

Hello @obiwanke ,

I’ve kept a note so I can include the “fake” field type but I can’t promise any dates for that so till then you can use a helper function for Codeigniter 4. For example, you can use a helper that will look like this:

function getAllDatabaseColumns($tableName) {
            // Code for Codeigniter 4
            $db = db_connect();
            $query = $db->query('SHOW FIELDS FROM `' . $tableName . '`');
            $result = $query->getResult();
            $columnNames = [];

            foreach ($result as $row) {
                $columnNames[] = $row->Field;
            }

            return  $columnNames;
        };

And you can simply get all the columnNames by using:

getAllDatabaseColumns('yourTableName')

If you would also like to add the extra columns you can use the array_merge of PHP. So at the above example this will look like this:

$crud->columns(array_merge(getAllDatabaseColumns('customers'), ['whatever']));

Also keep in mind that I always recommend to use your common functions into one place. For example, if you would like to have an extra column with all the fields, you can always have a function like this:

function groceryCRUDAddExtraColumn($crud, $columnName) {
	$crud->fieldTypeColumn($columnName, 'varchar');
	$crud->mapColumn($columnName, 'id');
}

And use it like this:

$crud->setTable(....);
$this->groceryCRUDAddExtraColumn($crud, 'whatever');
$crud->...

I hope any of the above helped to get the basic idea that I am trying to explain :slight_smile:

Regards
Johnny

Thank you for the explanation, @johnny.

After fieldTypeColumn, we still have to map it to other column then callback that column.

But I am triggered by one “fake” column at a table