2012-10-10 18 views
9

Mam dropDownList moim zdaniem, jest wypełnianie z clients tabeli, tabela zawiera kolumny jak first_name, last_name, id itd. Teraz chcę pokaż first_name i last_name jako tekst wyświetlany i id jako wartość na liście rozwijanej. Skończyłem jako id jako wartość i first_name jako tekst wyświetlany, ale tutaj chcę połączyć te kolumny (first_name i last_name) i użyć jako tekstu wyświetlanego.W ramach Yii jak można połączyć kolumny i pokazać jak wyświetlania napisu w dropdownlist

w modelu

function getClients() 
{ 
    $Clients = Client::model()->findAll(); 
    $list = CHtml::listData($Clients , 'client_id', 'first_name'); 
    return $list; 
} 

w widoku

echo $form->dropDownList($model,'client_id',$model->getClients()); 

Odpowiedz

20

Herezje inny sposób (MySQL) W modelu

function getFullName() 
{ 
    return $this->first_name.' '.$this->last_name; 
} 

i

function getClients() 
{ 
    $Clients = Client::model()->findAll(); 
    $list = CHtml::listData($Clients , 'client_id', 'fullName'); 
    return $list; 
} 

Myślę, że to metoda trochę wielokrotnego użytku, ponieważ można używać wirtualne urządzenie attache z fullName nie tylko na liście rozwijanej, ale wszędzie potrzebne jest pełne imię i nazwisko.

+1

@Kannan, też jest fajnie :) bardziej efektownie myślę. Napisałem swój ExtHtml 2 lata temu. – Sergey

+0

Tak, to jest takie szybkie i proste, a teraz jestem zdezorientowany, aby wybrać najlepszą odpowiedź .. :) Tak czy inaczej, dzięki @DInGd0nG – nu6A

+0

po pewnym eksperymencie zdałem sobie sprawę, że ta metoda jest odpowiednia dla mojego zmienionego scenariusza. I ogólnie to jest trafne ... więc akceptuję to. – nu6A

7

1-ty wariant (Easy):

$list = array(); 
foreach ($Clients as $c) { 
    $list[$c->id] = $c->first_name . ' ' . $c->last_name; 
} 

2-ty wariant (Universal):

class ExtHtml extends CHtml { 
    public static function listData($models,$valueField,$textField,$groupField='') 
    { 
     $listData=array(); 
     if($groupField==='') 
     { 
      foreach($models as $model) 
      { 
       $value=self::value($model,$valueField); 
       if (is_array($textField)) { 
        $t = array(); 
        foreach ($textField as $field) { 
         $t[]=self::value($model,$field,$field); 
        } 
        $text=implode(' ', $t); 
       } else { 
        $text=self::value($model,$textField, null); 
        if ($text == null) { 
         if (is_callable($textField)) $text=call_user_func($textField, $model); 
         else $text = $textField; 
        } 
       } 
       $listData[$value]=$text; 
      } 
     } 
     else 
     { 
      foreach($models as $model) 
      { 
       $group=self::value($model,$groupField); 
       $value=self::value($model,$valueField); 
       if (is_array($textField)) { 
        $t = array(); 
        foreach ($textField as $field) { 
         $t[]=self::value($model,$field,$field); 
        } 
        $text=implode(' ', $t); 
       } else { 
        $text=self::value($model,$textField, null); 
        if ($text == null) { 
         if (is_callable($textField)) $text=call_user_func($textField, $model); 
         else $text = $textField; 
        } 
       } 
       $listData[$group][$value]=$text; 
      } 
     } 
     return $listData; 
    } 
    public static function value($model,$attribute,$defaultValue=null) 
    { 
     foreach(explode('.',$attribute) as $name) 
     { 
      if(is_object($model) && ($model->hasAttribute($name) || isset($model->{$name}))) 
       $model=$model->$name; 
      else if(is_array($model) && isset($model[$name])) 
       $model=$model[$name]; 
      else 
       return $defaultValue; 
     } 
     return $model; 
    } 
} 

// in model 

function getClients() 
{ 
    $Clients = Client::model()->findAll(); 
    $list = ExtHtml::listData($Clients , 'client_id', array('first_name', 'last_name')); 
    return $list; 
} 

3-ty wariant

function getClients() 
{ 
    $Clients = Client::model()->findAll(array('select' => 'concat(first_name, " ", last_name) as first_name')); 
    $list = CHtml::listData($Clients , 'client_id', 'first_name'); 
    return $list; 
} 
+0

wow !! .. thank u @Sergey, ja po drugim drodze .. dziękuję – nu6A

+1

@Kannan, w 2. sposób: if (is_callable ($ textField)) $ zobaczyć text = call_user_func ($ textField, $ model); - Możesz go użyć do uzyskania wielu wariantów z funkcją oddzwaniania – Sergey

+0

tak, widziałem to, to jest naprawdę fajne .. – nu6A

0

Spróbuj tryb kompaktowy z użyciem funkcji „anonimowy” funkcji PHP:

function getClients() 
    { 
     return CHtml::listData(Client::model()->findAll(), 'id', function($client) { return $client->first_name . ' ' . $client->last_name; }); 
    } 
0

Poprzedni komentarz o „tryb kompaktowy z funkcją anonimową” wystąpił błąd! Prawy kod jest:

function getClients() 
{ 
    $clients = Client::model()->findAll(); 
    return CHtml::listData($clients, 'id', function($clients) { return $client->first_name . ' ' . $client->last_name; }); 
}