如何在yii2中只在表头的ActionColumn中放置按钮

yhuiod9q  于 2022-11-09  发布在  其他
关注(0)|答案(4)|浏览(159)

我只想在GridView小部件中的操作列下放置一次重置按钮。

有什么解决办法或建议吗?
谢谢

s4n0splo

s4n0splo1#

设置ActionColumn的表头属性:

[
        'class' => 'yii\grid\ActionColumn',
        'template' => '<div class="pull-right" >{update}{delete}</div>',
        'header' => '<button>Button</button>'
    ]
  • 更新日期:*

正如已经回答过,您可以创建自定义列,且可以仅添加其他筛选器属性,以便可以为每个网格自定义此属性。

class CustomActionColumn extends yii\grid\ActionColumn
{
     public $filter = "";
     protected function renderFilterCellContent()
     {
        return $this->filter;
     }
}

然后在网格定义中设置过滤器:

[
   'class' => 'CustomActionColumn',
   'template' => '<div class="pull-right" >{update}{delete}</div>',
   'filter' => '<button>Button</button>'
]
xyhw6mcr

xyhw6mcr2#

要将按钮放在顶盖上的ActionColumn中,请执行以下步骤:
1.在组件文件夹中创建一个文件CustomActionColumn.php
1.将以下代码放入以上文件

namespace app\components;

use yii\grid\ActionColumn;
use yii\helpers\Html;

class CustomActionColumn extends ActionColumn
{
     protected function renderFilterCellContent()
     {
        return Html::button('Reset', ['class' => 'btn btn-primary']);
     }
}

1.现在,在Gridview小部件中使用CustomActionColumn,而不是ActionColumn
就像

[
   'class' => 'app\components\CustomButton',
 ],

1.好了,好了

**注意:-**使用这些步骤,您可以只在标题中显示按钮。

lawou6xi

lawou6xi3#

要同时显示按钮和ActionColumn标签,你可以用你自己的类覆盖yii\grid\ActionColumn renderFilterCellContent()方法,如下所示:

namespace app\components;

class FilterActionColumn extends ActionColumn
{
    public $filterContent;

    /**
    * Renders the filter cell content.
    * The default implementation simply renders a blank space.
    * This method may be overridden to customize the rendering of the filter cell (if any).
    * @return string the rendering result
    */
    protected function renderFilterCellContent()
    {
        return $this->filterContent;
    }
}

然后,您就可以将标签和按钮添加到GridView中,这样就可以替换默认ActionColumn
1.在你看来

use app\components\FilterActionColumn;

1.替换默认GridView ActionColumn

[
    'class' => FilterActionColumn::className(),
    // Add your own filterContent
    'filterContent' => Html::a('Your button', ['some/url'], [
        'class' => 'btn btn-default', 'title' => 'Some btn title',
    ]),
    'header'=> 'Your label',
    // Another ActionColumn options
    // ..   
],

您可以在此处看到扩展示例,例如,如果您需要多个按钮https://github.com/nick-denry/yii2-filter-action-column

wi3ka0sx

wi3ka0sx4#

GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'emptyCell' => Html::a('<i class="fa fa-refresh"></i>&nbsp;Reset', ['index'], ['class' => 'btn btn-primary btn-xs', 'style' => 'margin: 2px;']),
                    ]);

使用emptyCell设置按钮...

相关问题