GridView Select2 showing data

I have an interesting problem. In a filter I need to display employees by their status (fired or active, this is stored in the DB). If you don’t type anything in the filter input, it should only show active employees. But if you start typing anything, it should show every employee, including the fired ones

show what you have attempted

1 Like

I tried to find how to add classes to filter, so I could just add d-none to them and then remove those when something is typed in the filter input, but I didn’t find how to do this

                        <?= $form->field($report, 'employee')->widget(
                            Select2::class, [
                                'data' => $report->Employee::find()->where()->all,
                                'options' => [
                                    'multiple' => true,
                        ) ?>

I tried adding ‘options’ => [‘class’ => ‘d-none’] in ‘filterWidgetOptions’. But I don’t know if it will even work, since if you press ‘select all’ those that are hidden will also be selected

I also tried adding something like this

    isset($searchModel->value_id) ?
   and so on

But it’s not going to work neither, because it is only going to work after I add something to the search model

I think I solved it
to pass status of employee to the widget, I made an array like this:

$array[$employee->id] = ['status' => $employee->status];

and then passed it to the widget:

'options' => [
    'options' => $array,

And then in pluginOptions I made a custom matcher

'pluginOptions' => [
    'matcher' => new JsExpression("function matchCustom(params, data) {}

More about those matchers: Search | Select2 - The jQuery replacement for select boxes

From your problem explanation and incomplete snippets, you just need to generate your CRUD with Gii then replace textInput with Select2

No need for further complications

1 Like

you should only doing filter like this

<?= $form->field($model, 'agen_id')->widget(kartik\select2\Select2::className(), 
[ 'data' => ArrayHelper::map(\app\models\Agen::findAll(['listed' => true, 
'role' => 'AGEN']), 'id', 'name'),
   'options' => ['placeholder' => 'Select agen'],
   'pluginOptions' => [
          'allowClear' => true,
 ]) ?>
1 Like

Thanks for the suggestions and I’m sorry for describing my problem this badly

I already had a select2 snippet as well as model and a searchModel.
I only needed to show active employees when nothing was typed in select2 input form and show all active and inactive employees that there is when select2 input contains text.

So what I really needed was modified search for a single select2 form

Use ajax load of items in select2

1 Like