【问题标题】:yii2 multiple select with imageyii2带图像的多选
【发布时间】:2017-11-02 10:13:57
【问题描述】:

我使用了带有图像的 Yii2 多选下拉菜单,它在创建时工作正常,但在更新时没有显示选择的值... 表格:

<?php
    $allProducts = Product::find()->where('active = 1')->all();
    $prArr = array();
    if ($allProducts) {
        foreach ($allProducts as $allProduct) {
            echo '<option value="' . $allProduct->id . '" style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\'' . $allProduct->getThumb() . '\');background-repeat: no-repeat;background-size: 65px auto;">' . $allProduct->title . '</option>';
        }
    }
?>

控制器:

$oldRels = ProductRelated::find()->where('main_product_id = :main_product_id', ['main_product_id' => $model->id])->all();
if ($oldRels) {
    foreach ($oldRels as $oldRel) {
        $oldRel->delete();
    }
}
if (isset($_POST['relProducts']) and ! empty($_POST['relProducts'])) {
    foreach ($_POST['relProducts'] as $relProduct_id) {
        $relProduct = new ProductRelated;
        $relProduct->main_product_id = $model->id;
        $relProduct->rel_product_id = $relProduct_id;
        $relProduct->save(false);
    }
}

当我更新我的记录时,如何在带有图像的下拉列表中显示多选值?

【问题讨论】:

  • 您必须在选项中添加选定的属性

标签: php jquery yii yii2 multiple-select


【解决方案1】:

你可以试试这个:

 <select id="relProductSelect" name="relProducts[]" multiple>
     <?php 
     $allProducts = Product::find()->where('active = 1')->all();
     $arrRelatedProducts = ArrayHelper::map(ProductRelated::find()->where('main_product_id = :main_product_id', ['main_product_id' => $model->id])->all(), 'rel_product_id', 'rel_product_id');
     if($allProducts){
       foreach($allProducts as $allProduct){
        if(in_array($allProduct->id, $arrRelatedProducts)){
            echo '<option value="'.$allProduct->id.'" selected style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\''.$allProduct->getThumb().'\');background-repeat: no-repeat;background-size: 65px auto;">'.$allProduct->title.'</option>';    
        }else{
          echo '<option value="'.$allProduct->id.'" style="color: #000; height: 50px; padding-left: 70px;padding-top: 15px;background-image: url(\''.$allProduct->getThumb().'\');background-repeat: no-repeat;background-size: 65px auto;">'.$allProduct->title.'</option>';
                                        }
                                }
                        }
                ?>
                </select>

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2023-02-13
    相关资源
    最近更新 更多