Yii2:如何在 Kartik 展开行列小部件中按 id 显示数据


Yii2: how to display data by id in Kartik Expand Row Column Widget

使用 Kartik 展开行列小部件在折叠行中显示数据, 我想按该特定记录的 id 显示数据,

行动索引()

public function actionIndex() {
        $searchModel = new CreateBookingsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $model = new CreateBookings();
        $data = CreateBookings::findOne($id);
        if (Yii::$app->request->post('hasEditable')) {
           $id = Yii::$app->request->post('editableKey');
           $model = CreateBookings::findOne($id);
           $out = Json::encode(['output'=>'', 'message'=>'']);
           $posted = current($_POST['CreateBookings']);
           $post = ['CreateBookings' => $posted];
           if ($model->load($post)) {
             $model->save();
             $output = '';
           }
           echo $out;
           return;
       }
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'model' => $model,
            'data' => $data,
        ]);
 }

部分视图 :

<?php echo Html::encode($data->check_in);?>
<?php echo Html::encode($data->check_out);?>

网格视图

'columns' => [
                [
                'class' => 'kartik'grid'ExpandRowColumn',
                'value' => function ($model,$key,$index,$column) {
                    return GridView::ROW_COLLAPSED;
                },
                'detail' => function ($model,$key,$index,$column) {
                    $searchModel = new CreateBookingsSearch();
                    $searchModel->booking_id = $model ->id;
                    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
                    return Yii::$app->controller->renderPartial('_expandrowview.php',[
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,
                    ]);
                },
            ],

当我在索引中传递$data = CreateBookings::findOne($id);时出现错误Undefined variable: id。我需要类似索引public function actionView($id)的东西public function actionIndex($id)需要在索引中传递id

你可以试试这个(使用detailUrl)
在网格视图中(在任意位置添加列)

      .  
      .
      'ExpandRowColumn'=>  
     [  
       'class' =>''kartik'grid'ExpandRowColumn',  
       'value'=>function ($model, $key, $index,$column) {  
                 return GridView::ROW_COLLAPSED;  
        },  
       'detailUrl'=> Yii::$app->request->getBaseUrl().'..../expand-view',
     ], 
     .
     .

这里:detailUrl 将是格式为 .. 的 URl。/控制器名称/展开视图

在控制器中

    public function actionExpandView()  
        {  
         if(isset($_POST['expandRowKey'])) {  
             $model = ModelName::findOne($_POST['expandRowKey']);  
             $variable= $anydata; //anydata want to send in expanded view  
             return $this->renderPartial('_expandrowview.php',['anydata'=>$anydata,'model'=>$model]);  
        }  
        else  
        {  
           return '<div class="alert alert-danger">No data found</div>';  
        }  
        }  
    >>>expandRowKey is the first_column_data(may be id) of your model or table  
    *** you can send anydata using this method and can fetch data using simple php  
    print_r($anydata);  code
    on _expandrowview.php (view file) to make changes according to need..

对不起,但是我使用基于视图的模型,$_POST['expandRowKey'] 总是返回 NULL。