2013-12-20 133 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