CListView, How to only get records starting with a given letter?

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. :)

Got it to work… Thanks. For anyone else, the & should be a ?

could this be somthing like this:




echo CHtml::link('A', array('FilteredListView', 'initialString'=>'a'));

echo CHtml::link('B', array('FilteredListView', 'initialString'=>'b'));

...

echo CHtml::link('Z', array('FilteredListView', 'initialString'=>'z'));



EDIT. HappyGiant originally posted above that he was getting an:

That was what I was addressing :)

Thanks, for the additional solution.

Yeah, Yii always had a neat code to write to replace dirty html.