Yii 1.1: jquery-cascade

Cascading Select Lists
8 followers

Created to ease the quite repetitive task of programming cascading select lists.

Resources

Documentation

Requirements

  • Yii 1.1 or above

Installation

  • Extract the release file under protected/extensions

Usage

See the following code example:

$state = $this->createWidget('ext.jquery-cascade.jQueryCascade');
 
// dropDownList($id, $selected, $data, $htmlOptions = array(), $source, $cascaded)
echo $state->dropDownList('stateId',
    '',
    CHtml::listData(State::model()->findAll(), 'id', 'name'),
    '',
    CController::createUrl('city/list'),
    'cityId'
);

The function which serves the data needed:

public function actionList()
{
    if (Yii::app()->request->isAjaxRequest) {
        $state = State::model()->findByPk($_GET['selected']);
        $cities = array();
 
        foreach ($state->cities as $city) {
            $cities[] = array('label' => $city->name, 'value' => $city->id);
        }
 
        echo json_encode($cities);
        Yii::app()->end();
    } else {
        throw new CHttpException(400, 'Invalid request.');
    }
}

Change Log

Apr 20, 2011

  • Never use DIRECTORY_SEPARATOR in URLs.

May 31, 2010

  • Initial release.

Total 6 comments

#3599 report it
PinkBrainPlan at 2011/04/21 06:43am
Select Value from javascript

Here we go:

Anyway the header doesn't change:(

function updateEditFormFilters(target_id){
 
    $('#Notification').jnotifyAddMessage({
            text: 'Loading filters...',
            permanent: false,
            showIcon: true
   });
 
    var id =$.fn.yiiGridView.getSelection(target_id);
 
    $('#fil_save_button').attr('value', (id > 0 ? 'update' : 'create'));
 
    $.getJSON('<?php echo Yii::app()->createUrl("//qvfilters/subviewload"); ?>'+'&id='+id,
            function(data) {
                $('#Qvfilters_filter').val(data.filter);
                                $('#Qvfilters_fil_id').val(data.fil_id);
                                $('#Qvfilters_qvf_id').val(data.qvf_id);
                                $('#Qvfilters_qvf_id').multiselect('widget').find('input[value="'+ data.qvf_id +'"]').trigger('click');
                                $('#Qvfilters_rec_id').val(data.rec_id);
                        });
 
   return false;
}
#3597 report it
PinkBrainPlan at 2011/04/21 05:06am
Filter is now working

Hi,

I got the filter working;) Sorry, my fault...

<?php
 
                    $qvfields = $this->createWidget('ext.JQueryCascade.jQueryCascade');
 
                    echo $qvfields->dropdownList('Qvfilters[qvf_id]',
                        $_GET['id'],
                        CHtml::listData(Qvfields::model()->findAll("qvf_docName = :qvf_docName",array(':qvf_docName'=>strtoupper(Reports::model()->findByPK($_GET['id'])->ber_docName))),'qvf_id','qvf_fieldName'),
                        array(
                            'ajax'=>array(
                                'type'=>'POST',
                                'url'=>Yii::app()->createUrl('//qvmapping/loadvalues'),
                                'update'=>'#qvv_values',
                                'data'=>array('id'=>'js:this.value'),
                            ),
                            'filter'=>'true',
                        ),
                        CController::createUrl('qvfields/listFields'),
                        'tt_qvf_id',
                        ''
                    );
                    ?>

But I still have the problem, that I'm not able to set the value...:( How can I do that...

I tried with:

$('#Qvfilters_qvf_id').multiselect().selectText(data.qvf_id);

Cheers Phil

#3588 report it
mercurial at 2011/04/20 04:08pm
reply: enable filter

@philippfrenzel,

  1. Do you have an error message?
  2. Does the actionListFields() action returns the correct JSON response?
#3586 report it
PinkBrainPlan at 2011/04/20 11:06am
enable filter

Hey, I like your extension! But I don't get to run the filter stuff:(

Here is my code:

<?php
 
                    $qvfields = $this->createWidget('ext.JQueryCascade.jQueryCascade');
 
                    echo $qvfields->dropdownList('Qvfilters[qvf_id]',
                        $_GET['id'],
                        CHtml::listData(Qvfields::model()->findAll("qvf_docName = :qvf_docName",array(':qvf_docName'=>strtoupper(Reports::model()->findByPK($_GET['id'])->ber_docName))),'qvf_id','qvf_fieldName'),
                        array(
                            'ajax'=>array(
                                'type'=>'POST',
                                'url'=>Yii::app()->createUrl('//qvmapping/loadvalues'),
                                'update'=>'#qvv_values',
                                'data'=>array('id'=>'js:this.value'),
                            ),
                            'searchable'=>'true',
                        ),
                        CController::createUrl('qvfields/listFields'),
                        'tt_qvf_id',
                        ''
                    );
                    ?>

And then in the controller:

public function actionListFields()
        {
            if (Yii::app()->request->isAjaxRequest) {
                $docName = strtoupper(Reports::model()->findByPK($_GET['selected'])->ber_docName);
                $Fields = Qvfields::model()->findAll('qvf_docName = :qvf_docName',array(':qvf_docName'=>$docName));
                $Labels = array();
 
                foreach ($Fields as $Field) {
                    $Labels[] = array('label' => $Field->qvf_fieldName, 'value' => $Field->qvf_id,'searchable'=>'true');
                }
 
                echo json_encode($Labels);
                Yii::app()->end();
            } else {
                throw new CHttpException(400, 'Invalid request.');
            }
        }

And one other issue is, that I try to set the selection by javascript Jquery - but I don't get it to run:(

Thank you for your help!

Cheers Phil

#2375 report it
Werewolf at 2010/12/19 05:24am
bug on windows server

Never use DIRECTORY_SEPARATOR in URL!

$js = $this->baseUrl . '/jquery.cascade-select.js';
#443 report it
intel352 at 2010/06/01 10:51am
Need some changes to your controller method

Since you are providing an example for everyone to implement, I would recommend:

  1. In actionList(), wrap the output with:

    if(Yii::app()->request->isAjaxRequest) { .... } else { throw new CHttpException(400,'Invalid request. Please do not repeat this request again.'); }

  2. Additionally, please end the output of actionList() properly, using

    Yii::app()->end();

so that debugging output doesn't screw with the json output.

Leave a comment

Please to leave your comment.

Create extension