如何在yii2中仅在标题处的ActionColumn中放置按钮

如何在yii2中仅在标题处的ActionColumn中放置按钮,yii,yii2,yii2-advanced-app,Yii,Yii2,Yii2 Advanced App,在GridView小部件中,我只想在action列下放置一次reset按钮 有什么解决方案或建议吗 感谢将按钮放入标题上的操作列,因此,请按照以下步骤操作: 在components文件夹中创建一个文件CustomActionColumn.php 把下面的代码放在上面的文件中 namespace app\components; use yii\grid\ActionColumn; use yii\helpers\Html; class CustomActionColumn extends A

在GridView小部件中,我只想在action列下放置一次reset按钮

有什么解决方案或建议吗


感谢

将按钮放入标题上的
操作列
,因此,请按照以下步骤操作:

  • 在components文件夹中创建一个文件CustomActionColumn.php

  • 把下面的代码放在上面的文件中

    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']);
         }
    }
    
  • 现在在Gridview小部件中使用
    CustomActionColumn
    而不是
    ActionColumn

     [
       'class' => 'app\components\CustomButton',
     ],
    
  • 完成了

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

    要将按钮放入标题上的
    操作列中,请执行以下步骤:

  • 在components文件夹中创建一个文件CustomActionColumn.php

  • 把下面的代码放在上面的文件中

    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']);
         }
    }
    
  • 现在在Gridview小部件中使用
    CustomActionColumn
    而不是
    ActionColumn

     [
       'class' => 'app\components\CustomButton',
     ],
    
  • 完成了

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

    设置ActionColumn的标题属性:

        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '<div class="pull-right" >{update}{delete}</div>',
            'header' => '<button>Button</button>'
        ]
    
    然后在网格定义中设置过滤器:

    [
       'class' => 'CustomActionColumn',
       'template' => '<div class="pull-right" >{update}{delete}</div>',
       'filter' => '<button>Button</button>'
    ]
    
    [
    'class'=>'CustomActionColumn',
    '模板'=>'{update}{delete}',
    '过滤器'=>'按钮'
    ]
    
    设置ActionColumn的标题属性:

        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '<div class="pull-right" >{update}{delete}</div>',
            'header' => '<button>Button</button>'
        ]
    
    然后在网格定义中设置过滤器:

    [
       'class' => 'CustomActionColumn',
       'template' => '<div class="pull-right" >{update}{delete}</div>',
       'filter' => '<button>Button</button>'
    ]
    
    [
    'class'=>'CustomActionColumn',
    '模板'=>'{update}{delete}',
    '过滤器'=>'按钮'
    ]
    
    GridView::widget([
    “dataProvider”=>$dataProvider,
    “filterModel”=>$searchModel,
    'emptyCell'=>Html::a('Reset'、['index']、['class'=>'btn btn primary btn xs','style'=>'边距:2px;']),
    ]);
    
    使用emptyCell设置按钮…

    GridView::widget([
    “dataProvider”=>$dataProvider,
    “filterModel”=>$searchModel,
    'emptyCell'=>Html::a('Reset'、['index']、['class'=>'btn btn primary btn xs','style'=>'边距:2px;']),
    ]);
    

    使用emptyCell设置按钮…

    显示按钮和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

  • 在你看来

    use app\components\FilterActionColumn;
    
  • 替换默认的GridView操作列

    [
        '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
        // ..   
    ],
    

  • 您可以在这里看到扩展示例,即,如果需要多个按钮来同时显示按钮和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

  • 在你看来

    use app\components\FilterActionColumn;
    
  • 替换默认的GridView操作列

    [
        '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
        // ..   
    ],
    

  • 您可以在这里看到扩展示例,即,如果您需要多个按钮,我认为您需要覆盖ActionColumn的renderFilterCellContent方法来实现这一点。对于yii1,你可以参考我的WP帖子来了解如何实现它。我认为你需要覆盖ActionColumn的renderFilterCellContent方法来实现这一点。对于yii1,你可以参考我的WP帖子来了解如何实现。然后你需要创建GAMITG回答的自定义ActionColumn类和overide renderFilterCell方法。然后你需要创建GAMITG回答的自定义ActionColumn类和overide renderFilterCell方法。你应该尝试
    ['header'=>'Action','class'=>'app\components\CustomButton',],
    您应该尝试
    ['header'=>'Action','class'=>'app\components\CustomButton',],
    这将用按钮填充gridview的所有空单元格!这将用按钮填充gridview的所有空单元格!