This is a migrated thread and some comments may be shown as answers.
Ajax Grid Refresh
1 Answer 353 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Manuel
Top achievements
Rank 1
Manuel asked on 23 Jul 2010, 12:20 PM
I've just started with MVC2 and Telerik Controls, i need to refresh the grid with a filter which will be entered into a separate Textbox. The filter methods of the grid is not an option because of it's incompatibility with Castle ActiveRecord.

   $(document).ready(function () {
            $('#filterTextBox').change(function () {
                var query = $('#filterTextBox').val();
                var grid = $('#UserGrid').data('tGrid');
                grid.ajax.selectUrl = '/User/_GetUsers?filterText=' + query;
                grid.ajaxRequest();
            });
        });

This what i've got so far it works but i was wondering if there is a better way to access the grids ajax binding or to bind the value of the Textbox to the grid instead of the ViewData value.

dataBinding.Ajax().Select("_GetUsers", "User", new { filterText = (string)ViewData["filterText"] });

I am new to javascript too but the third thought i had was to set the ViewData value via JavaScript but i don't know how to do that and i don't know how to set up a databinding with javascript like the line below. I've read some posts about jquery post methods like this one

Refresh Grid

but the solution doesn't work for me. Is there a better way to refresh the grid with the entered filtering text?


1 Answer, 1 is accepted

Sort by
0
Chris
Top achievements
Rank 1
answered on 28 Oct 2010, 08:46 AM
Hi Manuel,
I came across the same problem and found a solution which seems more elegant. I use this way as common practice for querying grid data in my solution.

First you need to register the data binding event
<%
    Html.Telerik().Grid<VMember>()
        .Name("MemberGrid")
        .Columns(columns =>
        {
            ...
        })
        .DataBinding(dataBinding => dataBinding.Ajax()
                                     .Select("_Query", "Member"}))
        .Pageable(paging => paging.PageSize(20))
        .Filterable()
        .Sortable()
        .EnableCustomBinding(true)
        .DataKeys(keys => keys.Add(c => c.Id))
        .ClientEvents(events => events
            .OnDataBinding("memberBindingHandler"))
        .Render();
%>

Second in the memberBindingHandler, put the query information into e.data
// Passing parameters when binding data
memberBindingHandler
= function(e) {
      var queryString = composeQuery();
    e.data = { query: queryString };
}

Then in the contoller the query is sent as a parameter of action, along with GridCommand object which contains filter, group, and paging information to generate the query criteria at back end.
[GridAction(EnableCustomBinding = true)]
public ActionResult _Query(String query, GridCommand command)
{
    ....
    ....
    return View(new GridModel<VMember>
    {
        Data = result.ResultSet,
        Total = result.Total
    });
}

Hope this can help :)
Tags
Grid
Asked by
Manuel
Top achievements
Rank 1
Answers by
Chris
Top achievements
Rank 1
Share this question
or