0

I created two dropdownlists using Chtml::dropdownlists as shown below.

echo $form - > dropDownList($model, 'min_cost', Yii::app() - > params['cost_resales'],
    array(
        'empty' = > 'Choose one',
    )
);
echo $form - > dropDownList($model, 'max_cost', Yii::app() - > params['cost_resales'],
    array(
        'empty' = > 'Choose one',
    )
);

Now, the above code has to work according to the below script

// Keep a copy of the default options
var $options = $('#SearchForm_min_cost').children().clone();

$('#SearchForm_min_cost').change(function(){
// Within your change handler:

var index = $(this).find(':selected').index();
$('#SearchForm_max_cost').html($options).children(':lt('+index+')').remove();

});    

I created a fiddle http://jsfiddle.net/E3mY2/1/ with the above code. In fiddle it works fine. But, I don't know how to call the script in order to make the dropdownlists work

1

2 Answers 2

0

just give them proper id in htmlOptions attribute:

echo $form->dropDownList($model, 'min_cost', Yii::app()->params['cost_resales'], 
       array('empty'=>'Choose one', ) ,
       array('id' => 'SearchForm_min_cost') // set id in htmlOptions
);
echo $form->dropDownList($model, 'max_cost', Yii::app()->params['cost_resales'], 
      array('empty'=>'Choose one', ),
      array('id' => 'SearchForm_max_cost') // set id in htmlOptions
 );

and register your javascript with yii

Sign up to request clarification or add additional context in comments.

3 Comments

The HTML code in the fiddle is the code generated using Yii dropdownlist. Hence the id is generated by the dropdownlist.
Can you tell me how to register javascript. I saw the example, it is not clear enough
Yii::app()->clientScript->registerScript('my vars','put your javascript here', CClientScript::POS_HEAD);
0

Firstly check the id which is generated after rending the view on browser is it correct or same as in your script. Then for testing put your script at the bottom of your view inside script tag.

========= View ends here below this =========== Make sure jquery file is added in the page. If the id in broswer of your dropdown is same as script it will definetly work after successful test resiter your script with Yii.

// Keep a copy of the default options
var $options = $('#SearchForm_min_cost').children().clone();

    $('#SearchForm_min_cost').change(function(){
    // Within your change handler:

    var index = $(this).find(':selected').index();
    $('#SearchForm_max_cost').html($options).children(':lt('+index+')').remove();

    });

2 Comments

I have done the same thing. But why it is not working
Can you paste your view & model code here or some where else so that i can see why its not working and let you now the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.