~fusonic/chive/1.1

« back to all changes in this revision

Viewing changes to yii/zii/widgets/CListView.php

  • Committer: Matthias Burtscher
  • Date: 2010-02-12 09:12:35 UTC
  • Revision ID: matthias.burtscher@fusonic.net-20100212091235-jqxrb62klx872ajc
* Updated Yii to 1.1.0
* Removed CodePress and CodeMirror
* Updated jQuery and some plugins
* Cleaned some code ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CListView class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2010 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
Yii::import('zii.widgets.CBaseListView');
 
12
 
 
13
/**
 
14
 * CListView displays a list of data items in terms of a list.
 
15
 *
 
16
 * Unlike {@link CGridView} which displays the data items in a table, CListView allows one to use
 
17
 * a view template to render each data item. As a result, CListView could generate more flexible
 
18
 * rendering result.
 
19
 *
 
20
 * CListView supports both sorting and pagination of the data items. The sorting
 
21
 * and pagination can be done in AJAX mode or normal page request. A benefit of using CListView is that
 
22
 * when the user browser disables JavaScript, the sorting and pagination automatically degenerate
 
23
 * to normal page requests and are still functioning as expected.
 
24
 *
 
25
 * CListView should be used together with a {@link IDataProvider data provider}, preferrably a
 
26
 * {@link CActiveDataProvider}.
 
27
 *
 
28
 * The minimal code needed to use CListView is as follows:
 
29
 *
 
30
 * <pre>
 
31
 * $dataProvider=new CActiveDataProvider('Post');
 
32
 *
 
33
 * $this->widget('zii.widgets.CListView', array(
 
34
 *     'dataProvider'=>$dataProvider,
 
35
 *     'itemView'=>'_post',   // refers to the partial view named '_post'
 
36
 *     'sortableAttributes'=>array(
 
37
 *         'title',
 
38
 *         'create_time'=>'Post Time',
 
39
 *     ),
 
40
 * ));
 
41
 * </pre>
 
42
 *
 
43
 * The above code first creates a data provider for the <code>Post</code> ActiveRecord class.
 
44
 * It then uses CListView to display every data item as returned by the data provider.
 
45
 * The display is done via the partial view named '_post'. This partial view will be rendered
 
46
 * once for every data item. In the view, one can access the current data item via variable <code>$data</code>.
 
47
 * For more details, see {@link itemView}.
 
48
 *
 
49
 * In order to support sorting, one has to specify the {@link sortableAttributes} property.
 
50
 * By doing so, a list of hyperlinks that can sort the data will be displayed.
 
51
 *
 
52
 * @author Qiang Xue <qiang.xue@gmail.com>
 
53
 * @version $Id: CListView.php 99 2010-01-07 20:55:13Z qiang.xue $
 
54
 * @package zii.widgets
 
55
 * @since 1.1
 
56
 */
 
57
class CListView extends CBaseListView
 
