如何在yii2的gridView中创建自定义ActionColumn? [英] How to create a custom ActionColumn in the gridView of yii2?
问题描述
我有一个 gridView,我设法让它包含我需要的数据,但接下来我需要做的是创建一个列,其中包含两个用于 has_facebook 和 has_twitter 的按钮.
=GridView::widget(['dataProvider'=>$dataProvider,'filterModel' =>$searchModel,'列' =>[['class'=>'yii\grid\SerialColumn'],'姓名','cm_name','has_facebook','has_twitter',['class'=>'yii\grid\ActionColumn'],],]);?>
姓名 |cm_name |平台
帐户 1 |杰克|btn1 btn2
其中 btn1 和 btn2 指的是 facebook 和 twitter.
对不起,桌子被毁了.
您不需要创建自己的列类.您可以创建简单的原始列并在其中显示您想要的任何内容:
<预><代码>['属性' =>'some_title','格式' =>'生的','价值' =>功能($模型){返回 '这个功能
function ($model) {返回 ''.$model->id.'和其他 html 代码</div>';} 命名回调函数.有核心方法 evaluateExpression in CComponent:
公共函数evaluateExpression($_expression_,$_data_=array()){if(is_string($_expression_)){提取($_data_);return eval('return '.$_expression_.';');}别的{$_data_[]=$this;返回 call_user_func_array($_expression_, $_data_);}}
在我们的例子中,表达式不是字符串,它是一个函数,所以它运行 php 方法 call_user_func_array 并将您的模型传递给它.
I have a gridView and i managed to get it to contain the data i need, but what i need to do next is to create a column which contains two buttons for has_facebook and has_twitter.
<?=
GridView::widget([
'dataProvider'=>$dataProvider,
'filterModel' =>$searchModel,
'columns' =>[
['class'=>'yii\grid\SerialColumn'],
'name',
'cm_name',
'has_facebook',
'has_twitter',
['class'=>'yii\grid\ActionColumn'],
],
]);
?>
name | cm_name | platforms
account1 | jack | btn1 btn2
where btn1 and btn2 refer to facebook and twitter.
sorry for the disfigured table.
解决方案 You don't need to create own column Class. You can create simple raw-column and show there anything you want:
[
'attribute' => 'some_title',
'format' => 'raw',
'value' => function ($model) {
return '<div>'.$model->id.' and other html-code</div>';
},
],
This function
function ($model) {
return '<div>'.$model->id.' and other html-code</div>';
}
names callback function. There is core method evaluateExpression in CComponent:
public function evaluateExpression($_expression_,$_data_=array())
{
if(is_string($_expression_))
{
extract($_data_);
return eval('return '.$_expression_.';');
}
else
{
$_data_[]=$this;
return call_user_func_array($_expression_, $_data_);
}
}
in our case expression is not string, it's a function, so it runs php method call_user_func_array and pass into it your model.
这篇关于如何在yii2的gridView中创建自定义ActionColumn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文