i have a gridview in Yii2 with two columns, first_name and last_name. I want to merge this two values into one single column named full_name made like tihs: 'first_name'.' '.'last_name' , searchable and filterable. How can i do it?
4 Answers
try this way:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'an_attributeid',
'label' => 'yourLabel',
'value' => function($model) { return $model->first_name . " " . $model->last_name ;},
],
['class' => 'yii\grid\ActionColumn', ],
],
]); ?>

- 131,976
- 10
- 91
- 107
-
Hi, sorry for delay, i added a new answer for yuor last question, because comment is too shorto, I hope the answer and the tutorail area clear for you, otherwise comment me so i can explain more. – ScaisEdge Jul 09 '15 at 15:12
Thanks to this tutorial: Yii 2.0: Filter & Sort by calculated/related fields in GridView Yii 2.0
The tutorial is only working when first_name
& last_name
searched separately, I have added an additional filter
condition for full name search in the search model
. i.e.
'OR CONCAT(first_name, " ", last_name) LIKE "%' . $this->fullName . '%"'
STEP 1: Add a getter function to your base Person model:
Setup base model
/* Getter for person full name */
public function getFullName() {
return $this->first_name . ' ' . $this->last_name;
}
/* Your model attribute labels */
public function attributeLabels() {
return [
/* Your other attribute labels */
'fullName' => Yii::t('app', 'Full Name')
];
}
STEP 2: Add an attribute fullName to your model PersonSearch and configure your rules.
Setup search model
/* your calculated attribute */
public $fullName;
/* setup rules */
public function rules() {
return [
/* your other rules */
[['fullName'], 'safe']
];
}
/**
* setup search function for filtering and sorting
* based on fullName field
*/
public function search($params) {
$query = Person::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
/**
* Setup your sorting attributes
* Note: This is setup before the $this->load($params)
* statement below
*/
$dataProvider->setSort([
'attributes' => [
'id',
'fullName' => [
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
'label' => 'Full Name',
'default' => SORT_ASC
],
'country_id'
]
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$this->addCondition($query, 'id');
$this->addCondition($query, 'first_name', true);
$this->addCondition($query, 'last_name', true);
$this->addCondition($query, 'country_id');
/* Setup your custom filtering criteria */
// filter by person full name
$query->andWhere('first_name LIKE "%' . $this->fullName . '%" ' . //This will filter when only first name is searched.
'OR last_name LIKE "%' . $this->fullName . '%" '. //This will filter when only last name is searched.
'OR CONCAT(first_name, " ", last_name) LIKE "%' . $this->fullName . '%"' //This will filter when full name is searched.
);
return $dataProvider;
}
STEP 3: Configure your gridview columns in your view index file
Setup view file
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'fullName',
['class' => 'yii\grid\ActionColumn'],
]
]);

- 865
- 1
- 11
- 28
-
I am using yii-basic-app-2.0.11, but not working, the search and sort functionalities are not working. I need to concat 2 fields of customers table, and show them in Logs grid – Galilo Galilo Jun 07 '17 at 09:05
Gridview columns are defined by the attributes you list, which are indeed converted into yii\grid\DataColumn objects. You can specify a custom defined column as following:
'columns=>[
'first_column',
'second_column'=>[ //note that using an index like 'second_column' here, is not necessary, but it helps understand what this column definition attempts to define.
'attribute' => 'first_name', //must be a known model attribute, used for filtering/sorting
'value' => ($model, $key, $index, $column) { //here you can specify a custom anonymous function to return more complex data for display, note that it WON'T BE HTML ENCODED.
return $model->first_name . " " . $model->last_name ;
},
],
'third_column'
]
You can find more information on defining your own custom columns by checking the yii\grid\DataColumn class reference

- 1,105
- 11
- 20
For Filter and sort the solution is a bit more complex in a case like this when it comes to managing a computed column with fields that belong to the same model you have to essentially do three things:
- Adapt the form to handle in the field calculated (by adding the field and creating an appropriate getter method),
- Adapt the search model (to filter the query adding the andFilterWhere for the calculated filed).
- Adjust the view (to handle the new calculated field) .
These activities are well described in the following tutorial.

- 131,976
- 10
- 91
- 107