M
M
Maxim2017-10-06 10:33:22
Yii
Maxim, 2017-10-06 10:33:22

Yii2 how to display multiple values ​​in Select 2 dropdown list kartik?

Hello! Please help me with a simple issue. I display data in the form in a drop-down list from another table using the Select2 kartik widget for Yii2 ,

echo $form->field($model, 'judge_id')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\backend\models\Judge::find()->where(['status'=>1])->asArray()->all(), 'id', 'last_name'),
        'language' => 'ru',
        'options' => ['placeholder' => 'Выберите судью...'],
        'pluginOptions' => [
            'allowClear' => false
        ],

which is linked via a module:
class JudgeComitet extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'judge_comitet';
    }

......

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getJudge()
    {
        return $this->hasOne(Judge::className(), ['id' => 'judge_id']);
    }

    public function getFullName()
    {
        return $this->judge->last_name.' '.$this->judge->name.' '.$this->judge->middle_name;
    }
}

It is necessary to display in the drop-down list not only the Last Name (last_name), but also the First Name (name) Middle Name (midlle_name). Is there a way to do this without using Ajax? In views I did it with a function:
public function getFullName()
{
     return $this->judge->last_name.' '.$this->judge->name.' '.$this->judge->middle_name;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-10-06
@myks92

ArrayHelper::map works great with both arrays and objects, in your case the getFullName method only works in the context of an object, so it makes no sense to select data as an array, replace
on the
PS Forming queries to the database in the View is shit code. So you better take them somewhere else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question