如何在yii2 katrik gridview中使用屏蔽输入或下拉列表?

时间:2016-10-25 12:42:51

标签: php yii2

我有这样的蒙面输入:

    <?= $form->field($model, 'ip')->widget(\yii\widgets\MaskedInput::className(),[
                                                    'clientOptions' => [
                                                        'alias' => 'ip',
                                                        'groupSeparator' => '.',
                                                        'autoGroup' => true,
                                                    ],
                                                ]);
                                             ?>

我的索引文件中有一个带有可编辑字段(ip)的kartik gridview:

<?=    GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'pjax' => true,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'class' => 'kartik\grid\EditableColumn',
            'attribute' => 'ip',
        ],    
        'objectID',
        'ownerID',
        'subnetID',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

如果在gridview中用户更新ip字段时如何设置屏蔽输入? 还有我应该如何使用dropdownlist来更新字段?

1 个答案:

答案 0 :(得分:2)

您可以在&#34;过滤器&#34;中使用MaskedInput小部件。上下文

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => true,
'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
        'attribute' => 'ip',
        'filter'=> MaskedInput::widget([
            'name'=>'MyFilterModelName[ip]',
            'clientOptions' => [
                 'alias' => 'ip',
             ],
        ]);
    ],    
    'objectID',
    'ownerID',
    'subnetID',

    ['class' => 'yii\grid\ActionColumn'],
],

]); ?&GT;

否则,作为行字段,您可以传递MaskedInput小部件作为匿名函数的结果:

<?=    GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => true,
'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
    [
        'attribute' => 'ip',
        'value' => function($model) {
             return MaskedInput::widget([
                'name'          => 'ip_' . $model->id,
                'value'         => $model->ip,

                'clientOptions' => [
                     'alias' => 'ip',
                ],
            ])
        },
    ],    
    'objectID',
    'ownerID',
    'subnetID',
    ['class' => 'yii\grid\ActionColumn'],
],

]); ?&GT;

InputMask与Yii2集成,具有高度可配置性。有关提示和技巧,请herehere