Archiv

Artikel Tagged ‘Widgets’

renderPortlets

16. Januar 2012
//views/layouts.main.php
<div class="span-6 last">
  <div id="sidebar">
    <?php $this->renderPortlets('right'); ?>
  </div>
</div>
//components/Controller.php
class Controller extends CController
{   
  public $portlets = array();
 
  public function addPortlet($position, $portlet)
  {
    $this->portlets[$position][] = $portlet;
  }
 
  public function renderPortlets($position)
  {
    if (isset($this->portlets[$position])) {
      foreach ($this->portlets[$position] as $portlet) {
        $this->beginWidget('zii.widgets.CPortlet', array(
          'title' => $portlet['title'],
        ));
        $this->widget($portlet['class'], $portlet['params']);
        $this->endWidget();
      }
    }
  }
}
//views/article/view.php
$this->addPortlet('right', array(
  'class' => 'zii.widgets.CMenu',
  'title' => 'Operations',
  'params' => array( //Configuration CMenu
    'items' => array(
      array('label' => 'List Article', 'url' => array('index')),
      array('label' => 'Create Article', 'url' => array('create')),
      array('label' => 'Update Article', 'url' => array('update', 'id' => $model->id)),
      array('label' => 'Delete Article', 'url' => '#', 'linkOptions' => array('submit' =>
        array('delete', 'id' => $model->id), 'confirm' => 'Are you sure you want to delete this item?')),
      ),
    'htmlOptions' => array('class' => 'operations')
  )
));
 
$this->addPortlet('right', array(
  'class' => 'ArticleInfoWidget',
  'title' => 'ArticleInfo',
  'params' => array(
    'article' => $model
  )
));
Author: Categories: Yii Tags: ,

CButtonColumn erweitern

31. August 2011

Yii::import('zii.widgets.grid.CButtonColumn');
 
//ext.widgets.grid.ButtonColumn - rendert die letzte Spalte in der Abbildung
class ButtonColumn extends CButtonColumn
{
    public static $assetsUrl;
 
    public function init()
    {
        $this->viewButtonImageUrl   = self::getAssetsUrl() . '/application_view_detail.png';
        $this->updateButtonImageUrl = self::getAssetsUrl() . '/application_edit.png';
        $this->deleteButtonImageUrl = self::getAssetsUrl() . '/application_delete.png';
 
        parent::init();
    }
 
    public static function getAssetsUrl()
    {
        if (self::$assetsUrl === null) {
            self::$assetsUrl = Yii::app()->getAssetManager()->publish(
                Yii::getPathOfAlias('ext.widgets.grid.images')
            );
        }
 
        return self::$assetsUrl;
    }
}
Author: Categories: Yii Tags: , , ,

CGridColumn

31. August 2011
//NumberColumn.php
Yii::import('zii.widgets.grid.CGridColumn');
 
//erzeugt eine Spalte mit fortlaufender Nummer
class NumberColumn extends CGridColumn
{
    protected function renderDataCellContent($row, $data)
    {
        static $counter = 0;
        echo ++$counter;
    }
}
 
//columns-configuration in einem CGridView
array(
    'class' => 'NumberColumn'
),
Author: Categories: Yii Tags: , ,

CGridView

11. Februar 2011
 
$this->widget('zii.widgets.grid.CGridView', array(
  'id' => 'member-grid',
  'dataProvider' => $model->search(),
  'filter' => $model,
  'columns'=>array(
    'id',
    array(
      'name' => 'salutationId',
      'filter' => CHtml::listData(Salutation::model()->findAll(), 'id', 'value'),
      'value' => '$data->salutation->value', //$data und NICHT $model verwenden
      'htmlOptions' => array('style' => 'text-align:center')
    ),
    'title',
    array(
        'class' => 'CLinkColumn', //zum DetailView verlinken
        'labelExpression' => '$data->fullname',
        'urlExpression' => "array('/member/view', 'id' => \$data->id)"
    ),
    array(
      'class'=>'CButtonColumn',
      'buttons' => array(
        'view' => array(
            'visible' => 'false' // Den ViewButton ausblenden
        )
      )
    ),
)));
Author: Categories: Yii Tags: , ,

Widgets

2. August 2009

Wer Smarty kennt, wird sicherlich auch schon mal Plugins geschrieben haben, speziell Smarty Template-Funktionen. Diese lassen sich in Templates einbinden und kapseln mehr oder weniger aufwendige Darstellungslogik.

In Yii übernehmen das die Widgets. Ein Widget erbt von CWidget und überschreibt die Methoden init() und run(), mindestens jedoch run(). Im Beispiel wenden wir Parameterized Scopes an, um die neuesten Beiträge zu holen.

class RecentPosts extends CWidget
{
    public $limit = 5;
 
    public function run() {
        $recentPosts = Post::model()->recently($this->limit)->published()->findAll();
        echo "<ul>";
        foreach ($recentPosts as $post) {
            echo "<li>" . $post->subject . "</li>";
        }
        echo "</ul>";
    }
}

Man könnte die Darstellung noch auslagern in einen extra View.

class RecentPosts extends CWidget
{
    public $limit = 5;
 
    public function run() {
        $this->render("recentPosts", array("posts" => $this->getRecentPosts()));
    }
 
    public function getRecentPosts() {
        return Post::model()->recently($this->limit)->published()->findAll();
    }
}

Der View recentPosts.php befindet sich hierbei im Ordner “views” unterhalb der Datei RecentPosts.php.

Und so der Aufruf im View

<?php $this->widget("application.extensions.widgets.RecentPosts", array("limit" => 10)); ?>
Author: Categories: Yii Tags: ,