在yii2中将数据从表单传递到控制器

时间:2017-01-22 09:57:27

标签: yii2

我正在创建一个包含3个字段的页面 - 产品代码,startdate,enddate。当我点击搜索按钮时,它应该创建一个pdf文件。其中3个领域没有模型。 enter image description here

我尝试过代码 -

<?php

use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
use frontend\modules\stock\models\Sellitem;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use kartik\form\ActiveForm;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\stock\models\SellitemSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Stock';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitem-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="row">
        <div class="form-group">
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Product Code&nbsp;<i class="icon-star"></i></p></label>
                <input type="text" class="form-control" id="upc" class="span3">
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">Start Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'startdate',
                'id' => 'startdate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
            <div class="col-xs-4 col-sm-4 col-lg-4">
                <label for="upc" class="control-label"><p class="text-info">End Date&nbsp;<i class="icon-star"></i></p></label>
                <?= DatePicker::widget([
                //'label' => 'Startdate',
                'name' => 'enddate',
                'id' => 'enddate',
                //'value' => '02-16-2012',
                'template' => '{addon}{input}',
                    'clientOptions' => [
                        'autoclose' => true,
                        'todayHighlight' => true,
                        'format' => 'yyyy-mm-dd'
                    ]
                ]);?>
            </div>
        </div>

    </div>
    <p>
        <div class="form-group pull-right">
            <?= Html::a('Search', ['/stock/sellitem/stockbetweendates','upc'=> $upc, 'startdate'=> $startdate, 'enddate'=>$enddate], 
        ['class'=>'btn btn-success']) 
        ?>
        </div>
    </p>
</div>

这是错误 - 未定义的变量。 我在模型中添加了$ upc,$ startdate,$ enddate,但它没有帮助。请让我知道如何将这些值传递给控制器​​。

控制器操作

public function actionStockbetweendates($upc, $startdate, $enddate) {

$upc = yii::$app->request->get('upc');
$startdate = yii::$app->request->get('startdate');
$enddate = yii::$app->request->get('enddate');

// $modelProduction = Puritem::find()->where(['pi_upc' => $productname]);
// $searchModel1  = new PuritemsbSearch();
// $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams, $productname);

// $modelSell  = Sellitem::find()->where(['si_iupc' => $productname]);
// $searchModel2 = new SellitemsbSearch();       
// $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams, $productname);

// $content = $this->renderPartial('_printproductledger', [
//     'modelProduction' => $modelProduction,
//     'dataProvider1' => $dataProvider1,
//     'searchModel1'  => $searchModel1,
//     //'data'=> $data,

//     'modelSell' => $modelSell,
//     'searchModel2' => $searchModel2,          
//     'dataProvider2' => $dataProvider2,

//     'productname' => $productname,
//     'prodesc' => $prodesc,

//     ]);
// $footer = "<table name='footer' width=\"1000\">
//    <tr>             
//      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Signature</td>
//    </tr>
//  </table>";
// $pdf = new Pdf([
//     'mode'=> Pdf::MODE_UTF8,
//     'format'=> Pdf::FORMAT_A4,
//     'destination'=> Pdf::DEST_BROWSER,
//     'orientation'=> Pdf::ORIENT_LANDSCAPE,
//     //'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 Party Ledger'],
//     //'options' => ['defaultfooterline' => 0,],
//     'options' => ['defaultheaderline' => 0,],
//      // call mPDF methods on the fly
//     'methods' => [
//         'SetHeader'=>['Ledger'], 
//         //'SetFooter'=>[$footer],
//     ],
//     'content' => $content,

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

}

呈现到index2页面的ActionIndex

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

        return $this->render('index2', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            ]);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定它如何工作而不渲染......但无论如何,要定义它们,你需要将它们传递给视图文件。最常见的方法(我认为目前最好的方法)是使用$this->render()操作。在public function actionStockbetweendates($upc, $startdate, $enddate) {结尾处添加此内容:

return $this->render('index2', [
    'upc' => $upc,
    'startdate' => $startdate,
    'enddate' => $enddate,
];

一方面注意:您不需要使用这些:

$upc = yii::$app->request->get('upc');
$startdate = yii::$app->request->get('startdate');
$enddate = yii::$app->request->get('enddate');

它们已在参数中定义(使用)。