我正在Yii2中开发一个应用程序。我用以下下拉结构制作了表格:
<!-- Konténer típus -->
<?= $form->field($model, 'kontener_tipus_id')
->dropDownList(
ArrayHelper::map(KontenerTipus::find()->AsArray()->all(), 'id',
function($model, $defaultValue) {
if ($model['tipus'] == "N") { $tipus = "Nyitott"; } else { $tipus = "Zárt"; };
if ($model['forma'] == "S") { $forma = "Szimmetrikus forma"; } else { $forma = "Aszimmetrikus forma"; };
if ($model['lancos'] == 0) { $lancos = "Láncos"; } else { $lancos = "Nem láncos"; };
if ($model['teto'] == "B") { $teto = "Billenő tetős"; } else { $teto = "Pille tetős"; };
return $tipus . " - " . $forma . " - " . $lancos . " - " . $teto;
}
)
)
->label('Konténer típus');
?>
在"SingleView"视图文件中,我想打印出相同的结果。
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'kod',
'nev',
'cim',
'kihelyezes_datuma',
'lng',
'lat',
['attribute' => 'sajat', 'value' => $model->getSajat()],
//Konténer típushoz tartozó tulajdonságok megjelenítése
['attribute' => 'kontener_tipus_id', 'value' => 11]
],
]) ?>
我试图在value部分写一个函数,但它给了我一个错误:"HTML特殊字符得到了一个对象输入"。
你认为最简单的方法是什么?
不允许使用函数。但最终值可以指定:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'kod',
'nev',
'cim',
'kihelyezes_datuma',
'lng',
'lat',
['attribute' => 'sajat', 'value' => $model->getSajat()],
//Konténer típushoz tartozó tulajdonságok megjelenítése
['attribute' => 'kontener_tipus_id', 'value' =>
($model['tipus'] == "N" ? "Nyitott" : "Zárt") . ' - ' .
($model['forma'] == "S" ? "Szimmetrikus forma" : "Aszimmetrikus forma") . ' - ' .
($model['lancos'] == 0 ? "Láncos" : "Nem láncos") . ' - ' .
($model['teto'] == "B" ? "Billenő tetős" : "Pille tetős")
]
],
]) ?>
更好的解决方案:
当然,你可以把字符串的创建放到自己的模型方法中
class KontenerTipus extends Model // or ActiveRecord....
...
public function getKonténerTípus() {
return
($this->tipus == "N" ? "Nyitott" : "Zárt") . ' - ' .
($this->forma == "S" ? "Szimmetrikus forma" : "Aszimmetrikus forma") . ' - ' .
($this->lancos == 0 ? "Láncos" : "Nem láncos") . ' - ' .
($this->teto == "B" ? "Billenő tetős" : "Pille tetős")
}
....
}
或者在其他地方的帮助器功能中。这种方法可以以以下形式重复使用:
<?= $form->field($model, 'kontener_tipus_id')
->dropDownList(
ArrayHelper::map(KontenerTipus::find()->AsArray()->all(), 'id', 'konténerTípus')
)
->label('Konténer típus');
?>
在单一模型视图中:
DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'kod',
'nev',
'cim',
'kihelyezes_datuma',
'lng',
'lat',
['attribute' => 'sajat', 'value' => $model->getSajat()],
//Konténer típushoz tartozó tulajdonságok megjelenítése
'konténerTípus',
],
])