Yii2使用activeCheckboxList()显示数据

时间:2017-06-10 04:33:21

标签: php yii2

全部交易,

从这yii2 docs开始,Yii2说:

A checkbox list allows multiple selection, like listBox(). 
As a result, the corresponding submitted value is an array. 
The selection of the checkbox list is taken from the value of the model attribute.

所以,我有一个这样的主数据:

$data= ArrayHelper::map(\app\models\estimator\TipeTank::find()->all(), 'id', 'nama_tipe');

Array
(
  [1] => Frame
  [2] => Beam
  [3] => Chemical
  [4] => Food
  [5] => Gas
)

现在从我的模型中,我有这个:

 $selected = $modelRepairEstimate->inspection->getLinkTipeTankToIrs()->all();

Array
(
[0] => app\models\estimator\LinkTipeTankToIr Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
            (
                [id] => 5293
                [inspection_id] => 3874
                [tipe_id] => 1
            )

        [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
            (
                [id] => 5293
                [inspection_id] => 3874
                [tipe_id] => 1
            )



    )

[1] => app\models\estimator\LinkTipeTankToIr Object
    (
        [_attributes:yii\db\BaseActiveRecord:private] => Array
            (
                [id] => 5294
                [inspection_id] => 3874
                [tipe_id] => 3
            )

        [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
            (
                [id] => 5294
                [inspection_id] => 3874
                [tipe_id] => 3
            )
    )

 )

如您所见,模型中包含tipe_id = 1tipe_id=3。 我怎样才能将它作为tipe_id 1&的复选框列表。检查3?

请告知。

1 个答案:

答案 0 :(得分:0)

你可以使用像“Select2”这样的小部件或尝试那样的

//查看

<?php $form = \yii\widgets\ActiveForm::begin()?>
<?= $form->field($model, 'id')
    ->dropDownList(\yii\helpers\ArrayHelper::map(\app\models\TipeTank::find()->asArray()->all(), 'id', 'nama_tipe'), [
            'multiple' => true,
            'options' => $selected,
        ]
    );
?>
<?php $form::end()?>

//你的控制器

public function actionSomeindex()
    {
        //some other code ...
        $selected = array(
            '1' => ['selected' => true] , //tipe_id = 1
            '3' => ['selected' => true]   //tipe_id = 3
        );
        $model = new TipeTank();
        return $this->render('someindex, [
            'model' => $model,
            'selected' => $selected,
        ]);
    }

您可以从所选检查中选择$。我不知道你的功能如何工作