2016-04-08 19 views
1

Yii2'de bir uygulama geliştiriyorum. Ben aynı sonucu yazdırmak istiyorum "SingleView" görünümü dosyasındaYii2: Güncelleme/ekleme formunda dropDown adları nasıl görüntülenir?

<!-- 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'); 
     ?> 

: Aşağıdaki açılan yapısı ile formunu yaptı.

<?= 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] 
     ], 
    ]) ?> 

ben değer bölümüne bir işlevi yazmak çalıştı ama bana bir hata verdi: "HTML özel karakter bir nesne girişi var".

Bunu yapmanın en basit yolu nedir?

+0

'Ben değer bölümüne bir işlevi yazmak çalıştı ama bana bir error' verdi - verebilir size Bu işlevin kodunu gönderilsin mi? –

cevap

1

İşlev yapılamıyor. Ama son değer atanabilir:

<?= 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") 
     ] 
    ], 
]) ?> 

iyi çözüm: Bir kendi modeli yöntem haline dize oluşturulmasını koyabilirsiniz Tabii

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") 
    } 
    .... 
} 

veya Bir yardımcı işlevinde somehwere. Bu yöntem şeklinde yeniden olabilir:

<?= $form->field($model, 'kontener_tipus_id') 
    ->dropDownList(
     ArrayHelper::map(KontenerTipus::find()->AsArray()->all(), 'id', 'konténerTípus') 
    ) 
    ->label('Konténer típus'); 
?> 

ve tek model görünümüne

:

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', 
    ], 
]) 
+0

Teşekkür ederim, verdiğiniz talimatları izleyerek ve anlatarak görünüm dosyasında bazı değişiklikler yaptım. – Gabesz

+0

@Gabesz Cevabımı güncelledim. Bunun sizin için anlamlı olup olmadığını bir göz atın. – robsch