0

在我的 yii2 项目中,我想为我的图像获取复选框,以便按图像搜索。每当用户选中复选框并提交按钮时,它应该只显示那些选中的图像的内容。

我的复选框列表

  <?php $img = ArrayHelper::map(app\models\GhsPictogram::find()->all(), 'pictogram_id', 'pictogram_filepath') ?>

  <?= $form->field($model, 'ghsPictogram',['template'=>'<div class="ghs-pict"> {label}{input} </div>'])->checkboxList($model->imageList($img)) ?>

我的模型中的 imageList 方法

   public function imageList($filenames) {
        $imageList = [];
        foreach ($filenames as $key => $value) {
            $imageList[$key] = Html::img(
                            Yii::$app->request->baseUrl . 'web' . $value,
                            '', array("style"=>"width: 50px")
            );
        }//foreach $filenames
        return $imageList;  
     }

但在我的表单中,它不显示图像。它改为显示图像 src。

我附上了我的输出: 点击这里

帮助我找到如何在搜索表单字段中显示图像。谢谢

4

1 回答 1

1

复选框列表有一个名为 encode 的选项,它默认为 true 并且它对传递给它的 html 进行编码(如它所说)。

要解决它,只需添加'encode' => true到 checkboxList 选项属性,如下所示:

<?= $form->field($model, 'ghsPictogram',['template'=>'<div class="ghs-pict"> {label}{input} </div>'])->checkboxList($model->imageList($img), [
    'encode' => false
]) ?>
于 2016-10-03T11:40:08.470 回答