Yii2 Gridview合并两列

Yii2 Gridview合并两列,gridview,merge,yii2,concat,Gridview,Merge,Yii2,Concat,我有一个Yii2格式的gridview,有两列,first_name和last_name。 我想将这两个值合并到一个名为full_name的列中,如tihs所示: “名字”。“姓” ,可搜索和过滤。 我该怎么做呢?试试下面的方法: <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [

我有一个Yii2格式的gridview,有两列,first_name和last_name。 我想将这两个值合并到一个名为full_name的列中,如tihs所示: “名字”。“姓” ,可搜索和过滤。 我该怎么做呢?

试试下面的方法:

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

您可以通过检查

中的“过滤器和排序”来查找有关定义自定义列的更多信息。在这种情况下,解决方案稍微复杂一些。当涉及到使用属于同一模型的字段管理计算列时,您必须做三件事:

  • 调整表单以在计算字段中处理(通过添加字段并创建适当的getter方法)
  • 调整搜索模型(为计算字段添加andFilterWhere以过滤查询)
  • 调整视图(以处理新的计算字段)
  • 以下对这些活动进行了详细描述。

    多亏了本教程:

    本教程仅在单独搜索
    first_name
    last_name
    时有效,我在
    搜索模型中为全名搜索添加了额外的
    过滤器
    条件<代码>即

    'OR CONCAT(first_name, " ", last_name) LIKE "%' . $this->fullName . '%"'
    
    步骤1:向基本人物模型添加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:向model PersonSearch添加属性fullName并配置规则

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

    您好,很抱歉耽搁了,我为您或上一个问题添加了一个新答案,因为评论太短,我希望答案和图托拉尔区域对您来说是清楚的,否则请评论我,以便我可以解释更多。我使用的是yii-basic-app-2.0.11,但不起作用,搜索和排序功能不起作用。我需要将customers表的两个字段合并,并在Logs网格中显示它们