使用自定义模板和过滤数据将 Yii2 gridview 导出为 pdf

Yii2 gridview export to pdf with custom template and filtered data

我正在尝试将我的 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() 总是打印出完整的 table 即使它在 actionIndex() 中被过滤了。 actionReport() 中的变量 $this->repquery 总是 NULL

当你调用 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/

您不会使用 $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();
}

感谢您的回答。 Session 是我的解决方案。

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();
}