yii-dataview
yii-dataview copied to clipboard
Suggestion: Html::clearButton() for search and filter forms
In 8608 someone else has already thought that Html::resetButton() would work differently. I just stumbled over this too and I wondered if it would be possible to introduce something like Html::clearButton(). Although I know that it is the standard behaviour, the reset button of a form is quite useless, and it would be very helpful if there would be a way to clear a form and possibly submit it with the same click.
I think it is a very common requirement for search/filter inputs. And it would be also nice if gii could generate forms with such buttons. Could be also introduced for GridView filter inputs.
Currently, I do the following which seems to work:
<?= Html::resetButton('Reset', [
'class' => 'btn btn-default',
'onclick' => "$(this).closest('form').find('input, select, textarea').val(''); " .
"$(this).closest('form').submit(); " .
"return false;"
]) ?>
Not perfect - it requires that the select has an empty option.
What do you think?
While it makes sense I'm not sure such JavaScript should be in the framework core.
@yiisoft/core-developers opinions?
The way I usually use to reset a form is the button with a link to initial url.
For example, if you have applied filter something like this:
/enterprises/?region=1&district=1&settlement=9
You can use this link and after click on it the form will be reset to initial condition:
<a class="btn btn-default" href="/enterprises/">Reset</a>
This method is simple, suitable for most cases and does not not require any additional JS .
I agreed with @arogachev : it should be a simple link. We already have Url::canonical(), which may help in such link composition, however automatic generation of such URL is too risky.
I do not consider putting such functionality into Html helper is valid. However filter reset button can be added to GridView widget as it already have a filterUrl field for this.
Agree. Let's a add it to gridview and leave Html helper as is.
Fine. Your're right, Html::clearButton() is not necessary if a button with an appropiate URL can be realized easily (current URL, just without the filter parameters). But the GridView button would be quite useful! Especially if gii could generate it automatically or if it is automatically part of the GridView, since it is such a common requirement.
But ListView cannot get such a button automatically?
Another question: is this GridView clear function then "pjax-able"?
Depends on implementation.
Are we talking about creating a clearButton method in the GridView class or modifying the _search template in yii2-gii extension?
GridView.
https://github.com/yiisoft/yii2/pull/16978
Closing since we're not going to provide any JS code in this package.