Yii2 Gridview 合并两列

Yii2 Gridview merge two columns

我在 Yii2 中有一个包含两列的 gridview,first_name 和 last_name。 我想将这两个值合并到一个名为 full_name 的列中,就像 tihs: 'first_name'.' '.'last_name' ,可搜索和可过滤。 我该怎么做?

试试这个方法:

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


]); ?>

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

找到有关定义自己的自定义列的更多信息

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

  1. 调整表单以在计算的字段中处理(通过添加字段并创建适当的 getter 方法),
  2. 调整搜索模型(过滤查询,为计算的字段添加 andFilterWhere)。
  3. 调整视图(以处理新的计算字段)。

下面tutorial对这些活动进行了详细描述。

感谢本教程: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'],
    ]
]);