自动填充yii中的下拉列表

时间:2014-02-08 09:59:18

标签: yii

我是Yii的新手,我目前正在尝试在我的本地系统中创建产品页面。我有一个位置下拉列表,从DB中获取位置。我的数据库中也有供应商表,也有相应的下拉列表。这些供应商映射到位置表。每当我从我的位置下拉选择一个位置时,必须在供应商下拉列表中自动填充该位置中的相应供应商。请说出一个解决方案。提前谢谢。

位置模型:

<?php

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

    /**
     * @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('location_name', 'length', 'max'=>50),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('location_id, location_name', '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(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'location_id' => 'Location',
            'location_name' => 'Location Name',
        );
    }

    /**
     * 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('location_id',$this->location_id);
        $criteria->compare('location_name',$this->location_name,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 Location the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

供应商模型:

<?php

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

    /**
     * @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('location_id', 'numerical', 'integerOnly'=>true),
            array('vendor_name', 'length', 'max'=>50),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('vendor_id, location_id, vendor_name', '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(
                'location_id'    => array(self::BELONGS_TO, 'Location','location_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'vendor_id' => 'Vendor',
            'location_id' => 'Location',
            'vendor_name' => 'Vendor Name',
        );
    }

    /**
     * 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('vendor_id',$this->vendor_id);
        $criteria->compare('location_id',$this->location_id);
        $criteria->compare('vendor_name',$this->vendor_name,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 Vendor the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }
}

形式:

<?php
/* @var $this ProductsController */
/* @var $model Products */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'products-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'=>true,
)); ?>

    <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,'product_name'); ?>
        <?php echo $form->textField($model,'product_name',array('size'=>50,'maxlength'=>50)); ?>
        <?php echo $form->error($model,'product_name'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'description'); ?>
        <?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
        <?php echo $form->error($model,'description'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'quantity'); ?>
        <?php echo $form->textField($model,'quantity'); ?>
        <?php echo $form->error($model,'quantity'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'category_id'); 
        echo $form->dropDownList($model,'category_id', CHtml::listData(Category::model()->findAll(), 'category_id', 'category_name'), array('prompt'=>'Select category','multiple' => 'multiple'));
         ?>
        <?php echo $form->error($model,'category_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'location_id'); 
        echo $form->dropDownList($model,'location_id', CHtml::listData(Location::model()->findAll(), 'location_id', 'location_name'), array('empty'=>'Select location'));?>
        <?php echo $form->error($model,'location_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'vendor');

        echo $form->dropDownList($model,'location_id',CHtml::listData(Vendor::model()->findAll(location_id),'location_id','vendor_name'),
                array(// htmlOptions
                        'ajax'=>array(// special htmlOption through clientChange, for ajax
                                'type'=>'GET',
                                'url'=>$this->createUrl('Vendor/Vendordetails'),// action that will generate the data
                                'data'=>'js:"id="+$(this).val()',// this is the data that we are sending to the action in the controller
                                'dataType'=>'json',// type of data we expect back from the server
                                'success'=>'js:updateFields'// a javascript function that will execute when the request completes successfully
                        ),


                )

        );





        //echo $form->dropDownList($model,'vendor', CHtml::listData(Vendor::model()->findAll(), 'location_id', 'vendor_name'), array('empty'=>'Select vendor'));?> 
        <?php echo $form->error($model,'vendor'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'status'); ?>
        <?php $status=array("AVAILABLE","NOT AVAILABLE")?>
        <?php echo $form->radioButtonList($model,'status',$status,array('separator'=>'')); ?>
        <?php echo $form->error($model,'status'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'stock'); ?>
        <?php echo $form->textField($model,'stock'); ?>
        <?php echo $form->error($model,'stock'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'prod_avl_date'); ?>
        <?php                    
       $this->widget('zii.widgets.jui.CJuiDatePicker',array(
    'name'=>'Products[prod_avl_date]',
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
'dateFormat' => 'yy/mm/dd',


    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
));                            
       ?> 
        <?php echo $form->error($model,'prod_avl_date'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'prod_end_date'); ?>
        <?php                    
       $this->widget('zii.widgets.jui.CJuiDatePicker',array(
    'name'=>'Products[prod_end_date]',
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
'dateFormat' => 'yy/mm/dd',

    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
));                            
       ?>
        <?php echo $form->error($model,'prod_end_date'); ?>

    </div>

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

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

</div>






<!-- form -->

1 个答案:

答案 0 :(得分:0)

听起来你想要一个依赖的下拉。这是一个很好的起点。我不会复制所有代码:

http://www.yiiframework.com/wiki/24/

相关问题