3

したがって、sortableAttributesで設定した属性を使用して並べ替えることができるCListViewがあります。これは、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'),
)); ?>
4

2 に答える 2

2

将来の参考のために、これは私がそれをやった方法です。

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

$ .fn.yiiListView.update関数のIDとして「Video[category]」を使用することはできないため、重要な部分はdropDownListのhtmlOptionsのIDです。ただし、既存の検索機能を使用できるようにするには、選択の名前として必要です。

<?php echo CHtml::dropDownList(
    'Video[category]',
    0,
    Lookup::items('VideoCategory'),
    array('id' => 'category')
); ?>
于 2012-04-20T10:00:12.873 に答える
2

多くの苦労の後:

あなたがしなければならないことが2つあります。まず、サーバーからの変更されたデータが必要です。これは、モデルの検索機能を変更できるためです。これは、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のコントローラー/アクションである必要があります。

yiiのlistviewウィジェットのjavascript関数の詳細については、 jquery.yiilistview.jsファイルを確認してください。

:$。fn.yiiListView.updateはid、リストビューとurl更新の呼び出しをパラメーターとして受け取ります。

編集elseグリッドビューなどでの実際の検索に検索機能を使用するため、条件も追加しました。

于 2012-04-20T06:20:26.327 に答える