Yii Framework Forum: Dependent Dropdown - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Dependent Dropdown Rate Topic: -----

#1 User is offline   jr_jags 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 05-November 12
  • Location:Philippines

Posted 04 February 2013 - 11:11 PM

I have this two dropdownlist:

<?php echo $form->dropDownList($account, 'Status1', array('1' => 'Active', '2' => 'InActive'), array('prompt' => ''));
?>

<?php echo $form->dropDownList($account, 'Status2', array('1' => 'Locked by Attempt', '2' => 'Expired'), array('prompt' => ''));
?>

When I choose Active form dropdown status1 dropdown status2 will be disabled and when I chose InActive dropdown status2 will be enabled and can show data 1 and 2,

How can I manage to do this in yii?
Can you please help me with this problem, Im having a hard time on using jquery on yii :(

Is there a way on creating dependent dropdown using CFormModel, the wikis about this lessons are using CActiveRecord
0

#2 User is offline   nermin.i 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 09-January 13

Posted 05 February 2013 - 03:51 AM

You can add this code, for attribute "onchange"


<?php echo $form->dropDownList($account, 
                       'Status1', 
                        array('1' => 'Active', '2' => 'InActive'),
                        array(
                          'prompt' => '',
                          'onchange'=>'function(){ if($("#Status1").val()==1){$("#Status2").attr("disabled",    "disabled"); } else {$("#Status2").removeAttr("disabled");}}'
                       )
                     );
?>


0

#3 User is offline   jr_jags 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 05-November 12
  • Location:Philippines

Posted 05 February 2013 - 04:29 AM

oh so my view file will look like this?

<?php echo $form->dropDownList($account,
'Status1',
array('1' => 'Active', '2' => 'InActive'),
array(
'prompt' => '',
'onchange'=>'function(){ if($("#Status1").val()==1){$("#Status2").attr("disabled", "disabled"); } else {$("#Status2").removeAttr("disabled");}}'
)
);
?>


<?php echo $form->dropDownList($account, 'Status2', array('1' => 'Locked by Attempt', '2' => 'Expired'), array('prompt' => ''));
?>

But still Status2 is still enabled when I chose 1
0

#4 User is offline   nermin.i 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 09-January 13

Posted 05 February 2013 - 05:44 AM

Remove from onchange "function(){}"


<?php echo $form->dropDownList($account, 
                       'Status1', 
                        array('1' => 'Active', '2' => 'InActive'),
                        array(
                          'prompt' => '',
                          'onchange'=>' if($("#Status1").val()==1){$("#Status2").attr("disabled",    "disabled"); } else {$("#Status2").removeAttr("disabled");}'
                       )
                     );
?>

0

#5 User is offline   jr_jags 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 05-November 12
  • Location:Philippines

Posted 05 February 2013 - 06:19 AM

Still not working, do you have any examples using dependent dropdowns? I checked on the wiki using dependent dropdowns but It usus CActiveRecord and Im using CFormModel.
0

#6 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 394
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 February 2013 - 07:39 AM

That should work!
<?php echo $form->dropDownList($account, 
                       'Status1', 
                        array('1' => 'Active', '2' => 'InActive'),
                        array(
                          'prompt' => '',
                          'onchange'=>' if($("#ModelName_Status1").val()==1){$("#ModelName_Status2").attr("disabled",    "disabled"); } else {$("#ModelName_Status2").removeAttr("disabled");}'
                       )
                     );
?>

Posted Image
0

#7 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 394
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 February 2013 - 07:41 AM

Assuming you are using model "User" #ModelName_Status1 is #User_Status1; it's the id for form input element ;)
Posted Image
0

#8 User is offline   jr_jags 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 05-November 12
  • Location:Philippines

Posted 05 February 2013 - 08:38 AM

but when I chose Inactive in my status1 dropdown I can still select data from status2, so it means its still enabled, I want to do is when I selectInactive in my status1 dropdown, my second dropdown will be automatically disabled
0

#9 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 394
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 05 February 2013 - 09:39 AM

View Postjr_jags, on 05 February 2013 - 08:38 AM, said:

but when I chose Inactive in my status1 dropdown I can still select data from status2, so it means its still enabled, I want to do is when I selectInactive in my status1 dropdown, my second dropdown will be automatically disabled


Would you paste your codes here so that we can help you better ?
Posted Image
0

#10 User is offline   jr_jags 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 05-November 12
  • Location:Philippines

Posted 05 February 2013 - 05:19 PM

<?php $this->pageTitle = Yii::app()->name . ' - Account Creation'; ?>

            <?php
$this->menu=array(
    array('label'=>'Create Account', 'url'=>array('create')),
	array('label'=>'Update Account', 'url'=>array('view')),
    array('label'=>'Search Account', 'url'=>array('search')),
    array('label'=>'Lock Account', 'url'=>array('lock')),
    array('label'=>'Unlock Account', 'url'=>array('unlock')),
    array('label'=>'Audit Trail', 'url'=>array('audittrail')),
);

            ?>
<?php if (Yii::app()->user->hasFlash('account')): ?>
    <div class="flash-success">
        <?php echo Yii::app()->user->getFlash('account'); ?>
    </div>

<?php else: ?>
                <h1>Register New Account</h1>
    <div class="form">
    

            <?php
            $form = $this->beginWidget('CActiveForm', array(
                'id' => 'AccountForm',
                'enableClientValidation' => true,
                'clientOptions' => array(
                    'validateOnSubmit' => true,
                ),
                    ));            
   ?>
            
            <?php
            if (isset($flash))
                echo($flash);
            $AccountType = new RefAccountType();
            ?>
            
            
            <?php echo $form->errorSummary($account); ?>


            <div class="row">
                <?php echo $form->labelEx($account, 'username'); ?>
                <?php echo $form->textField($account, 'username',array('maxlength' => 20)); ?>
                <?php echo $form->error($account, 'username'); ?>
            </div>

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

                <?php echo $form->dropDownList($account, 'AccountTypeName', $AccountType->getAccounttype(), array('prompt' => '')); ?>
                <?php echo $form->error($account, 'AccountTypeName'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($account, 'AccountGroup'); ?>
                <?php echo $form->dropDownList($account, 'AccountGroup', $AccountType->getAccountgroup(), array('prompt' => '')); ?>
                <?php echo $form->error($account, 'AccountGroup'); ?>
            </div>

<?php echo $form->dropDownList($account, 
                       'Status1', 
                        array('1' => 'Active', '2' => 'InActive'),
                        array(
                          'prompt' => '',
                          'onchange'=>' if($("#Account_Status1").val()==1){$("#Account_Status2").attr("disabled",    "disabled"); } else {$("#Account_Status2").removeAttr("disabled");}'
                       )
                     );
?>
        
        <?php echo $form->dropDownList($account, 'Status2', array('1' => 'Locked by Attempt', '2' => 'Expired'), array('prompt' => ''));?>
        
            <div class="row">
                <?php echo $form->labelEx($account, 'name'); ?>
                <?php echo $form->textField($account, 'name', array('size' => 38, 'maxlength' => 50)); ?>
                <?php echo $form->error($account, 'name'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($account, 'address'); ?>
                <?php echo $form->textArea($account, 'address', array('rows' => 4, 'cols' => 50, 'size' => 5, 'maxlength' => 150)); ?>
                <?php echo $form->error($account, 'address'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($account, 'email'); ?>
                <?php echo $form->textField($account, 'email',array('maxlength' => 50)); ?>
                <?php echo $form->error($account, 'email'); ?>
            </div>

            <div class="row">
                <?php echo $form->labelEx($account, 'Landline Number'); ?>
                <? echo 'Country Code: '; ?>
                <?php echo $form->textField($account, 'phone1', array('size' => 5, 'maxlength' => 2)); ?>
                <? echo 'Area Code: '; ?>
                <?php echo $form->textField($account, 'phone2', array('size' => 5, 'maxlength' => 2)); ?>
                <? echo 'Landline Number: '; ?>
                <?php echo $form->textField($account, 'phone3', array('size' => 7, 'maxlength' => 7)); ?>
                <?php echo $form->error($account, 'phone1'); ?>
                <?php echo $form->error($account, 'phone2'); ?>
                <?php echo $form->error($account, 'phone3'); ?>

            </div>

            <div class="row">
                <?php echo $form->labelEx($account, 'Mobile Number'); ?>
                <? echo 'Country Code: '; ?>
                <?php echo $form->textField($account, 'mobile1', array('size' => 5, 'maxlength' => 2)); ?>
                <? echo 'Area Code: '; ?>
                <?php echo $form->textField($account, 'mobile2', array('size' => 5, 'maxlength' => 2)); ?>
                <? echo 'Mobile Number: '; ?>
                <?php echo $form->textField($account, 'mobile3', array('size' => 10, 'maxlength' => 10)); ?>
                <?php echo $form->error($account, 'mobile1'); ?>
                <?php echo $form->error($account, 'mobile2'); ?>
                <?php echo $form->error($account, 'mobile3'); ?>
            </div>
            <div class="row buttons">
                <?php
                echo CHtml::submitButton('Submit', array('onclick' => 'js:function(){ alert("test"); $(#mymodal).dialog("close");}',
                ));
                ?>   
            </div>
            <div>   
    <?php echo CHtml::endForm(); ?>
            </div><!-- form -->
            </p>
            <?php $this->endWidget(); ?>
<?php endif; ?>
        <p><?php echo CHtml::link('Back', array('/account/search')) ?> </p>
    </div>    


Here are is my whole view file, I just copy the first dropdown Status1 just like what you say and change the Model to the name of my model.
0

#11 User is offline   fouss 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 394
  • Joined: 05-October 10
  • Location:Bamako Mali

Posted 06 February 2013 - 05:08 AM

Hi again!

I can't see any error in your code...
Could you check with your browser if really your select boxes values are Account_Status1 and Account_Status2?
It may be Account_status1 and Account_status2 or others ....
You can also quote 1 like(not required if not text):
' if($("#Account_Status1").val()=="1"){$("#Account_Status2").attr("disabled",    "disabled"); } 
                          else {$("#Account_Status2").removeAttr("disabled");}'


Good luck friend!
Posted Image
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users