Have a list of records that sort, but I also want to add the option to filter/condition them dynamically based on their first letter. Click on A, only records starting with A are shown. I know how to do this with just PHP/MySQL, but how would you do this using Yii?
doing this in the Yii way u have to update the CDbCriteria compare() method which now allows to do customized partial matching by not escaping the ‘%’ and ‘_’ chars.
get updated CDbCriteria here
// in the controller create an action like this
public function actionFilteredListView($initialString='')
{
$criteria=new CDbCriteria;
$criteria->compare('name',$initialString.'%',true,'AND', false); // replace 'name' to ur search field
$dataProvider=new CActiveDataProvider('employee', array('criteria'=>$criteria,)); // replace 'employee' to ur model
$this->render('index',array('dataProvider'=>$dataProvider,));
}
// in the index view
// u can obviously use a pager to generate these links
echo CHtml::link('A','/path/to/controller/FilteredListView&initialString=a');
echo CHtml::link('B','/path/to/controller/FilteredListView&initialString=b');
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
hope this helps…
[EDIT]: @HappyGiant, the '& ’ there is correct coz ‘/path/to/controller/FilteredListView’ is also a query string param, its not the full url, remember r=. so the ‘initialString’ is the 2nd query param & we use ‘&’ separate query params.