2012-10-10 10 views
9

Ho un dropDownList a mio avviso, è popolato da clients tavolo, la tabella contiene colonne come first_name, last_name, id ecc, Ora voglio mostra first_name e come testo visualizzato nell'elenco a discesa, ho finito con id come valore e first_name come testo visualizzato, ma qui voglio combinare quelle colonne (first_name e last_name) e utilizzare come testo di visualizzazione.Nel quadro Yii come posso combinare colonne e spettacolo come stringa di visualizzazione in DropDownList

nel modello

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

in vista

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

risposta

20

Heres un altro metodo Nel modello

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

e

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

Penso che questo sia il metodo un po riutilizzabile dato che è possibile utilizzare l'attributo virtuale fullName ute non solo in dropdownlist ma ovunque è necessario un nome completo.

+1

@Kannan, è bello anche :) più efficacemente penso. Ho scritto il mio ExtHtml 2 anni fa. – Sergey

+0

Sì, questo è così veloce e semplice, e ora sono confuso per selezionare la migliore risposta .. :) comunque grazie @ dInGd0nG – nu6A

+0

dopo alcuni esperimenti ho capito che questo metodo è adatto al mio scenario cambiato. E per generale questo è adatto ... quindi accetto questo. – nu6A

7

prima variante (Easy):

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

seconda variante (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; 
} 

terza variante (MySQL)

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, mi ha seguito il secondo modo, .. grazie mille – nu6A

+1

@Kannan, nel 2 ° modo: se (is_callable ($ textField)) $ text = call_user_func ($ textField, $ model); - Puoi usarlo per ottenere molte varianti con la funzione di callback – Sergey

+0

sì, ho visto che, questo è davvero fantastico .. – nu6A

0

provare questa modalità compatta con la funzione "funzione anonima" di PHP:

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

commento precedente con "modalità compatta" con funzione anonima presenta un errore! codice giusto è:

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