2
votes

I'm new to Yii2. I'm my view I'd like to show two tabs listing data from the same table but showing different attribute based on the content of a column.

For example I have a table BOOK With id, name, autor, color, release date and price. Book Table And I have that code in my view.

 <?=    Tabs::widget([
'items' => [
    [
        'label' => 'New Books',
        'content' =>  GridView::widget([
                        'dataProvider' => $dataProvider,
                        'filterModel' => $searchModel,
                        'columns' => [
                            [
                                'label' => 'Book Name',
                                'attribute' => 'book_name',
                                'value' => 'book_name',
                            ],

                        ],
                    ]),
    ],
    [
        'label' => 'Old Books',
        'content' => GridView::widget([
                        'dataProvider' => $dataProviderOld,
                        'filterModel' => $searchOldModel,
                        'columns' => [
                            ['class' => 'yii\grid\SerialColumn'],

                            //'id',
                            [
                                'label' => 'Book Color',
                                'attribute' => 'book_color',
                                'value' => 'book_color',
                            ],
                             ],
                    ]),
    ],
],

]); ?>

For the moment I'm doing that with two distinct Models with two different queries. (I don't know If I'll use two different Models in the end, or I'll merge the two into one). Is there a simple way (using the same Model so the same Query) to choose what attributes to show based on a column of Book? For example, for the books released more than 10 years ago I'd like to show only the color. For the other books I'd like to show only the name (It's just a simplified example that one). Or is there another easier way to do that with only one model and modelsearch? Thanks

2

2 Answers

1
votes

You can use function in value attribute for returning proper value for each field depending on the model values directly in gridview eg: for color you can test if the year is > then 2000 and the return the value you prefer .. and so on for others column

             GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'columns' => [
                        [
                            'label' => 'Book Name',
                            'attribute' => 'book_name',
                            'value' => 'book_name',
                        ],
                        [
                            'label' => 'Color',
                            'attribute' => 'color',
                            'value' =>  function($model){
                                    if ($model->year) > '2000' {
                                        return '';
                                    } else {
                                        return $model->color;
                                    },
                        ],
                    ],
                ]),
0
votes

You can create ActiveDataProvider with UNION SELECT query. Example is shown in this quastion.