2010-04-22 63 views
0

我有一列從CGridView表"product" => {'product_id','category_id',...} 我有另一個表"category" => {'category_id','category_name'}更新CGridView當下拉值改變

category_id是FK在product表。

現在我想要一個類別表的下拉列表,並選擇一個特定的值產品的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?

如果上述方法不正確,請提供其他方法。

回答

1

這就是我最終做到的。我不知道解決方案是否最優化,但它的工作原理。 歡迎任何評論。在CGridView的列選項中選擇

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); }; ");

The above solution, however, needs the category_id。如果刪除,則其他列上的過濾不起作用。 如果保持其他列上的過濾器正常工作,但網格中存在category_id(這不是必需的) 需要一種方法來隱藏CGridView中的category_id列或其他解決方案。

1

你只需要在下拉菜單添加到CGridView,僅此而已,例如:

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

綜觀上述,您將需要兩個方法添加到您的分類等級:

getCategoryName getCategoryObj

閱讀:

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

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

你並不需要添加任何JavaScript :-)