所以我有一个CListView,我可以使用我在sortableAttributes中设置的属性进行排序,当它只是ASC和DESC排序时,这很好.但我也想按类别对CListView进行排序.在我的模型中,我有一个类别,范围从0-8.我已经选择了显示类别的下拉菜单.
我想做的就是在下拉列表中选择一个选项时更新CListView,我可以为此编写自己的jQuery代码,但是我猜想有一些聪明的方法可以做到这一点.
谢谢
<?PHP $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$model->search(),
'sortableAttributes'=>array('views','create_time','release_time'),
'id'=>'#videos',
'itemView'=>$view,
'pager'=>array('cssFile'=>'/css/pager.css'),
)); ?>
解决方法:
经过大量的辛苦工作:
有两件事要做.首先,我们需要来自服务器的修改后的数据,
为此,您可以修改模型的搜索功能,因为那是为CListView提供数据提供程序的功能.
因此,在模型的search()函数中,您可以添加if条件来修改dataprovider的$criteria,例如,如下所示:
public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
// added the following if condition to modify the criteria to include only videos of a certain category
if (isset($_GET['category']))
$criteria->compare('category',$_GET['category'],true);// my category is string, hence the third attribute
else
$criteria->compare('category',$this->category,true);// we need the else part, coz the search function is used for actual searching also, for instance in gridview filters/search
$criteria->compare('id',$this->id);
// your other attributes follow
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
注意:我不确定在比较之前是否绝对有必要对$_GET [‘category’]进行清理.
其次,我们需要更新CListView,可以使用其功能$.fn.yiiListView.update.因此,例如:
<div id="categoryupdating">
<?PHP
echo CHtml::dropDownList('dropit', '',
array('1'=>'Cateogry1','2'=>'Category2','3'=>'Category3','4'=>'Category4'),
array('onchange'=>"$.fn.yiiListView.update('videos', {url: '".Yii::app()->createUrl('controller/action')."?category='+$('#dropit option:selected').val()})"));
?>
</div>
当然,您应该使用CHtml :: listData之类的函数动态填充下拉列表的数据,并且控制器/动作应该是CListView的控制器/动作.
查看jquery.yiilistview.js文件,以进一步了解yii的listview小部件的javascript函数.
注意:$.fn.yiiListView.update将列表视图的ID和要更新的URL作为参数.
编辑:还添加了else条件,因为搜索功能用于在gridview和其他地方进行实际搜索.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。