Ayyanar G
Ayyanar G

Reputation: 1545

Filter issue in Yii

Im facing this filter issue, when i filter textbox value it gets filtered correctly according to entered text (partial text filter) , but when i filter values using dropdown it does not match the whole word it filter partially like in image.

enter image description here

My FiltersForm (filter method):

public function filter(array $data)
{
    foreach ($data AS $rowIndex => $row) {
        foreach ($this->filters AS $key => $searchValue) {
            if (!is_null($searchValue) AND $searchValue !== '') {
                $compareValue = null;

                if ($row instanceof CModel) {
                    if (isset($row->$key) == false) {
                        throw new CException("Property " . get_class($row) . "::{$key} does not exist!");
                    }
                    $compareValue = $row->$key;
                } elseif (is_array($row)) {
                    if (!array_key_exists($key, $row)) {
                        throw new CException("Key {$key} does not exist in array!");
                    }
                    $compareValue = $row[$key];
                } else {
                    throw new CException("Data in CArrayDataProvider must be an array of arrays or an array of CModels!");
                }

                if (stripos($compareValue, $searchValue) === false) {                       
                    unset($data[$rowIndex]);
                }
            }
        }
    }
    return $data;
}

I want whole word filter for dropdown (from image i want only '1' to be gets filtered). Can anyone guide me ??

Upvotes: 0

Views: 152

Answers (1)

Samuel Liew
Samuel Liew

Reputation: 79123

In your model's search() function, make sure the compare function's third parameter is set to false. If there is a true, it will use partial matching.

http://www.yiiframework.com/doc/api/1.1/CDbCriteria#compare-detail


Misread your question, looks like you implemented your own search filter function.

In this few lines of code, you are doing partial matching:

    if (stripos($compareValue, $searchValue) === false) {   

All you should need to do is to change it to:

    if ($compareValue !== $searchValue) {

Upvotes: 1

Related Questions