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

select first row after filter (client-side)

2 Answers 252 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bart Reekmans
Top achievements
Rank 1
Bart Reekmans asked on 11 Feb 2013, 02:16 PM
Hi,

We are trying to select the grid's first row, after applying a (client-side) filter. Our code is as follows :
However, the following line of code : alert(Rows.length); still returns the original number of items in the grid (meaning no filter applied)
Apparently the filtering happens after the remaining code? Any ideas on this behaviour?
function selectFirstRow() {
                    var grid = $find("<%=OnderzoekenGrid.ClientID %>")
                    if (grid != null)
                        grid.get_masterTableView().selectItem(0);
                }
 
                function filterPatType(sender, args) {
                    var t = sender.get_text();
                    var masterTable = $find("<%= OnderzoekenGrid.ClientID %>").get_masterTableView();
                    masterTable.clearFilter();
                    if (t == "Ambulanten") {
                        masterTable.filter('typepatient', 'A', Telerik.Web.UI.GridFilterFunction.EqualTo, true); 
                    }
                    else if (t == "Gehospitaliseerden") {
                        masterTable.filter('typepatient', 'H', Telerik.Web.UI.GridFilterFunction.EqualTo, true);
                    }
                    var Rows = masterTable.get_dataItems();
                    alert(Rows.length);
 
                    selectFirstRow();
                }

2 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 14 Feb 2013, 12:21 PM
Hi Chris,

Thank you for contacting us.

You can use the following approach to achieve the requested functionality:
bool firstSelected = false;
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        Pair filterDetails = e.CommandArgument as Pair;
        string columnName = filterDetails.Second.ToString();
 
        if (columnName == "typepatient")
        {
            firstSelected = true;
        }
    }
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (firstSelected)
    {
        RadGrid1.MasterTableView.Items[0].Selected = true;
    }
}

Please note that both clearFilters and filter methods of the table view client object make a postback to the server. Therefore, they should not be called simultaneously in the same function. Analogously, no client side operation should be performed directly after the filter command since it will be called instantly before the actual filtering has been applied. In case you want to achieve the desired behavior on the client, you can use a HiddenField:
<asp:HiddenField ID="HiddenField1" runat="server" Value="false" />
JavaScript:
function filterPatType(sender, args) {
    $get("<%= HiddenField1.ClientID %>").value = "true";
    var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
    masterTable.filter('ShipCountry', 'France', Telerik.Web.UI.GridFilterFunction.EqualTo, true);
}
function pageLoad() {
    var value = $get("<%= HiddenField1.ClientID %>").value;
    if (value == "true") {
        $get("<%= HiddenField1.ClientID %>").value = "false";
 
        var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
        masterTable.selectItem(0);
    }
}

That should do the trick. Please give it a try and let me know about the result.

Regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Bart Reekmans
Top achievements
Rank 1
answered on 15 Feb 2013, 09:35 AM
Ok, just confused here.
I was hoping that a client-side filter-function would perform the entire filtering client-side, and thus without postback.
Thx for the explanation
Tags
Grid
Asked by
Bart Reekmans
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Bart Reekmans
Top achievements
Rank 1
Share this question
or