【问题标题】:kartik\Select2 as filter input in yii2\gridkartik\Select2 作为 yii2\grid 中的过滤器输入
【发布时间】:2016-08-24 13:44:04
【问题描述】:

我的 Yii2 项目遇到了另一个虚拟问题。我的视图中有一个标准的 gridView,定义如下:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => '{items}{summary}',
        'columns' => [
            [
                'class' => 'yii\grid\SerialColumn',
                'contentOptions' => [
                    'style' => 'vertical-align: middle;'
                ]
            ],
            [
                'attribute' => 'name',
            ],
            [
                'attribute' => 'type',
                'value' => function($model){
                    /* @var $model app\models\Object */
                    return $model->typeNames()[$model->type];
                },
                'filter' => Select2::widget([
                    'name' => 'ObjectSearch[type]',
                    'data' => Object::typeNames(),
                    'theme' => Select2::THEME_BOOTSTRAP,
                    'hideSearch' => true,
                    'options' => [
                        'placeholder' => 'Wybierz typ obiektu...',
                        'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null
                    ]
                ]),
            ],
            [
                'attribute' => 'countryId',
                'value' => 'country.name',
                'filter' => Select2::widget([
                   'name' => 'ObjectSearch[countryId]',
                   'data' => Country::forWidgets(),
                   'theme' => Select2::THEME_BOOTSTRAP,
                   'options' => [
                       'placeholder' => 'Wybierz państwo...'
                   ]
                ]),
             ],
  //other columns
        ],
    ]); ?>

我已经定义了一个 searchModel 类:

class ObjectSearch extends Object
{
    public function rules()
    {
        return [
            [['type'], 'integer'],
            [['name', 'city', 'countryId'], 'string']
        ];
    }

    //some other functions

    public function search($params)
    {
        $query = Object::find()->where(['userId' => Yii::$app->user->id]);
        $query->joinWith('country');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->sort->attributes['countryId'] = [
            'asc' => ['countries.name' => 'ASC'],
            'desc' => ['countries.name' => 'DESC']
        ];

        if (!$this->load($params) && $this->validate()) {
            return $dataProvider;
        }

        $query->andFilterWhere(['like', 'objects.name', $this->name])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['=', 'objects.countryId', $this->countryId])
            ->andFilterWhere(['=', 'type', $this->type]);

        return $dataProvider;
    }
}

排序和搜索工作正常 - 我得到了正确的结果。那我的问题是什么?对于标准列,当我在 textInput 中键入一些文本时,此输入的值会在搜索后保留在其中。但是当我在 Select2 小部件搜索工作中选择一些值时,但搜索后所选值消失了,我只有一个占位符。

感谢您的帮助,
卡米尔

【问题讨论】:

    标签: php select2 yii2


    【解决方案1】:

    您应该简单地使用 initValueText 参数:

    initValueText: 字符串,在 Select2 小部件中显示的文本作为初始值。

    例如:

    Select2::widget([
        'name' => 'ObjectSearch[type]',
        'data' => Object::typeNames(),
        'initValueText' => $searchModel->type,
        // ... other params
    ])
    

    您也可以将其用作 InputWidget :

    Select2::widget([
        'model' => $searchModel,
        'attribute' => 'type',
        'data' => Object::typeNames(),
        // ... other params
    ])
    

    【讨论】:

    • 第一个解决方案不起作用,但第二个我实现了我想要的;)谢谢!
    • 这两种解决方案都非常有效!谢谢一百万。
    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 2020-04-14
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多