This is a migrated thread and some comments may be shown as answers.

Automatically Clear Filter on MVC Grid

9 Answers 524 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Russell Solberg
Top achievements
Rank 1
Russell Solberg asked on 26 Jan 2010, 05:05 PM
I have a grid that allows the user to filter.  If the user changes the search word that is used to populate the grid, the filter from the previous search remains in place.

<label for="UserName"
    User Name:</label> 
<%= Html.TextBox("UserName", "") %> 
&nbsp; &nbsp; 
<input id="btnSearch" type="submit" value="Submit" /> 
</p> 
<div class="<%= "t-" + Html.GetCurrentTheme() %>style="width: 400px;"
<%= Html.Telerik().Grid<ADGroup>()       
        .Name("Groups") 
        .Columns(columns=> 
        { 
            columns.Add(c => c.GroupName).Width(350); 
        }) 
        .Sortable() 
        .Filterable() 
        .Pageable(paging => 
            paging.PageSize(20) 
        ) 
        .Ajax(ajax => ajax.Action("_GetGroups", "GroupSearch", new { userName = "John Doh" })) 
        .BindTo((IEnumerable<ADGroup>)ViewData["Groups"]) 
%> 
</div> 

I have triggered the rebind of the gird to occur when btnSearch is pressed. 
    <
    Html.Telerik().ScriptRegistrar() 
        .OnDocumentReady(() =>  
        { 
            %> 
            var groupsGrid = $('#Groups').data('tGrid'); 
            $('#btnSearch') 
                .live("click", function() { 
                    var user = $('#UserName').val(); 
                    // rebind the related grid 
                    groupsGrid.rebind({ 
                        userName: user 
                    }); 
                }); 
            <%  
        }); 
    %> 

I know that I can add the following code which will bring up the filter menu, but I'd prefer to be able to automagically clear the filter before the .rebind() call occurs.
$('.t-grid-filter:first') 
     .trigger('click'); 

9 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 27 Jan 2010, 08:37 AM
Hello Russell Solberg,

You can call this code before rebind():

$('#Groups .t-filter-options')
                    .find('input[type="text"], select')
                    .val('');

It would clear the filtering UI.

However I think this should be done by the rebind method itself so I have added it our code.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Russell Solberg
Top achievements
Rank 1
answered on 27 Jan 2010, 03:54 PM
Thanks.  I'll go ahead and execute that code before the rebind occurs.  When do you think the code change you made will be released?
0
Russell Solberg
Top achievements
Rank 1
answered on 27 Jan 2010, 04:12 PM
This seems to work.  Rather than clearing out the text value, simply simulating the clicks on the clear button and the filter button.

//Clear UI Filter Text 
$('#Groups .t-clear-button').click(); 
$('#Groups .t-filter-button').click(); 
 
// rebind the related grid 
groupsGrid.rebind({ 
    userName: user 
}); 
0
Atanas Korchev
Telerik team
answered on 28 Jan 2010, 08:04 AM
Hello Russell Solberg,

Perhaps I don't quite follow. Calling the rebind method will clear the filter (logically). The code I've sent' you will clear the UI. Could you please elaborate a bit more on your requirements?

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Russell Solberg
Top achievements
Rank 1
answered on 28 Jan 2010, 04:46 PM
The code you sent only clears whatever the value was that was typed into the filter box.  It does not change the filter that has been applied to the grid.  It doesn't appear the rebind actually clears the grid, it simply uses whatever the previous filter was.

The behavior I noticed with your code sample was that the filter value would go away, however the applied filter on the grid would remain.  I literally want the filter value to go away and when the grid rebinds to do it as if there was not a filter.
0
Atanas Korchev
Telerik team
answered on 29 Jan 2010, 08:57 AM
Hello Russell Solberg,

I managed to reproduce the problem. Fortunately the fix was easy: open telerik.grid.js and find the following code:

rebind:function(h){this.sorted=[];

Then replace it with:

rebind:function(h){this.sorted=[];this.filterExpression='';

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Eric
Top achievements
Rank 1
answered on 01 Sep 2010, 06:42 PM
I am trying to implement a clear all filter button that resides in my grids toobar, and it doesn't appear to be working.

In my grid declaration:
.ToolBar(toolbar => toolbar.Custom().Text("Clear Filters").HtmlAttributes(new 
{ onclick = "clearFilters(event)" }))

My onclick callback:
function clearFilters(e) {
    var grid = $('#Matrix').data('tGrid');
    grid.rebind(); } When debugging this with firefox I show that the grid's filterby = "", and the column that had the filter now has filters = []. So, the filters are cleared in the code, but when the callback returns, the filters are still present. I tried many of the suggestions on this post and many others, but it still doesn't work. Any suggestions? I just tried on additional thing, if I move the button outside of the grid's toolbar, it appears to be clearing the text of  the filter, but it doesn't actually remove the filter and show all the data without filters.
0
Eric
Top achievements
Rank 1
answered on 01 Sep 2010, 07:06 PM
Found a solution, but only for a button outside of the grid's toolbar.  For some reason,
 the same solution doesn't work when placing the button inside of the toolbar:

function clearFilters(e) {
    var grid = $('#Matrix').data('tGrid');
grid.rebind();
grid.filter(grid.filterExpr()); } So, does anyone know why the button inside of the grids toolbar doesn't work, but one outside of the grid does?
0
SEAN
Top achievements
Rank 1
answered on 25 Nov 2011, 09:26 PM
Not sure about that, but this works from a button on the toolbar:

function clearFilters(e) {
    e = new $.Event(e);
    e.preventDefault();
 
    //First clear the filter dialogs - took this from clearClick in telerik.grid.filtering.js
    $('div.t-filter-options')
        .find('input')
        .removeAttr('checked')
        .removeClass('t-state-error')
        .not(':radio')
        .val('')
        .end()
        .end()
        .find('select')
        .removeClass('t-state-error')
        .find('option:first')
        .attr('selected', 'selected');
 
    //Now iterate through the columns and clear the filters - this will cause the filter method to reset the filter icons
    var grid = $('#Grid').data('tGrid');
    $.each(grid.columns, function (index, value) {
        if (this.filters != null) this.filters = null;
    });
 
    //Now the filter method will do the rest
    grid.filter('');
}
Tags
Grid
Asked by
Russell Solberg
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Russell Solberg
Top achievements
Rank 1
Eric
Top achievements
Rank 1
SEAN
Top achievements
Rank 1
Share this question
or