我试图将我的gridview导出为pdf而没有运气:(。这是我控制器代码的一部分:
class LntTripsController extends Controller
{
protected $repquery; // my only idea is to pass query parameters via global variable
.................
public function actionIndex()
{
$searchModel = new LntTripsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$this->repquery = Yii::$app->request->queryParams; // save request here
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
.............
//Here I do export with custom template:
public function actionReport() {
$searchModel = new LntTripsSearch();
$dataProvider = $searchModel->search($this->repquery); //restore request here
$content = $this->render('report', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
$pdf = Yii::$app->pdf;
$mpdf = $pdf->api;
$mpdf->WriteHtml($content);
return $pdf->render();
}
}
函数actionIndex()
工作正常,但actionReport()
始终打印出整个表,即使它已在actionIndex()
中过滤掉。 $this->repquery
中的变量NULL
始终为actionReport()
。
答案 0 :(得分:1)
当你调用actionReport时,新创建了控制器,然后你丢失了你保存的queryParams ..
如果你想保存这个参数不要保存在控制器中 您可以将值存储在
中$session = Yii::$app->session;
有关会话管理的详细信息,请参阅此文档http://www.yiiframework.com/doc-2.0/yii-web-session.html
http://www.bsourcecode.com/yiiframework2/session-handling-in-yii-framework-2-0/
答案 1 :(得分:0)
使用$this->repquery
您无法获得价值,因为$this
指的是当前对象
您有两种方法,两种方法都有不同的对象:
$searchModel = new LntTripsSearch();
所以,你要做的就是使用$repquery
代替$this->repquery
。
protected $repquery;
public function actionIndex()
{
$searchModel = new LntTripsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$repquery = Yii::$app->request->queryParams;
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionReport()
{
$searchModel = new LntTripsSearch();
$dataProvider = $searchModel->search($repquery);
$content = $this->render('report', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
$pdf = Yii::$app->pdf;
$mpdf = $pdf->api;
$mpdf->WriteHtml($content);
return $pdf->render();
}
答案 2 :(得分:0)
感谢您的回答。会话就是我的解决方案。
public function actionIndex()
{
$searchModel = new LntTripsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$session = Yii::$app->session;
// check if a session is already open
if (!$session->isActive){
$session->open();// open a session
}
// save query here
$session['repquery'] = Yii::$app->request->queryParams;
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionReport()
{
$searchModel = new LntTripsSearch();
// restore query using session
$dataProvider = $searchModel->search(Yii::$app->session->get('repquery'));
$content = $this->render('report', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
// create pdf
$pdf = Yii::$app->pdf;
$mpdf = $pdf->api;
$mpdf->WriteHtml($content);
return $pdf->render();
}