2013-06-20 3 views
0

계산 된 열이 있습니다. 정렬 가능하고 싶습니다. 나는 그것을 위해 get 메소드를 가지고 ...Yii Sortable Column in CGridView

public function getCur_margin() { 
     return number_format(($this->store_cost/$this->store_price)/$this->store_cost * 100, 2) . '%'; 
    } 

그리고 나는이 스레드를 따라 ...

CGridview custom field sortable

을하지만 지금은 데이터베이스의 실제 열을 찾고. <calculations> AS cur_margin을 사용하여이 CActiveDataProvider에 대해 일시적으로 필드를 가져올 수 있습니까?

$dataProvider = new CActiveDataProvider('Products', array(
    'criteria' => array(
     'condition' => 'store_id IN (SELECT `id` FROM `stores` WHERE `user_id` = ' . Yii::app()->user->id . ')', 
    ), 
    'pagination' => array(
     'pageSize' => 15, 
    ), 
    'sort'=>array(
     'attributes'=>array(
      'cur_margin' => array(
       'asc' => 'cur_margin ASC', 
       'desc' => 'cur_margin DESC', 
      ), 
     ), 
    ), 
)); 
+0

도움이 될 수 있습니다. http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in-a-cgridview/ – MrSoundless

답변

3

당신이 CDbCriteriaselect 속성에 사용자 정의 필드를 지정하는 시도 해 봤나 : 여기

는 공급자에 대한 컨트롤러 코드?

'select' => array(
    ..., // fields to select 
    '(<calculations>) AS cur_margin' 
) 

그런 다음 getCur_margin() 메소드를 선언하지만 모델의 공공 $cur_margin 멤버를 선언 할 필요가 없습니다. 필요한 모든 계산은 SQL 쿼리에서 수행됩니다.

예를 들어 this thread을 참조 할 수도 있습니다.

+0

시도했지만 그냥 아무것도하지 않습니다 – casraf

+0

열이 표시되지만 값이없고 여전히 정렬 할 수 없음) : – casraf

+0

나타남 선택 영역을 잘못된 위치에 추가했습니다. 그럼 정리해 봅시다 : gridvieww의 criteria, compare 및 column; 이 일이 정말 고마워요! – casraf