Concept
We have already described the concept of Table in Here.
Let us recap the concept.
AIM Admin resolves the DAL (Data Access Layer) using the following function:
public function getRepository()
{
return $this->repo;
}
When it's resolved, it takes the column array from initGrid() and generates the table automatically:
$this->initGrid(['product_code', 'product_name', 'product_price', 'product_quantity'], pagination: 10);
Modifying Grid Columns with Field Callback Function
Let's say you need to remove product_code
from the columns and concatenate it with product_name
. There are two ways to do that:
- Using a query
- Using a Field callback function
Let's try with the Field callback function:
$this->initGrid([
Field::init('product_name')->formatValue(function ($value, $row) {
return $value . ' : ' . $row['product_code'];
}),
'product_price',
'product_quantity'
], pagination: 10);
Here, Field is used for the grid column. It has an init method similar to TextField. It also has a formatValue method, which is a callback function. This function takes two parameters: the current column value and the full row value.
$value
The current value of the product_name column.
$row
The full row of data, from which we access the product_code.
This example concatenates the product_name with the product_code for display in the grid.
Customizing Column View
In some cases, you need to customize the column view. Just like with form components, you can use setComponent to set your custom component for the column view. In that custom component, you can easily access the $value and $row variables.
Field::init('product_name')->setComponent('custom-component')
In your custom component, you can access:
$value
The current value of the field.
$row
The entire row data.
This allows you to fully customize how each column is rendered in the grid.