使用 Yii2 过滤占位符

Filter Placeholders with Yii2

有谁知道如何在 Yii2 Framework 的 Gridview 过滤器上实现占位符或工具提示?我需要一些对用户来说很突出的东西,让他们知道文本框实际上是一个搜索过滤器。

期待听到回复。

占位符可以这样实现:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel'  => $searchModel,
    'columns'      => [
        [
            'attribute' => 'name',
            'filterInputOptions' => [
                'class'       => 'form-control',
                'placeholder' => 'Type in some characters...'
             ]
        ],
        ['class' => 'yii\grid\ActionColumn' ],
    ],
]); ?>

class 应该提供,虽然这不是必须的 - 它只是默认样式 class.

全局设置

我找到的唯一方法是在 config/web.php 中用于应用程序配置:

$config = [
    ...
    'on beforeRequest'          => function ($event) {
        Yii::$container->set('yii\grid\DataColumn', [
            'filterInputOptions' => [
                'class'       => 'form-control',
                'placeholder' => 'Type in some characters...'
            ]
        ]);
    },
    ...
];

这是一个event handler. On each request DataColumn will be configured to use the placeholder. Some detail information can be found here。现在您无需调整任何 GridView 配置即可获得占位符。当然,您也可以在处理程序中更改其他配置。

你也可以使用 tooltip/titlefilterOptions

 [
            'attribute' => 'name',
            'label' => 'labelname',
            ...
            ....
            'filterOptions' => [ 'title' => 'prova'],
        ],