如何将数据从模型传递到yii2中的视图

时间:2016-07-12 02:01:51

标签: yii2

在这里,我试图设计一个工资单。我想从文本旁边的模型传递数据。喜欢 -

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

数据将从模型中获取,并将在&#34; Pay Slip旁边传递&#34;。请帮忙。

我的控制器操作

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 yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model frontend\modules\salary\models\Salary */
//@var $model yii\base\Model
//@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>

通过控制器操作,我得到以下屏幕 enter image description here

我想要的是传递详细视图中的句点,以显示在&#34;&#34;&#34;的付款单之后的行。我的问题是如何做到这一点

1 个答案:

答案 0 :(得分:0)

您需要将数据从controller action传递到您正在呈现的view

例如,希望您希望从索引操作发送$name变量到视图

行动就像

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

现在,您在render函数的第二个参数中传递的数组是您的变量列表。所有键都将在索引视图中被视为变量

索引视图的代码将是

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

['name' => 'Tanmay']将转换为$name,其值为'Tanmay'

希望这会有所帮助。 感谢。

根据更新的问题进行了更新:

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

在视图中:

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