1

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...

Salini L
  • 829
  • 5
  • 15
  • 43
  • You want to dynamically change url with PHP? Why not use jQuery `$(document).on('change', '.myDropDown', function(){});` ? – Justinas Feb 04 '14 at 09:27
  • Did u meaned $(document).on('change', '.Site_selectedsiteid', function() { $sid=$siteid->selectedsiteid; }); but its also not working. Actually I dnt knw jQuery well. – Salini L Feb 04 '14 at 09:54
  • Wait, so you want the URL of the CMenu bar to change if the user chooses an option in the drop down? I'm not sure what exactly you mean by "parameter". – Jan K. S. Feb 04 '14 at 12:18
  • @Jan I think that's what he want, he means the link in one of his menu items has a parameter that needs to be passed, and that parameter is dependent on the value in the dropdown. – deacs Feb 04 '14 at 15:50
  • @deacs In that case, *she* could give us more details about it, so we can write a proper answer using jQuery. – Jan K. S. Feb 04 '14 at 19:17
  • @jan and @ deacs ya u r right i want to change the url which is depending on the droupdown list – Salini L Feb 05 '14 at 04:26

2 Answers2

0

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;
}

Salini L
  • 829
  • 5
  • 15
  • 43
0

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;
             }
         );
     }
);
Jan K. S.
  • 1,761
  • 1
  • 14
  • 13