这是问题Pass data from a form to a controller in yii2和Get data from textbox and pass to Controller in yii2的延续。
我试图通过controllerAction actionStockbetweendates
生成报告。此productname
,startdate
,enddate
的参数来自index2
,点击按钮时通过javascript函数。在控制台中我收到以下错误。
index2.php
<?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;
use yii\helpers\Json;
use yii\db\Query;
use yii\db\Command;
use kartik\mpdf\Pdf;
/* @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 <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 <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 <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">
<button id="yourButton" class="btn btn-primary" >Seach</button>
</div>
</p>
</div>
<?php
/* start getting the itemid */
$this->registerJs(
"$('#yourButton').on('click', function() {
var productname = $('#upc').val();
var startdate = $('#startdate').val();
var enddate = $('#enddate').val();
//alert(uPc);
$.get('index.php?r=stock/sellitem/stockbetweendates',{ productname : productname, startdate : startdate, enddate : enddate}, function(data){
var data = $.parseJSON(data);
});
});"
);
/* end getting the itemid */
?>
ControllerAction
public function actionStockbetweendates($productname, $startdate, $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('_printproductledgerbtdt', [
'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]);
return $this->render('index2', [
'upc' => $upc,
'startdate' => $startdate,
'enddate' => $enddate,
]);
}
更新了javascript
<?php
/* start getting the itemid */
$this->registerJs(
"$('#yourButton').on('click', function() {
var productname = $('#upc').val();
var startdate = $('#startdate').val();
var enddate = $('#enddate').val();
//alert(uPc);
$.get('index.php?r=stock/sellitem/stockbetweendates',{ productname : productname, startdate : startdate, enddate : enddate});
});"
);
/* end getting the itemid */
?>
更新了控制器操作
public function actionStockbetweendates($productname, $startdate, $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('_printproductledgerbtdt', [
'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();
}
之前使用的按钮来调用Controller动作并传递参数
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{ledger}',
'buttons' => [
'ledger' => function ($url, $model) {
//var_dump($model);
//exit;
return Html::a(
'<span class="glyphicon glyphicon-eye-open"></span>',
['/stock/sellitem/printproductledger', 'productname' => $model['i_upc'],'prodesc'=>$model['i_desc']],
[
'title' => 'Ledger',
'data-pjax' => '0',
]
);
},
],
],
更新了控制器操作
public function actionPrintproductledger2($productname) {
$productname = yii::$app->request->get('productname');
$prodesc = yii::$app->request->get('prodesc');
$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);
Yii::$app->response->format = Response::FORMAT_JSON;
return $this->render('_printproductledgerbtdt', [
'modelProduction' => $modelProduction,
'modelSell' => $modelSell,
'dataProvider1' => $dataProvider1,
'searchModel1' => $searchModel1,
'searchModel2' => $searchModel2,
'dataProvider2' => $dataProvider2,
'productname' => $productname,
'prodesc' => $prodesc,
]);
}
更新了javascript
<?php
/* start getting the itemid */
$this->registerJs(
"$('#yourButton').on('click', function() {
var productname = $('#upc').val();
var startdate = $('#startdate').val();
var enddate = $('#enddate').val();
var desc = $('#desc').val();
//alert(productname);
//$.get('index.php?r=stock/sellitem/printproductledger2',{ productname : productname, startdate : startdate, enddate : enddate,prodesc:desc});
$.ajax({
type: 'GET',
url: 'index.php?r=stock/sellitem/printproductledger2',
data: { productname:productname, startdate : startdate, enddate : enddate,prodesc:desc},
contentType: 'application/json; charset=utf-8',
//dataType: 'json',
success: function() { alert('Success'); }
});
});"
);
/* end getting the itemid */
?>
答案 0 :(得分:1)
您的代码无法正常工作
所以这不是一个恰当的答案,而是一些主要的建议
1)您正在使用此调用获取数据
$.get('index.php?r=stock/sellitem/stockbetweendates',
{ productname : productname, startdate : startdate, enddate : enddate},
function(data){
var data = $.parseJSON(data);
});
数据获取未正确JSON格式..
如果您需要正确的数据JSON格式,则应在操作中正确编码encode_JSON,并使用echo将此数据发送给调用者。
2)在你的actionStockbetweendates
你有两次返回陈述
]);
return $pdf->render();
//return $this->render('_printSalarystatement', ['s_period' => $s_period]);
return $this->render('index2', [
'upc' => $upc,
'startdate' => $startdate,
'enddate' => $enddate,
]);
显然只有第一个工作..并且(可能)呈现pdf ..
但是这样yuo并没有向ajax调用者发送nothings($ .get ...)
我的建议是你将代码拆分为独立的函数。每个函数都与单个需求有关。
对于pdf尝试使用
'mode' => Pdf::MODE_BLANK,
代替('mode'=> Pdf::MODE_UTF8,
)