So, I’ve been wondering how can I filter a column with money values in a CgridView widget. Let’s say I have money values in my database and I have something like “68.0” but I’d like to format those values in my GridView so I put a “formatPrice” function in my Product model like this:
public function formatPrice($price) {
if ($price != "") {
$money = number_format($price,2);
$money = "$ ".$money;
}
else {
$money = "";
}
return ($money);
}
}
I then create a column in my "admin" view for my cGridView like this:
The view will be empty always if I type a value in the filter box. I know I’m probably missing something really basic here but spare my life and help me please!! hehe
The model instance you are calling search() on is the very model instance that holds the posted filter values. You rather should call some unFormatPrice() method for matching to the DB type.
Depending on how you intend to use this filter you may consider to set partialMatch to true. Did you succeed with other filter columns? If in doubt, I think it’s a good idea to try out filtering an integer field first, before continuing with the money type (I’ve not tried something similar myself).
By adding “true” it now looks for any substring containing the value and the filter is correct. However, in the second case it wasn’t necessary to user “unFormatPrice”. I’m kind of lost here, in the compare function, the first value is the value in the column or the value in the database?
You are right, but what I meant with "unFormat…" was to remove any formatting (as in the grid fields) the user might enter in the filter field. So I can see a possibility for conditioning the value before calling compare().