Yii sorting CListView with a dropdown

So, I have a CListView that I can sort using the attributes that I set in sortableAttributes, which is great when it just sorts ASC and DESC. But I also want to sort CListView by category. In my model, I have a category ranging from 0-8. I made a drop-down list that shows categories.

What I would like to do is update my CListView, when an option is selected from the drop-down list, I could write my own jQuery code for this, but I assume there are some smart ways to do this.

thank

<?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'),
)); ?>
+3
source share
2 answers

After many difficulties:

, . -, , , , dataprovider CListView.

, search() if $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, javascript yii listview.

: $.fn.yiiListView.update id url .

: else, gridview .

+2

, .

Yii::app()->clientScript->registerScript('category', "
$('#category').change(function(){
    $.fn.yiiListView.update('videos', {
        data: $(this).serialize()
    });
    return false;
});
");

id htmlOptions dropDownList, "Video [category]" $.fn.yiiListView.update. select, .

<?php echo CHtml::dropDownList(
    'Video[category]',
    0,
    Lookup::items('VideoCategory'),
    array('id' => 'category')
); ?>
+2

All Articles