Yii2 Gridview merge two columns

匿名 (未验证) 提交于 2019-12-03 08:56:10

问题:

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?

回答1:

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


回答2:

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


回答3:

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



回答4:

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:

  1. Adapt the form to handle in the field calculated (by adding the field and creating an appropriate getter method),
  2. Adapt the search model (to filter the query adding the andFilterWhere for the calculated filed).
  3. Adjust the view (to handle the new calculated field) .

These activities are well described in the following tutorial.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!