首次加载页面时显示组合数据

时间:2020-01-15 19:17:41

标签: yii2

下午好。 我制作了一个组合,显示一个选项的各自数据,但是只有在导航其他选项并返回第一个选项时才会加载该组合。

我需要帮助以在首次重新加载页面时显示组合数据,而无需单击其他选项。

谢谢您的帮助。

附加代码。

组合代码

$cat = ArrayHelper::map(Category::find()->all(), 'id', 'name_category');
echo $form->field($model, 'category_id')->dropDownList(
    $cat,
    [
        'prompt'=>'All the products',
        'onchange'=>'javascript:this.form.submit();',
    ]
);
?>

<table class="table table-bordered">
      <thead>
        <tr>
          <th scope="col"><center>Name</center></th>
          <th scope="col"><center>Description<center></th>
          <th scope="col"><center>Price<center></th>
        </tr>   
      </thead>

        <tbody>
        <?php

        if (isset($produc) && count($produc) > 0)
        {
            foreach ($produc as $products)
            {
        ?>

        <tr>    
          <td><center><?php echo $products->name_product ?></center></td>
          <td><center><?php echo $products->description ?></center></td>
          <td><center><?php echo $products->price ?></center></td>
        </tr>
        <?php  }}
        elseif(isset($pt)){

            foreach ($pt as $produ)
            {
        ?>

        <tr>    
          <td><center><?php echo $produ->name_product ?></center></td>
          <td><center><?php echo $produ->description ?></center></td>
          <td><center><?php echo $produ->price ?></center></td>
        </tr>
        <?php  }}
         ?>     
    </tbody>
</table>

<?php ActiveForm::end(); ?>

组合功能

public function actionCategoryProduct()
{
    $model = new Product();

        if ($model->load(Yii::$app->request->post()) && empty($model->category_id)) {

        $pt = Product::findBySql("SELECT * FROM product")->all();

        return $this->render('category-product', ['model' => $model, 'pt' => $pt]);

    }elseif ($model->load(Yii::$app->request->post()) ) {

        $produc = Product::findBySql("SELECT * FROM product WHERE category_id = ".$model->categoria_id."")
            ->all();

            return $this->render('category-product', ['model' => $model, 'produc' => $produc]);
        }
    return $this->render('category-product', ['model' => $model]);
}

0 个答案:

没有答案