Yii Framework Forum: Retrieve Model Data In Layout With Dropdownlist - Yii Framework Forum

Jump to content

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

Retrieve Model Data In Layout With Dropdownlist Make a dropdownlist that works globally Rate Topic: -----

#1 User is offline   RobMies 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 25-April 14

Posted 02 June 2014 - 08:40 AM

Hello everyone,

as someone who is quite new to the whole Yii-experience, I would like to share some of my findings regarding a dropdownlist at te top of my website. I use this list to choose a client. In this way, I have the least amount of clicks to switch between clients. The list is dynamically populated BTW.

I don't know if this is the correct way to do it (but it works, and that must count for something) and maybe I'm going against the whole MVC, DRY and whatever paradigms. But here goes:

The layout.php is not connected to any model, so my first step is to make a widget, under /protected/components/ClientWidget.php

<?php
class ClientWidget extends CWidget {

    public function run() {
        $models = Clienten::model()->findAll();
        //echo YiiBase::getPathOfAlias('views');
        $this->render('application.views.clientdropdown', array(
            'models'=>$models   
        ));
    }
}

This widget gets all of my clients in model: Clients.

In my view (I use the theme named abound, BTW) I paste this in /views/layouts/tpl_navigation.php. This is the topbar navigation of the site:
...
<div class="subnav navbar navbar-fixed-top">
    <div class="navbar-inner">
    	<div class="container">
    	    <div class="navbar-search pull-left">
              <?php
              $this->widget('ClientWidget') ?>
            </div>
        	
    	</div><!-- container -->
    </div><!-- navbar-inner -->
</div><!-- subnav -->


And the final piece of code in /protected/views/clientdropdown.php
This one had me pulling my hairs out! (but I always start with the gray hairs. So at least there is a little bit of positive downside to it :) ).
I was googling for the better part of a day, but couldn't figure out how to react to a change in the dropdownlist. So, I devised something, by not using the properties of the dropdownlist, but of JQuery.
After a onchange event, I redirect to the proper page. I know, there is some hardcoding of the URL there, but to change that in something more elegant and variable shouldn't pose much of a problem.
I hope this solution may help some of you.

<?php if($models != null): ?>
    
     <?php //$models = categories::model()->findAll();
     $select = '';
     $NaarClientPagina = Yii::app()->controller->createUrl("clienten");
     
     // format models resulting using listData     
     $list = CHtml::listData($models, 
                'ClientID', 'ClientAchternaam');    
 
     echo CHtml::dropDownList('clienten', $select, 
              $list,
              array('empty' => 'Selecteer een client',
              ));
          
    ?>
 
<?php endif; ?>

<script type="text/javascript">
$("#clienten").change(function() {
    
     /* do something here */ 
     var Keuze="<?php echo $NaarClientPagina; ?>";
     Keuze = 'localhost/bs/clienten/'+$("#clienten").val();
     window.location.href = '//'+(location.hostname)+'/bs/clienten/'+$("#clienten").val();
     //Okay, not really up to standards, this line, have to change it into something more elegant...
     }); 

    </script>


0

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