1

I have select2

return $this->form->field($this->model, 'observers')
            ->widget(Select2::className(),
                [
                    'data' => Tasks::getAllStaffsGroupOffice(),
                    'disabled' => !$this->can['changeObservers'],
                    'options' => [
                        'multiple' => true,
                        'value' => ArrayHelper::map($this->model->observers, 'staff_id', 'staff_id'),
                        'placeholder' => Yii::t('tasks_forms', 'FORM_PLACEHOLDER_CHOOSE'),
                        'class' => 'hiddenInput'
                    ],
                    'pluginOptions' => [
                        'allowClear' => true,
                        'closeOnSelect'=> false,

                    ],
                    'pluginLoading' => false,
                ]);

Tasks::getAllStaffsGroupOffice() geting users array by offices. Example ->

 array:4 [▼
  "main office" => array:1 [▼
    2 => "123 123"
  ]
  "office 1" => array:3 [▼
    3 => "staff_1"
    6 => "staff_2"
    2 => "123 123"
  ]
  "office 3" => array:2 [▼
    4 => "staff_3"
    3 => "staff_1"
  ]
  "office 2" => array:2 [▼
    5 => "staff_4"
    3 => "staff_1"
  ]
]

select2 value example -> array (2 => "2")

As a result, the display of the widget itself looks like this select2 value

How to make it so that the staff which is in 2 and more offices is displayed only 1 time?

1 Answers1

0

I believe you should filter your staff list and then pass it to Select2 widget. My approach for creating a unique array of staff is like this:

$allStaffsGroupOffice = [
    "main office" => [
        2 => "123 123"
    ],
    "office 1" => [
        3 => "staff_1",
        6 => "staff_2",
        2 => "123 123"
    ],
    "office 3" => [
        4 => "staff_3",
        3 => "staff_1"
    ],
    "office 2" => [
        5 => "staff_4",
        3 => "staff_1"
    ]
];
$repeatedStaff = [];
$newUniqueList = [];
foreach ($allStaffsGroupOffice as $office => $staffList) {
    foreach ($staffList as $staffId => $staffName) {
        if (!in_array($staffId, $repeatedStaff)) {
            $repeatedStaff[] = $staffId;
            $newUniqueList[$office][$staffId] = $staffName;
        }
    }
}

$newUniqueList will be your new array which contains offices with unique staff.

Ahmad_kh
  • 151
  • 1
  • 6
  • the problem is that the same staff can be present in 2 or more different offices, and in select2 widget staff should be displayed in those offices where it is. and so that you can choose it from any office – Dima Sidorenko Dec 02 '21 at 14:01