Reputation: 4261
This is my dropdownlist
like..
<?= $form->field($queModel, 'qm_category_id')->dropdownList(QuestionCategory::getQuestionCat()) ?>
This is getQuestionCat()
function
public static function getQuestionCat()
{
$dataTmp = self::find()->all();
$result = yii\helpers\ArrayHelper::map($dataTmp, 'qc_id', 'qc_name', 'qcCourse.course_name');
print_r($result); exit;
return $result;
}
Above function return this array using ArrayHelper
show below image like..
In this dropdownlist the first <optgroup label="">
set to <optgroup label="Other">
Upvotes: 0
Views: 1762
Reputation: 818
The map
function accepts closures, so you could use the following:
$result = \yii\helpers\ArrayHelper::map($dataTmp, 'qc_id', 'qc_name', function($model) {
if ($course_name = $model->qcCourse->course_name) {
return $course_name;
}
else {
return 'Other';
}
});
Upvotes: 1