how to make CGridView filter to search values by: values starting with.
if i write ‘p’ should search values starting with letter ‘p’.
how to make CGridView filter to search values by: values starting with.
if i write ‘p’ should search values starting with letter ‘p’.
[font="arial, verdana, tahoma, sans-serif"][size="2"]You must modify your search() method in the model to do that.[/size][/font]
[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"] [/font][font="arial, verdana, tahoma, sans-serif"][size="2"]Changing
$criteria->compare('myfield',$this->myfield,true)
to
$criteria->compare('myfield',$this->myfield.'%',false)
might work (haven’t tried).[/size][/font]
I made it working but there is a bit of change in the CDbCriteria compare() method.
We have to escape the ‘%’ and ‘_’ else they will be treated as values to be searched.
So i added the $escape=TRUE parameter to the compare() method so now we can specify for each column how to search for its values.
Here is the code:
$criteria->compare('name',$this->name.'%',true,false);
Output:
when name=’’ it’ll get all rows.
when name=‘some-name’ it’ll get rows with name starting with ‘some-name’.
Changes in the compare method:
public function compare($column, $value, $partialMatch=false,$escape=true, $operator='AND')
{
...
if($partialMatch)
{
if($op==='')
return $this->addSearchCondition($column,$value,$escape,$operator);
if($op==='<>')
return $this->addSearchCondition($column,$value,$escape,$operator,'NOT LIKE');
}
...
}
i think this could be added to the default functionality of CDbCriteria compare, will provide more flexibility.
cheers,
this is been updated & committed changes for Yii v1.1.7.
public function compare($column, $value, $partialMatch=false, $operator='AND',$escape=true){...}
get updated CDbCriteria
cheers