New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comparing operators like >,< >=,<= to search in GridView #2022
Comments
where have you seen this feature? its only in debug module afaik in Yii2. |
@Ragazzo yii1.1 has this. |
I think that will be much useful having it. |
related to #2315 |
hmm. any workaround on this? the use case is simple.. you have a grid with a bunch of dates and you only want to see the data after a specific date... or an int anything bigger than xyz and so on. |
if anyone needs a workaround: public function search($params)
{
$query = AllEdition::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (isset($_GET['EvtSearch']) && !($this->load($params) && $this->validate())) {
return $dataProvider;
}
foreach (AllEdition::getSearchColumns() as $columnname){
$operator = $this->getOperator($this->$columnname);
$operand = str_replace($operator,'',$this->$columnname);
$query->andFilterWhere([$operator, $columnname, $operand]);
}
return $dataProvider;
}
private function getOperator($qryString){
switch ($qryString){
case strpos($qryString,'>=') === 0:
$operator = '>=';
break;
case strpos($qryString,'>') === 0:
$operator = '>';
break;
case strpos($qryString,'<=') === 0:
$operator = '<=';
break;
case strpos($qryString,'<') === 0:
$operator = '<';
break;
default:
$operator = 'like';
break;
}
return $operator;
} |
I did this to filter pages on number of views: if (!empty($this->views)) {
preg_match('/(<=|>=|<|>)/', $this->views, $operator);
preg_match('/\d+/', $this->views, $views);
$operator = isset($operator[0]) ? $operator[0] : '=';
$views = isset($views[0]) ? $views[0] : '0';
$query->andFilterWhere([$operator,'views', $views]);
} |
how did you archive that the operator comes back to the filter field after filtering? |
Not sure what you mean. |
the problem is you have to replace the operator in views otherwise the sql is wrong. e.g. if operator is '>=' because of that i replace the operator with an empty string. |
What my example does is it extracts operator In your case you modify the filter input. Try this in your $operator = $this->getOperator($this->$columnname);
$operand = str_replace($operator,'',$this->$columnname);
$query->andFilterWhere([$operator, $columnname, $operand]); |
oh didnt saw that. thanks for the hint. |
@ItsReddi I'm trying your workaround but what is getSearchColumns()? |
its just an array of fields. |
👍 What happen with Notice that: |
Hey, guys. I wonder how do you envision implementing this? I will need this for rest api in my project and can dig and make a contribution, will just need a few pointers. |
@ilyapoz we have no concrete ideas or plans on this. you could make a proposal for an implementation and we can discuss it. |
Basic implementation proposal for yiisoft#2022 Shortcomings/todo's: * filtering on non-scalars (where IN statements) not supported * Numeric properties with a corresponding validation rule will yield errors when they contain an alphanumeric operator. These properties' rules should be defined as 'string' or 'safe'. * no implementation example in Gii code (yet).
Something like that? ^^ I'd find this feature very helpful too. Yii 1's CDbCriteria::compare() was very user friendly. |
@lennartvdd I guess functions with the property "Note that when the value is empty, no comparison expression will be added to the search condition." have "filter" in their name in yii2 by convention. |
ah alright! Makes sense! how about |
I guess it's time to open a PR :) |
any updates on this issue? |
@ilyapoz To use currently available operator format This way we would keep implementations of As for validation, I would extend |
@ArekX i don't think its that simple and even if you manage to do that, how do you fix the validation rules?
for example, what if I send |
@Faryshta |
thats the proposed syntax, but how do you solve it? i mean how do you propose allowOperators to work internally? |
If you look at this line in the You will see a number pattern which is used for checking whether or not a value is an actual number. You can easily prefix this regex with operators so it will look like this: So if you make another public variable like Of course you would have separate a number from operator to perform Another solution would be to put operators into a separate variable like Same should also apply for when Because this code could easily get jumbled up with standard |
thats a good idea. i think you have solved it to the point where you can make a patch and tests. |
So? Is this implemented or any plans to implement this option? |
I did not implement anything from the latest comments, but there's a PR for a GridView comparison function ready and waiting to be merged for quite a while now in #8505 |
Basic implementation proposal for #2022 Shortcomings/todo's: * filtering on non-scalars (where IN statements) not supported * Numeric properties with a corresponding validation rule will yield errors when they contain an alphanumeric operator. These properties' rules should be defined as 'string' or 'safe'. * no implementation example in Gii code (yet). close #8505
merged #8505, keeping this open for documentation. |
Basic implementation proposal for yiisoft/yii2#2022 Shortcomings/todo's: * filtering on non-scalars (where IN statements) not supported * Numeric properties with a corresponding validation rule will yield errors when they contain an alphanumeric operator. These properties' rules should be defined as 'string' or 'safe'. * no implementation example in Gii code (yet). close #8505
Yii1 Gridview has a feature to search with comparing operators like >,< >=,<= in GridView,
I can't see they are working now with gii generated code?
The text was updated successfully, but these errors were encountered: