Yii2 Gridview merge two columns

时间:2015-07-08 15:50:52

标签: gridview merge yii2 concat

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 个答案:

答案 0 :(得分:11)

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',  ],
    ],


]); ?>

答案 1 :(得分:4)

感谢本教程:Yii 2.0: Filter & Sort by calculated/related fields in GridView Yii 2.0

本教程仅在first_name&amp; last_name单独搜索,我在filter中为全名搜索添加了额外的search model条件。 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'],
    ]
]);

答案 2 :(得分:1)

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

找到有关定义自定义列的详细信息

答案 3 :(得分:1)

对于过滤和排序,在这样的情况下,解决方案有点复杂,当涉及到管理具有属于同一模型的字段的计算列时,您必须基本上做三件事:

  1. 在计算的字段中调整表单以进行处理(通过添加字段并创建适当的getter方法),
  2. 调整搜索模型(过滤查询,为计算的字段添加andFilterWhere)。
  3. 调整视图(以处理新的计算字段)。
  4. 以下tutorial详细介绍了这些活动。