Yii2:如何在Kartik扩展行列控件中按id显示数据

3npbholx  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(131)

使用KartikExpandRowColumnWidget在折叠的行中显示数据,我希望按特定记录的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

3phpmpom

3phpmpom1#

您可以尝试此操作(使用detailUrl)
在网格视图中(在任意位置添加列)

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

此处:detailUrl将是格式为../controllerName/expandView的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..
bz4sfanl

bz4sfanl2#

很抱歉,我使用基于视图的模型,并且$_POST ['expandRowKey']始终返回NULL。

相关问题