58
{
 
59
        /**
 
60
         * @var string the view used for rendering each data item.
 
61
         * This property value will be passed as the first parameter to either {@link CController::renderPartial}
 
62
         * or {@link CWidget::render} to render each data item.
 
63
         * In the corresponding view template, the following variables can be used in addition to those declared in {@link viewData}:
 
64
         * <ul>
 
65
         * <li><code>$this</code>: refers to the owner of this list view widget. For example, if the widget is in the view of a controller,
 
66
         * then <code>$this</code> refers to the controller.</li>
 
67
         * <li><code>$data</code>: refers to the data item currently being rendered.</li>
 
68
         * <li><code>$index</code>: refers to the zero-based index of the data item currently being rendered.</li>
 
69
         * <li><code>$widget</code>: refers to this list view widget instance.</li>
 
70
         * </ul>
 
71
         */
 
72
        public $itemView;
 
73
        /**
 
74
         * @var array additional data to be passed to {@link itemView} when rendering each data item.
 
75
         * This array will be extracted into local PHP variables that can be accessed in the {@link itemView}.
 
76
         */
 
77
        public $viewData=array();
 
78
        /**
 
79
         * @var array list of sortable attribute names. In order for an attribute to be sortable, it must also
 
80
         * appear as as a sortable attribute the {@link IDataProvider::sort} property of {@link dataProvider}.
 
81
         * @see enableSorting
 
82
         */
 
83
        public $sortableAttributes;
 
84
        /**
 
85
         * @var string the template to be used to control the layout of various components in the list view.
 
86
         * These tokens are recognized: {summary}, {sorter}, {items} and {pager}. They will be replaced with the
 
87
         * summary text, the sort links, the data item list, and the pager.
 
88
         */
 
89
        public $template="{summary}\n{sorter}\n{items}\n{pager}";
 
90
        /**
 
91
         * @var string the CSS class name for the sorter container. Defaults to 'sorter'.
 
92
         */
 
93
        public $sorterCssClass='sorter';
 
94
        /**
 
95
         * @var string the text shown before sort links. Defaults to 'Sort by: '.
 
96
         */
 
97
        public $sorterHeader;
 
98
        /**
 
99
         * @var string the text shown after sort links. Defaults to empty.
 
100
         */
 
101
        public $sorterFooter='';
 
102
        /**
 
103
         * @var mixed the ID of the container whose content may be updated with an AJAX response.
 
104
         * Defaults to null, meaning the container for this list view instance.
 
105
         * If it is set false, it means sorting and pagination will be performed in normal page requests
 
106
         * instead of AJAX requests. If the sorting and pagination should trigger the update of multiple
 
107
         * containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).
 
108
         */
 
109
        public $ajaxUpdate;
 
110
        /**
 
111
         * @var string the name of the GET variable that indicates the request is an AJAX request triggered
 
112
         * by this widget. Defaults to 'ajax'. This is effective only when {@link ajaxUpdate} is not false.
 
113
         */
 
114
        public $ajaxVar='ajax';
 
115
        /**
 
116
         * @var string a javascript function that will be invoked before an AJAX update occurs.
 
117
         * The function signature is <code>function(id)</code> where 'id' refers to the ID of the list view.
 
118
         */
 
119
        public $beforeAjaxUpdate;
 
120
        /**
 
121
         * @var string a javascript function that will be invoked after a successful AJAX response is received.
 
122
         * The function signature is <code>function(id, data)</code> where 'id' refers to the ID of the list view
 
123
         * 'data' the received ajax response data.
 
124
         */
 
125
        public $afterAjaxUpdate;
 
126
        /**
 
127
         * @var string the base script URL for all list view resources (e.g. javascript, CSS file, images).
 
128
         * Defaults to null, meaning using the integrated list view resources (which are published as assets).
 
129
         */
 
130
        public $baseScriptUrl;
 
131
        /**
 
132
         * @var string the URL of the CSS file used by this list view. Defaults to null, meaning using the integrated
 
133
         * CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
 
134
         */
 
135
        public $cssFile;
 
136
 
 
137
        /**
 
138
         * Initializes the list view.
 
139
         * This method will initialize required property values and instantiate {@link columns} objects.
 
140
         */
 
141
        public function init()
 
142
        {
 
143
                if($this->itemView===null)
 
144
                        throw new CException(Yii::t('zii','The property "itemView" cannot be empty.'));
 
145
                parent::init();
 
146
 
 
147
                if(!isset($this->htmlOptions['class']))
 
148
                        $this->htmlOptions['class']='list-view';
 
149
 
 
150
                if($this->baseScriptUrl===null)
 
151
                        $this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/listview';
 
152
 
 
153
                if($this->cssFile!==false)
 
154
                {
 
155
                        if($this->cssFile===null)
 
156
                                $this->cssFile=$this->baseScriptUrl.'/styles.css';
 
157
                        Yii::app()->getClientScript()->registerCssFile($this->cssFile);
 
158
                }
 
159
        }
 
160
 
 
161
        /**
 
162
         * Registers necessary client scripts.
 
163
         */
 
164
        public function registerClientScript()
 
165
        {
 
166
                $id=$this->getId();
 
167
 
 
168
                if($this->ajaxUpdate===false)
 
169
                        $ajaxUpdate=array();
 
170
                else
 
171
                        $ajaxUpdate=array_unique(preg_split('/\s*,\s*/',$this->ajaxUpdate.','.$id,-1,PREG_SPLIT_NO_EMPTY));
 
172
                $options=array(
 
173
                        'ajaxUpdate'=>$ajaxUpdate,
 
174
                        'ajaxVar'=>$this->ajaxVar,
 
175
                        'pagerClass'=>$this->pagerCssClass,
 
176
                        'sorterClass'=>$this->sorterCssClass,
 
177
                );
 
178
                if($this->beforeAjaxUpdate!==null)
 
179
                        $options['beforeAjaxUpdate']=(strpos($this->beforeAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->beforeAjaxUpdate;
 
180
                if($this->afterAjaxUpdate!==null)
 
181
                        $options['afterAjaxUpdate']=(strpos($this->afterAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->afterAjaxUpdate;
 
182
 
 
183
                $options=CJavaScript::encode($options);
 
184
                $cs=Yii::app()->getClientScript();
 
185
                $cs->registerCoreScript('jquery');
 
186
                $cs->registerScriptFile($this->baseScriptUrl.'/jquery.yiilistview.js');
 
187
                $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiListView($options);");
 
188
        }
 
189
 
 
190
        /**
 
191
         * Renders the data item list.
 
192
         */
 
193
        public function renderItems()
 
194
        {
 
195
                echo CHtml::openTag('div',array('class'=>$this->itemsCssClass))."\n";
 
196
                $data=$this->dataProvider->getData();
 
197
                if(count($data)>0)
 
198
                {
 
199
                        $owner=$this->getOwner();
 
200
                        $render=$owner instanceof CController ? 'renderPartial' : 'render';
 
201
                        foreach($data as $i=>$item)
 
202
                        {
 
203
                                $data=$this->viewData;
 
204
                                $data['index']=$i;
 
205
                                $data['data']=$item;
 
206
                                $data['widget']=$this;
 
207
                                $owner->$render($this->itemView,$data);
 
208
                        }
 
209
                }
 
210
                else
 
211
                        $this->renderEmptyText();
 
212
                echo CHtml::closeTag('div');
 
213
        }
 
214
 
 
215
        /**
 
216
         * Renders the sorter.
 
217
         */
 
218
        public function renderSorter()
 
219
        {
 
220
                if($this->dataProvider->getItemCount()<=0 || !$this->enableSorting || empty($this->sortableAttributes))
 
221
                        return;
 
222
                echo CHtml::openTag('div',array('class'=>$this->sorterCssClass))."\n";
 
223
                echo $this->sorterHeader===null ? Yii::t('zii','Sort by: ') : $this->sorterHeader;
 
224
                echo "<ul>\n";
 
225
                $sort=$this->dataProvider->getSort();
 
226
                foreach($this->sortableAttributes as $name=>$label)
 
227
                {
 
228
                        echo "<li>";
 
229
                        if(is_integer($name))
 
230
                                echo $sort->link($label);
 
231
                        else
 
232
                                echo $sort->link($name,$label);
 
233
                        echo "</li>\n";
 
234
                }
 
235
                echo "</ul>";
 
236
                echo $this->sorterFooter;
 
237
                echo CHtml::closeTag('div');
 
238
        }
 
239
}