【发布时间】:2015-09-26 15:48:35
【问题描述】:
我在 Yii2 中有一个 gridview,有两列,first_name 和 last_name。 我想将这两个值合并到一个名为 full_name 的列中,就像 tihs 一样: '名'。' '。'姓' ,可搜索和可过滤。 我该怎么做?
【问题讨论】:
标签: gridview merge yii2 concat
我在 Yii2 中有一个 gridview,有两列,first_name 和 last_name。 我想将这两个值合并到一个名为 full_name 的列中,就像 tihs 一样: '名'。' '。'姓' ,可搜索和可过滤。 我该怎么做?
【问题讨论】:
标签: gridview merge yii2 concat
试试这个方法:
<?= 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', ],
],
]); ?>
【讨论】:
感谢本教程:Yii 2.0: Filter & Sort by calculated/related fields in GridView Yii 2.0
本教程仅在first_name 和last_name 单独搜索时有效,我在search model 中添加了额外的filter 条件用于全名搜索。 i.e.
'OR CONCAT(first_name, " ", last_name) LIKE "%' . $this->fullName . '%"'
第 1 步:在基本 Person 模型中添加一个 getter 函数:
设置基础模型
/* 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')
];
}
第 2 步:将属性 fullName 添加到您的模型 PersonSearch 并配置您的规则。
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;
}
第 3 步:在视图索引文件中配置 gridview 列
设置视图文件
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'fullName',
['class' => 'yii\grid\ActionColumn'],
]
]);
【讨论】:
Gridview 列由您列出的属性定义,这些属性确实转换为 yii\grid\DataColumn 对象。您可以指定自定义列,如下所示:
'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'
]
您可以通过查看yii\grid\DataColumn class reference 找到有关定义自己的自定义列的更多信息
【讨论】:
对于过滤和排序,在这种情况下,当涉及到管理具有属于同一模型的字段的计算列时,解决方案会稍微复杂一些,您基本上必须做三件事:
这些活动在下面的tutorial中有很好的描述。
【讨论】: