M
M
Max DangerPro2016-11-24 17:32:38
Yii
Max DangerPro, 2016-11-24 17:32:38

How to display data from Widget in Html::dropDownList?

Tell me how can I pass a menu with categories that are in the widget (called MenuWidget).
The widget creates a tree menu based on the database, and generates an html template for output.
I need to transfer this widget with categories to dropDownList in order to implement mass transfer of articles. I will transfer to any of them and I need to know which one to transfer to. Accordingly, you need: Value (this is the 'id' field) and the name of the category ('name').

<?=Html::beginForm(['posts/bulk'],'post',['class' => 'form-group']);?><br>
    <?=Html::dropDownList('action','',[c'=>'Confirmed',
                // Вот сюда нужно как то вывести
                ])?>
    <?=Html::submitButton('Сохранить', ['class' => 'btn btn-warning btn-xs',]);?><br><br>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            [ 'class' => yii\grid\CheckboxColumn::className(),],
            'id',
            'name',
            ...
            'description',
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?= Html::endForm();?>

I am passing the template call here
<?=Html::dropDownList('action','',[ ''=>'Категория из меню: ',
        app\components\MenuWidget::widget(['tpl' => 'select_book', 'model' => $model])
])?>

Categories display template:
<option value="<?=$category['id'] ?>"
    <? if($category['id'] == $this->model->category_id) echo ' selected' ?>>
    <?=$tab . $category['name'] ?>
</option>
<?php if(isset($category['childs']) ): ?>
    <?=$this->getMenuHtml($category['childs'], $tab . '   - ') ?>
<?php endif;?>

This output results in:
<select class="form-control " name="action">
    <option value="0">
        <option value="1">Название 1</option>
        ...
        <option value="1">Название 10</option>
    </option>
</select>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-11-24
@DangerPro

Good evening.
Here are two examples from the official documentation

<?= Html::dropDownList('list', $currentUserId, ArrayHelper::map($userModels, 'id', 'name')) ?>
<?= Html::activeDropDownList($users, 'id', ArrayHelper::map($userModels, 'id', 'name')) ?>

Try to pass the model of your categories in the same way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question