【问题标题】:Yii2 array elements to display in different rows in detail viewYii2 数组元素在详细视图中显示在不同行中
【发布时间】:2016-12-11 16:27:46
【问题描述】:

在我的项目中,我有一个返回元素数组的函数。这些元素数组已使用 implode 转换为字符串,并已在详细视图中调用。在这里,我的详细视图显示了 1 行中的所有元素。 我希望每个数组元素都显示在不同行的详细视图中。

我的函数返回数组元素。

  public function getHazStatement(){
        $codes = array();
        $pLines = GhsHazStatements::find()->where(['haz_id' => $this->getHazID()])->all();
        foreach ($pLines as $pLine){
            $codes[] = $pLine->haz_statement;
            //var_dump($codes); exit();
        }
     //  var_dump(); exit();
        return implode(', ', $codes);
    }

    public function getHazCode(){
        $codes = array();
        $pLines = GhsHazStatements::find()->where(['haz_id' => $this->getHazID()])->all();
        foreach ($pLines as $pLine){
            $codes[] = $pLine->haz_code;
            //var_dump($codes); exit();
        }
     //  var_dump(); exit();
        return implode(', ', $codes);
    }

我的视图文件详细视图。

 [
                           'label' => $model->getHazCode(),
                           'value' => $model->getHazStatement(),
                           'options' => ['class' => 'table table-striped table-bordered detail-view','style'=> 'width:500px'], 
               ],

我的输出是:

在我的输出中,您可以看到同一行中有两个元素。我希望它们在详细视图中位于两个不同的行中。我怎样才能做到这一点?任何可能的解决方案?

谢谢

【问题讨论】:

  • 到底是什么问题?你不知道如何准备行数组?或者如何将行数组传递到视图中?或者如何将数组呈现为视图内的表格行?
  • 我建议您在类中声明hasManyGhsHazStatements 的关系,使用此关系获取GhsHazStatements[] 并将此数组传递到视图中。这很简单。为什么需要将 css 类名(甚至样式!)传递到视图中?这看起来是一种不好的做法
  • 我已经声明了 hasMany 关系。我有相同的函数,可以一次显示多个数组元素。而且我需要将每个数组元素拆分为单行并显示......这是我的问题。
  • 所以...这个结果(在图像上)符合您的需求,您只想在用户单击一行时显示带有拆分行元素的模式窗口?还是详细视图是另一个页面?

标签: arrays yii2 yii2-basic-app detailview


【解决方案1】:

在你的课堂上:

public YourClass extends ActiveRecord 
{
    ... 

    public function getHazStatemets()
    {
        return $this->hasMany(GhsHazStatements::className(), ['haz_id' => '<haz id column name in your class>']);
    }
}

在您的控制器中:

$this->render('<your view file name>', [
    'haz_statements' => $yourClassInstance->getHazStatements()->all(),
    'options' => ... // try to avoid passing CSS classes to from view controller
])

在你看来:

<?php foreach($haz_statement as $statement) ?>

<tr>
    <td>
        <?= $statement->haz_code ?>
    </td>
    <td>
        <?= $statement->haz_statement ?>
    </td>
</tr>

<?php endforeach; ?>

【讨论】:

    【解决方案2】:

    我没有完全理解你的问题。

    如果你想有两行和信息..:(例如)

    [
       'label' => $model->getHazCode(),
       'value' => "detail",
       'options' => ['class' => 'table table-striped table-bordered detail-view','style'=> 'width:500px'], 
    ],
    [
       'label' => $model->getHazStatement(),
       'value' => "detail",
       'options' => ['class' => 'table table-striped table-bordered detail-view','style'=> 'width:500px'], 
    ],
    

    但是如果你想要一个...的列表

    当你想要一个行列表时,这是同一个 GridView

    无论如何,我希望以下代码对您有所帮助。 请禁用显示标题(见下文)

      $query = GhsHazStatements::find()->where(['haz_id' => $this->getHazID()]);
      $dataProvider = new \yii\data\ActiveDataProvider([
         'query' => $query,
            ]);
      echo yii\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'haz_statement',
            'haz_code',
          ],
        'showHeader' => false,
      ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多