yii-dataview icon indicating copy to clipboard operation
yii-dataview copied to clipboard

Suggestion: Html::clearButton() for search and filter forms

Open robsch opened this issue 8 years ago • 9 comments

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?

robsch avatar Jun 08 '17 14:06 robsch

While it makes sense I'm not sure such JavaScript should be in the framework core.

@yiisoft/core-developers opinions?

samdark avatar Jun 20 '17 23:06 samdark

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 .

arogachev avatar Jun 21 '17 06:06 arogachev

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.

klimov-paul avatar Jun 21 '17 09:06 klimov-paul

Agree. Let's a add it to gridview and leave Html helper as is.

samdark avatar Jun 21 '17 13:06 samdark

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"?

robsch avatar Jun 21 '17 14:06 robsch

Depends on implementation.

samdark avatar Jun 21 '17 22:06 samdark

Are we talking about creating a clearButton method in the GridView class or modifying the _search template in yii2-gii extension?

rrmontuan avatar Dec 10 '18 11:12 rrmontuan

GridView.

samdark avatar Dec 15 '18 22:12 samdark

https://github.com/yiisoft/yii2/pull/16978

samdark avatar Mar 20 '19 11:03 samdark

Closing since we're not going to provide any JS code in this package.

samdark avatar Sep 27 '24 17:09 samdark