Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

How to pass data from model to a view in yii2

Here, I'm trying to design a payslip. I want to pass data from model right beside the text. like -

<p class="text-center">Pay Slip for the month of  </p>

The data will be fetched from the model and will be passed right beside "Pay Slip for the month of". Please help.

My Controller Action

public function actionPrintsalarystatement($id) {

        //$model =  Salary::find()->where(['s_id' => $id]);
        $model = $this->findModel($id);
        $searchModel  = new SalarySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $data         = Salary::findOne($id);
        $content = $this->renderPartial('_printSalarystatement', ['model' => $model,'dataProvider' => $dataProvider,'searchModel'  => $searchModel,'data'=> $data]);
        $pdf = new Pdf([
            'mode'=> Pdf::MODE_UTF8,
            'format'=> Pdf::FORMAT_A4,
            'destination'=> Pdf::DEST_BROWSER,
            //'destination' => Pdf::DEST_DOWNLOAD,
            'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
            // any css to be embedded if required
            'cssInline' => '.kv-heading-1{font-size:18px}', 
             // set mPDF properties on the fly
            'options' => ['title' => 'Print Payslip'],
             // call mPDF methods on the fly
            'methods' => [
                'SetHeader'=>['Private and Confidential'], 
                'SetFooter'=>['This Payslip is computer generated.'],
            ],
            'content'     => $content,


        ]);
        return $pdf->render();
        //return $this->render('_printSalarystatement', ['period' => 's_period']);
    }

_printSalarystatement.php

<?php

use yiihelpersHtml;
use yiiwidgetsDetailView;

/* @var $this yiiwebView */
/* @var $model frontendmodulessalarymodelsSalary */
//@var $model yiiaseModel
//@var $totaldays any

//$this->title = $model->s_id;
$this->params['breadcrumbs'][] = ['label' => 'Salaries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="salary-view">

    <h1><?= Html::encode($this->title) ?></h1>
    <h1><strong><p class="text-center">PS Enterprise</p></strong></h1>
    <p class="text-center">Pay Slip for the month of s_period </p>


    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            's_id',
            's_date',
            's_period',
            's_empid',
            's_empname',
            's_workingdays',
            's_leave',
            's_holiday',
            's_wageperday',
            's_totalwage',
            's_ovthour',
            's_ovtrateperhour',
            's_ovtamount',
            's_tiffinworkingday',
            's_tiffinrateperday',
            's_wdtiffinamount',
            's_sundayworked',
            's_sundayrate',
            's_sundaytiffinamount',
            's_nightcount',
            's_nightallrate',
            's_nightallowance',
            's_convday',
            's_convrate',
            's_conveyanceamount',
            's_tiffinovtcount',
            's_tiffinovtrate',
            's_tiffinovtamount',
            's_incentivecount',
            's_incentiverate',
            's_incentive',
            's_totalearning',
            's_epf',
            's_esi',
            's_ptax',
            's_takehome',
        ],
    ]) ?>

</div>

By the controller action I get the following screen enter image description here

What I want is to pass the period that is in the detailview to be shown after the line "Pay Slip for the month of ". mY question is how to do that

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to pass data from the controller action to the view you are rendering.

e.g lets hope you want to send $name variable from index action to view

Action will be like

public function actionIndex()
    {
        return $this->render('index', ['name' => 'Tanmay']);
    }

Now the array you passed in the 2nd argument of render function is your variable list. All the keys will be treated as variable in the index view

The code for the index view will be

<h1>Welcome <?= $name; ?>

that is ['name' => 'Tanmay'] will be converted to $name having value 'Tanmay'

Hope this helps. Thanks.

Updated according to the updated Question:

$content = $this->renderPartial('_printSalarystatement', [
'model' => $model,
'dataProvider' => $dataProvider,
'searchModel'  => $searchModel,
'data'=> $data,
's_period' => $s_period // new variable added
]
);

in Controller

in view:

<p class="text-center">Pay Slip for the month of <?= $s_period; ?> </p>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...