2016-07-12 162 views
-2

這裏,我試圖設計一個工資單。我想從文本旁邊的模型傳遞數據。像 -如何將數據從模型傳遞到yii2中的視圖

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

數據將從模型中提取,並會在「月份付款單」旁邊傳遞。請幫忙。

我的控制器動作

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

我想是要傳遞一個在週期詳細視圖將顯示在「該月的付款單」行後面。我的問題是如何做到這一點

回答

0

您需要將數據從controlleraction傳遞到您正在渲染的view

e.g讓你想從索引操作發送$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> 
+0

未知你好,我想你的解決方案,但它結束了未定義的變量。我將添加我的控制器操作和查看文件以清除它。請讓我知道該怎麼做。 – Tanmay

+0

感謝您更新問題,還添加了什麼錯誤,你越來越 –

+0

我已經添加了我目前的位置,我想在問題中做什麼。 – Tanmay

相關問題