在gridview yii2中使用onclick事件

时间:2019-11-04 18:36:26

标签: yii2

视图

在gridview中使用onclick事件吗?

  

错误:试图获取非对象的属性

  ['class' => 'yii\grid\ActionColumn',
                    'template' => '{view} {delete} {myButton}',
                    'buttons' => [
                        'format' => 'raw',
                        'myButton' => function ($model) {
                            return Html::a('<li class="fa fa-folder"></li> info sale', ['#'], [
                                'class' => 'btn btn-primary btn-xs',
                                'onclick'=>'saleinfo('.$model->id.')',
                            ]);
                        }
                    ]
       ],

1 个答案:

答案 0 :(得分:0)

您可以尝试

  ['class' => 'yii\grid\ActionColumn',
    'template' => '{view} {delete} {myButton}',                        
    'urlCreator' => function ($action, $model, $key, $index) {
            if ($action === 'view') {
               ....
            }
            if ($action === 'update') {
               ...
            }      
            if ($action === 'myButton') {
                $url =\yii\helpers\Url::to(['/your_controller/your_action', 'id' => $model->id]); 
                return $url;
            }                                            
    }, 
    'buttons' => [
        'myButton' => function($url, $model){
            return Html::a('<li class="fa fa-folder"></li> info sale', ['#'], [
                    'class' => 'btn btn-primary btn-xs',
                ]);
        }
    ],
  ],