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

Server side binding for initial load, but ajax binding for page changes

4 Answers 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gavin
Top achievements
Rank 1
Gavin asked on 07 May 2013, 02:01 AM
Hi
I want to load the initial first page of a grid using the first page of data bound from a server side collection.  Subsequent page changes should be loaded using ajax. 

My problem is how to set the total records of the collection to a number so that the correct number of pages are displayed.

So in the example below, Model.Calls may only contain 20 records, but the full collection count could be 100.  How can I tell the grid there are in fact 100 records, not 20?

@(Html.Kendo().Grid<CallsViewData>(Model.Calls)
      .Name("CallsGrid")
      .Columns(c => {
                        c.Bound(m => m.TestResultId).Groupable(false).Title(@Html.Resource("HeadTestResultId"));
                        c.Bound(m => m.RunType).Title(@Html.Resource("HeadType"));
                        c.Bound(m => m.CallFlow).Title(@Html.Resource("HeadDirection"));
                        c.Bound(m => m.PhoneNo).Title(@Html.Resource("HeadCalledNo"));
                        c.Bound(m => m.ActualStart).Title(@Html.Resource("HeadStartDate"));
                        c.Bound(m => m.Connected).Title(@Html.Resource("HeadConnectedDate"));
                        c.Bound(m => m.Disconnected).Title(@Html.Resource("HeadDisconnectedDate"));
                        c.Bound(m => m.Duration).Title(@Html.Resource("HeadDuration"));
                        c.Bound(m => m.Duration).Title(@Html.Resource("HeadMinutes"));
      })
      .Resizable(s => s.Columns(true))
      .Pageable()
      .DataSource(ds => ds.Server().Total(100))
      .BindTo(Model.Calls)
      .Sortable()
      .Scrollable()
      .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .Read(read => read.Action("calls", "report").Data("tt").Type(HttpVerbs.Post    ))
                                    .PageSize(20)
      ))

4 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 08 May 2013, 01:01 PM
Hello Gavin,

You should pass all the 100 records to the Grid constructor - the Grid will automatically apply paging and serialize just 20 records and display them.

If you want to continue and pass only 20 records, specify the Total count manually, then you should consider implementing the Custom Binding.

Kind Regards,
Petur Subev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Gavin
Top achievements
Rank 1
answered on 08 May 2013, 10:06 PM
Hi
Kind of defeats the purpose of paging if you have to supply the entire collection to the binder.

Here's how I solved it:

 @(Html.Kendo().Grid<CallsViewData>()
              .Name("CallsGrid")
              .Columns(c =>
                  {
                      c.Bound(m => m.TestResultId).Groupable(false).Title(@Html.Resource("HeadTestResultId"));
                      c.Bound(m => m.RunType).Title(@Html.Resource("HeadType"));
                      c.Bound(m => m.CallFlow).Title(@Html.Resource("HeadDirection"));
                      c.Bound(m => m.PhoneNo).Title(@Html.Resource("HeadCalledNo"));
                      c.Bound(m => m.ActualStart).Title(@Html.Resource("HeadStartDate"));
                      c.Bound(m => m.Connected).Title(@Html.Resource("HeadConnectedDate"));
                      c.Bound(m => m.Disconnected).Title(@Html.Resource("HeadDisconnectedDate"));
                      c.Bound(m => m.Duration).Title(@Html.Resource("HeadDuration"));
                      c.Bound(m => m.Duration).Title(@Html.Resource("HeadMinutes"));
                  })
              .Resizable(s => s.Columns(true))
              .Pageable(p => p.PageSizes(MvcApplication.Settings.PageSizesList))
              .Scrollable()
              .EnableCustomBinding(true)
              .BindTo(Model.Calls)
              .DataSource(dataSource => dataSource.Server().Total(Model.CollectionSize.Value))
              .DataSource(dataSource => dataSource
                                            .Ajax()
                                            .Read(read => read.Action("clientcalls", "report").Data("$.proxy(CallsGridExt.attachFormFields, CallsGridExt)").Type(HttpVerbs.Post))
                                            .PageSize(MvcApplication.Settings.DefaultPageSize)
                                            .Events(e => e.RequestStart("$.proxy(CallsGridExt.surpressInitialAjax, CallsGridExt)").Error("$.proxy(CallsGridExt.handleError,CallsGridExt)"))
              ))
        <script type="text/javascript">
            var CallsGridExt = new KendoGridExtensions({ form: '#callsreport' });
        </script>


function KendoGridExtensions(args) {
    this._firstRequest = true;
    this._args = args;
};
KendoGridExtensions.prototype = {
    handleError: function(e) {
        alert(e.errors.join('\n'));
    },
    surpressInitialAjax: function(e) {
        if (this._firstRequest) {
            this._firstRequest = false;
            e.preventDefault();
        }
    },
    attachFormFields: function(parameters) {
        $(this._args.form).serializeObject(parameters);
    }
};
0
Venkata
Top achievements
Rank 1
answered on 10 Jul 2013, 06:34 PM
Hi Gavin,

I have the exact same requirement and I am sure most of the people who use grid would want to take the same approach. Can you share your (cshtml) if possible.?

Thanks,
Venkat
0
Josh
Top achievements
Rank 1
answered on 13 Feb 2015, 09:41 PM
This post is old but I thought I would share that I accomplished intial server databinding with ajax binding on page request simply by putting the grid into CustomBinding. 

Basically the code provided here but no need put in the initial check for first request. 
Because the grid was server databound to list the autobinding does not occur so no need to check that.

A later release may have enabled this functionality after this post was made but thought I would share none the less.
Tags
Grid
Asked by
Gavin
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Gavin
Top achievements
Rank 1
Venkata
Top achievements
Rank 1
Josh
Top achievements
Rank 1
Share this question
or