2013-12-20 189 views
1

我必須創建一個複選框列表,它將打印除ids以外的所有列,數據庫中的表稱爲parametros,其中id和parametro_id不必是打印,但是將會有更多的列將由應用程序創建,這些應用程序只有1-0或true-false的數據。Yii,創建複選框列表和每個複選框將更改列數據

我不知道如何複選框列表工作,並一直試圖找到一些地方,可以解釋所有可以生成的方式,即時通訊將顯示由gii,模型和行動控制器產生的形式。

_form.php這個

<?php 
/* @var $this ParametroController */ 
/* @var $model Parametro */ 
/* @var $form CActiveForm */ 
?> 

<div class="form"> 

<?php $form=$this->beginWidget('CActiveForm', array(
     'id'=>'parametro-form', 
     // Please note: When you enable ajax validation, make sure the corresponding 
     // controller action is handling ajax validation correctly. 
     // There is a call to performAjaxValidation() commented in generated controller code. 
     // See class documentation of CActiveForm for details on this. 
     'enableAjaxValidation'=>false, 
)); ?> 

     <p class="note">Fields with <span class="required">*</span> are required.</p> 

     <?php echo $form->errorSummary($model); ?> 

     <div class="row"> 
       <?php echo $form->labelEx($model,'nombre'); ?> 
       <?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>256)); ?> 
       <?php echo $form->error($model,'nombre'); ?> 
     </div> 

     <div class="row buttons"> 
       <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> 
     </div> 

<?php $this->endWidget(); ?> 

</div><!-- form --> 

Parametro.php(型號)

<?php 

/** 
* This is the model class for table "parametro". 
* 
* The followings are the available columns in table 'parametro': 
* @property integer $id 
* @property string $nombre 
*/ 
class Parametro extends CActiveRecord 
{ 
     /** 
     * @return string the associated database table name 
     */ 
     public function tableName() 
     { 
       return 'parametro'; 
     } 

     /** 
     * @return array validation rules for model attributes. 
     */ 
     public function rules() 
     { 
       // NOTE: you should only define rules for those attributes that 
       // will receive user inputs. 
       return array(
//      array('nombre', 'required'), 
//      array('nombre', 'length', 'max'=>256), 
         // The following rule is used by search(). 
         // @todo Please remove those attributes that should not be searched. 
         array('id', 'safe', 'on'=>'search'), 
         array('hplocal', 'safe', 'on'=>'search'), 
//      array('id, nombre', 'safe', 'on'=>'search'), 
       ); 
     } 

     /** 
     * @return array relational rules. 
     */ 
     public function relations() 
     { 
       // NOTE: you may need to adjust the relation name and the related 
       // class name for the relations automatically generated below. 
       return array(
       'peticion'=>array(self::BELONGS_TO,'Peticion','peticion_id'), 
       ); 
     } 

     /** 
     * @return array customized attribute labels (name=>label) 
     */ 
     public function attributeLabels() 
     { 
       return array(
         'id' => 'ID', 
//      'nombre' => 'Nombre', 
       ); 
     } 

     /** 
     * Retrieves a list of models based on the current search/filter conditions. 
     * 
     * Typical usecase: 
     * - Initialize the model fields with values from filter form. 
     * - Execute this method to get CActiveDataProvider instance which will filter 
     * models according to data in model fields. 
     * - Pass data provider to CGridView, CListView or any similar widget. 
     * 
     * @return CActiveDataProvider the data provider that can return the models 
     * based on the search/filter conditions. 
     */ 
     public function search() 
     { 
       // @todo Please modify the following code to remove attributes that should not be searched. 

       $criteria=new CDbCriteria; 

       $criteria->compare('id',$this->id); 
//    $criteria->compare('nombre',$this->nombre,true); 

       return new CActiveDataProvider($this, array(
         'criteria'=>$criteria, 
       )); 
     } 

     /** 
     * Returns the static model of the specified AR class. 
     * Please note that you should have this exact method in all your CActiveRecord descendants! 
     * @param string $className active record class name. 
     * @return Parametro the static model class 
     */ 
     public static function model($className=__CLASS__) 
     { 
       return parent::model($className); 
     } 
} 

ParametroController.php(動作)

public function actionCreate() 
{ 
     $model=new Parametro; 

     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['Parametro'])) 
     { 
       $model->attributes=$_POST['Parametro']; 
       if($model->save()) 
         $this->redirect(array('view','id'=>$model->id)); 
     } 

     $this->render('create',array(
       'model'=>$model, 
     )); 
} 

你可以看到幾乎沒有什麼改變,但只是,即時通訊試圖找出如何開始,將由應用程序創建的列將要被稱爲「ph,h1,ho3等」。所以我想做一個複選框列表,它會打印應用程序創建的所有列,當您選擇其中一些列並按下提交時,選中的框將按特定列保存爲1或true。

請大家幫忙。

回答

0

您可以簡單地在yii中創建一個複選框列表。

<?php echo CHtml::activecheckBoxList($model, 'yourAttribute', array("1" => "Arts", "2" => "Science", "3" => "Culture"), array('separator' => '', 'id' => 'chk_lst_id')); ?> 

這將在列表中點擊這裏創建3個複選框,

<input type="checkbox" name="Arts" value="1">Arts<br> 
<input type="checkbox" name="Science" value="2">Science<br> 
<input type="checkbox" name="Culture" value="3">Culture 

檢查其他一些例子:http://www.yiiframework.com/wiki/48/by-example-chtml/#hh4