2014-02-08 44 views
0

我是Yii中的新手,我正在嘗試在本地系統中執行產品頁面。我有一個位置下拉列表,它從數據庫中提取位置。我的數據庫中也有一個供應商表,並且也有相應的下拉菜單。這些供應商映射到位置表。每當我從我的位置下拉選擇一個位置時,出現在該位置的相應供應商必須在供應商下拉菜單中自動填充。請說一個解決方案。提前致謝。在yii中自動填充下拉菜單

選址模型:

<?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 --> 
+0

做u要更新其供應商的形式的屬性? –

+0

控制器在哪裏? –

回答