0

I have a report table and columns report_id, reference_number and report_name. Some of the data has the same reference_number. It is displayed in the gridview with view button.

What I want is when I click the view button of the specific row, the data of the row will show as well as the row that has the same reference_number of i selected. As of now, i can display all the records with the same reference_number but what I want is when I click the specific row, it will be shown on the top and the other details will just below it.

    $gridColumns = [
            ['class' => 'yii\grid\SerialColumn'],
            'report_id',
            'reference_no',
            'subject',
            'doc_date', 
            [
                'label' => 'For',
                'value' => 'namefor.fullName',
            ],
            [
                'label' => 'From',
                'value' => 'namefrom.fullName',
            ],
            'drawer_id',
            'user_id',
            'doc_name',
            [
                'class' => 'yii\grid\ActionColumn',
            ],
            [
                'attribute' => '',
                'format' => 'raw',
                'value' => function($data)
                {
                    return Html::a('', ['report/download', 'id' => $data->reference_no],['class' => 'fa fa-download']);
                }
            ],
        ];
    echo   

    // ExportMenu::widget([
    //     'dataProvider' => $dataProvider,
    //     'columns' => $gridColumns
    // ]);

    GridView::widget([
        'dataProvider'=> $dataProvider,
        'filterModel' => $searchModel,
        'columns' => $gridColumns,
        'responsive'=> true,
        'hover'=> true,
        'pjax'=> true,
        'pjaxSettings'=>[
            'neverTimeout'=>true,
        ]
    ]);

The controller

    public function actionIndex()
    {
    $searchModel = new reportDetailsSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
    }
    public function actionView($id)
    {   
    $dataProvider = new ActiveDataProvider([
        'query' => reportDetails::find()
                ->select('reference_no, report_id')
                ->where(['report_id' => $id]),
        'pagination' => [
            'pageSize' => 10,
        ],
    ]);
    return $this->render('view', [
        'model' => $this->findModel($id),
        'dataProvider' => $dataProvider,
    ]);
    }
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Jaypee
  • 53
  • 1
  • 2
  • 10

1 Answers1

0

I am assuming that you have a unique key as report_id. Every reference_number has a unique report_id. So that populate grid and pass report_id as a query string in view button. And then get that value from your controller's action.

For example:

     <?php 
$gridColumns = [
            ['class' => 'yii\grid\SerialColumn'],
            'report_id',
            'reference_no',
            'subject',
            'doc_date', 
            [
                'label' => 'For',
                'value' => 'namefor.fullName',
            ],
            [
                'label' => 'From',
                'value' => 'namefrom.fullName',
            ],
            'drawer_id',
            'user_id',
            'doc_name',
            [
                    'class' => 'yii\grid\ActionColumn',
                    'header' => 'Actions',
                    'contentOptions'=> [ 'style' => 'vertical-align: center;width:8%!important;'],
                    'template' => '{view}',
                    'buttons' =>
                            [
                                'view' => function ($url, $model, $key) 
                                    {
                                        return Html::a('&nbsp;<span class="fa fa-download"></span>&nbsp;', ['report/download', 'id' => $model->reference_no],            
                                                 [  
                                                    'title' => Yii::t('app', 'View'),
                                                    'class' => 'btn btn-primary btn-xs',
                                                    'data-method' => 'post',

                                        ]);
                                    },
                            ],            
            ],
            /*[
                'attribute' => '',
                'format' => 'raw',
                'value' => function($data)
                {
                    return Html::a('', ['report/download', 'id' => $data->reference_no],['class' => 'fa fa-download']);
                }
            ], */
        ];
    echo   GridView::widget([
        'dataProvider'=> $dataProvider,
        'filterModel' => $searchModel,
        'columns' => $gridColumns,
        'responsive'=> true,
        'hover'=> true,
        'pjax'=> true,
        'pjaxSettings'=>[
            'neverTimeout'=>true,
        ]
    ]); 
?>

Then your actionDownload in Contorller

<?php
public function actionDownload($id) {

        //var_dump($id); die;

        //$emodel = Yourmodel::find()->where('id="' . $id . '"')->all();

        if (Yii::$app->request->post()) {

                // var_dump(Yii::$app->request->post()); die;

                var_dump($_POST); die;
                 // Do your stuff   

            }

        }
        return $this->render('download');
        ]);
    }
 ?>
vijay nathji
  • 1,608
  • 13
  • 23
  • Where specifically should i put this code? Im new in yii2. Thank you sir. – Jaypee May 29 '17 at 07:20
  • I am getting an error sir. Setting unknown property: yii\grid\DataColumn::view – Jaypee May 29 '17 at 08:30
  • kindly edit your question and post grid page code and controller code. – vijay nathji May 29 '17 at 08:36
  • @user8028650 : Pls check my updated answer then try. If its helps you then accept my answer. thanks – vijay nathji May 29 '17 at 09:52
  • Im still confused on how will i do it. Can you please include how to do make it in the controller? – Jaypee May 31 '17 at 01:00
  • @user8028650 : Controller you need separate action and you can get reference_no as $id and then perform necessary steps as per your needs – vijay nathji May 31 '17 at 04:47
  • I think we are having a misunderstanding here. What i want is like on this link. https://stackoverflow.com/questions/5417980/mysql-sql-specific-item-to-be-first-and-then-to-sort-the-rest-of-the-items But i do not know how to convert it in yii2. ->orderBy('???') – Jaypee May 31 '17 at 05:01
  • Just go thorough this site Once as a beginner: http://www.bsourcecode.com/yiiframework2/select-query-model/#orderBy – vijay nathji May 31 '17 at 05:05
  • I am looking for converting this order by in yii2. "order by id=5 desc, id asc" the only option that i am getting so far is just ->orderBy('column_name' => SORT_ASC/SORT_DESC) – Jaypee May 31 '17 at 05:14