【问题标题】:yii sorting CListView with dropdownyii 使用下拉列表对 CListView 进行排序
【发布时间】:2023-03-03 21:20:01
【问题描述】:

所以我有一个 CListView,我可以使用我在 sortableAttributes 中设置的属性进行排序,这在它只是 ASC 和 DESC 排序时很好。但我也想按类别对 CListView 进行排序。在我的模型中,我有一个类别,范围为 0-8。我做了一个显示类别的下拉选择。

我想做的是在选择下拉列表中的一个选项时更新我的​​ CListView,我可以为此编写自己的 jQuery 代码,但我猜有一些聪明的 yii 方法可以做到这一点。

谢谢

<?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,那么呢?
  • 我只想显示类别等于 4 的项目。就像将其添加到 URI 中一样。 /index?Video[category]=4
  • 问题是我想用 AJAX 更新 CListView,就像我的 sortableAttributes 一样。

标签: php javascript jquery ajax yii


【解决方案1】:

经过一番辛劳:

你必须做两件事。首先,我们需要来自服务器的修改数据, 为此,您可以修改模型的搜索功能,因为这是为 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 文件以了解有关 yii 的 listview 小部件的 javascript 函数的更多信息。

注意:$.fn.yiiListView.update 将列表视图的id 和调用更新的url 作为参数。

编辑:还添加了else 条件,因为搜索功能用于在gridview 和其他地方进行实际搜索。

【讨论】:

  • 嘿,非常感谢您的意见。我想出了一个自己做的方法。由于等待 8 小时,昨晚无法发布。很抱歉浪费您的时间:(希望将来对其他人有所帮助:)
【解决方案2】:

为了将来参考,这就是我最终的做法。

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

重要的部分是 dropDownList 的 htmlOptions 中的 id,因为您不能使用 "Video[category]" 作为 $.fn.yiiListView.update 函数的 id。但是需要它作为选择的名称,才能使用已经存在的搜索能力。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2015-11-20
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多