0

我有一个 CGridView,其中包含一个表中的列"product" => {'product_id','category_id',...} 我有另一个表"category" => {'category_id','category_name'}

category_idproduct表中的 FK。

现在我想要一个类别表的下拉列表,在选择特定值时,产品的 CGridView 应该更新为仅显示带有该值的行category_id。我还需要 CGridView 的列过滤/排序才能工作(使用 AJAX)。

当从下拉列表中选择一个值时,我能够刷新 CGridView,但是我无法为 CGridView 发送带有“数据”的 category_id:

clientScript->registerScript('search', "
$('.cat_dropdown').change(function(){
    $.fn.yiiGridView.update('order-grid', {
        data: $(this).serialize(),
    });
    return false;
});
");
data: $(this).serialize()仅发送 CGridView 的过滤文本字段中存在的值 。我如何附加 category_id 呢?

如果上述方法不正确,请提出替代方法。

4

2 回答 2

1

这就是我最终做到的方式。我不知道解决方案是否最佳,但它有效。欢迎任何意见。

Yii::app()->clientScript->registerScript('yiiGridView.update', " $.fn.yiiGridView.update = function(id, options) { var settings = $.fn.yiiGridView.settings[id]; var data = { 'product[category_id]': $('.cat_dropdown option:selected').val(), }; options = $.extend({ type: 'GET', data: data, success: function(data,status) { $.each(settings.ajaxUpdate, function() { $('#'+this).replaceWith($(data).find('#'+this)); }); if(settings.afterUpdate != undefined) settings.afterUpdate(id, data); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.responseText); } }, options || {}); if(settings.beforeUpdate != undefined) settings.beforeUpdate(id); $.ajax(options); }; ");

但是,上述解决方案需要category_idCGridView 的列选项。如果将其删除,则其他列上的过滤将不起作用。如果它保持对其他列的过滤工作,但category_id网格中存在(这不是必需的)需要一种方法来隐藏category_idCGridView 中的列或其他一些解决方案。

于 2010-04-25T05:21:14.137 回答
1

您只需要将下拉菜单添加到 CGridView,仅此而已,例如:

array('name' => 'category_id', 'value' => 'Categories::model()->getCategoryName($data->category_id)', 'filter'=>CHtml::listData(Categories::model()->getCategoryObj(), 'category_id', 'category_name')),

查看上面的内容,您需要向 Categories 类添加两个方法:

getCategoryName getCategoryObj

读这个:

http://www.mattiressler.com/customising-cgridview-select-menu/

http://www.mattiressler.com/using-class-properties-to-minimise-database-queries/

您不需要添加任何 javascript :-)

于 2012-05-01T22:17:06.373 回答