79625236

Date: 2025-05-16 13:03:55
Score: 0.5
Natty:
Report link

Bumped on this thread. This site helped me a lot. The 2nd return is the correct format for the optgroup of kartik select2. https://demos.krajee.com/widget-details/depdrop#depdrop-action

// inside the controller
public function actions()
{
    return \yii\helpers\ArrayHelper::merge(parent::actions(), [
        'subcategory' => [
            'class' => \kartik\depdrop\DepDropAction::className(),
            'outputCallback' => function ($selectedId, $params) {
                return [
                    [
                        'id' => 1,
                        'name' => 'Car',
                    ],
                    [
                        'id' => 2,
                        'name' => 'bike',
                    ],
                ];
 
                // if using with optgroup return something like below instead of above
                return [
                    'group1' => [
                        ['id' => '', 'name' => ''],
                        ['id' => '', 'name' => '']
                    ],
                    'group2' => [
                        ['id' => '', 'name' => ''],
                        ['id' => '', 'name' => '']
                    ]
                ];
            }
        ]
    ]);
}

Happy coding!

Reasons:
  • Blacklisted phrase (1): helped me a lot
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Marie A