sorting - yii sort by a custom attribute -
in vacation model vac have function
public function getvaccount(){ this function returns how many days there in 1 vacation.
and want add custom column cgridview this:
<?php $this->widget('zii.widgets.grid.cgridview', array( ... array( 'name' => 'count', 'value' => '$data->getvacperiod()' ), ... ), )); ?> it works fine. don't know how can sort upon custom attribute. tried use csort not work. idea?
to use csort sorting, you'll need convert vacation function sql query , stash results in public variable in model.
csort works sql statements/functions, underneath it's using order sorting.
more info (and demo code) available here
here's sample of how i'm doing on site of mine:
$criteria->select = array( "*", new cdbexpression("if(survey.requestdate, survey.requestdate, surveycompletedate) surveydate") ); this allows me type of filter:
return new cactivedataprovider($this, array( 'criteria' => $criteria, 'sort'=>array( 'attributes'=>array( 'surveydate' => array( 'asc' => 'surveydate', 'desc' => 'surveydate desc', ), '*', ), ), ); note: you'll need public variable defined in model hold results of cdbexpression you're doing. mine called surveydate.
Comments
Post a Comment