List Columns
List columns are declared using the column()
method, which adds a column to the list.
Information is displayed in the list based on the data we receive from the model and passed to the list
using the view($rows)
method, which is responsible for rendering the list.
Let's take a closer look at how to work with list columns:
$list->column(
'column_id', # Column ID (corresponds to the field name in the database)
'title', # Column title
'100' # Column width
# $extra additional parameters
[ ... ],
# string|callable $render additional processing of the field content
function(){},
);
Simple content output when column_id
corresponds to the field name in the database:
$list
->column('id', 'ID', 50)
->column('title', 'Title', 200);
Processing Column Content
Often, the value in a column requires additional preparation. To do this, you should specify the column type as COLUMN_TYPE_CUSTOM
and pass a callback function responsible for preparing the data.
For example, if we need to combine the name and surname data and display it in a single author column:
$list
->column('author', 'Author', 200, [
'type' => $list::COLUMN_TYPE_CUSTOM,
'align' => $list::COLUMN_ALIGN_LEFT,
], function($value, $item, $opts){
// form content based on name and surname data
return $item['name'] . ' ' . $item['surname'];
});
Note that we used one of the additional parameters 'align' => $list::COLUMN_ALIGN_LEFT
, which aligns the content of the column to the left. By default, alignment is centered.
Sorting by Columns
You can also sort by columns when you need to change the order of records on the page. This is specified as additional parameters:
$list->column('created', 'Created Date', 150, [
'order' => $list::COLUMN_ORDER_DESC,
]);
Sorting is handled in the controller's method by setting the sorting order for the list in the model.
switch ($this->input->postget('order')) {
// ...
case 'created-desc':
$orderBy = 'created DESC';
break;
case 'created-asc':
$orderBy = 'created ASC';
break;
}