Question

I am new to yii. In my project I want to pass a parameter through the Cmenu bar

 array('label' => 'Category', 'url' => array('site/catagory', 'visible' => !Yii::app()->user->isGuest),

and the parameter passing is getting from a droupdown list which is also in the navigation bar code of droupdownlist is

  $site = Site::model()->findAll();  
  $data = CHtml::listData($site, 'id', 'type');  
  echo $form->dropDownList($siteid, 'selectedsiteid', $data, array('class' => "form-control"));

which comes after the menu bar. please somebody help me.
Thanks in advance...

Était-ce utile?

La solution

I have solved it by using javascript, the menu butttons code is

array('label' => 'Category', 'url' => '#','linkOptions'=>array('onclick'=>'getsid()'),'visible' => !Yii::app()->user->isGuest),

and called a java script

function getsid()
{
$sid=$('#Site_selectedsiteid').val();
window.location.href = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf( '/' ) + 1 ) + 'catagory?sid=' + $sid;
}

Autres conseils

It is not recommended to use onclick + global function/variables. Things can become messy over time.

A slightly more elegant solution using jQuery. See how the logic is separated from the view.


PHP:

array(
    'label' => 'Category',
    'url' => '#',
    'linkOptions' => array( 'id' => 'menu-link' ),
    'visible' => ! Yii::app( )->user->isGuest
),


Javascript:

$( document ).ready(
    function ( )
    {
         $( '#menu-link' ).click(
             function ( )
             {
                 var sid = $( '#Site_selectedsiteid' ).val( );
                 var currentURL = window.location.pathname;
                 var newUrl = currentURL.substring( 0, currentURL.lastIndexOf( '/' ) + 1 );
                 window.location.href = newUrl + 'category?sid=' + sid;
             }
         );
     }
);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top